广告位联系
返回顶部

git config怎么用?常用配置命令及添加删除配置项的方法

相关其他 来源:互联网 作者:佚名 发布时间:2026-09-29 22:09:22 人浏览
摘要

一、config文件分类 Git中有三层config文件:系统、全局、本地 查看不同级别的配置文件: 1 2 3 4 5 6 7 8 #查看系统config git config --system --list #查看当前用户(global)配置 git config --global --list #查看

一、config文件分类

Git中有三层config文件:系统、全局、本地

查看不同级别的配置文件:

1

2

3

4

5

6

7

8

#查看系统config

git config --system --list

  

#查看当前用户(global)配置

git config --global  --list

  

#查看当前仓库配置信息

git config --local  --list

  • /etc/gitconfig:包含了适用于系统所有用户和所有项目的值。注是git的安装目录(Win:D:\Git\mingw64\etc\gitconfig) --system 系统级
  • ~/.gitconfig:只适用于当前登录用户的配置。(Win:C:\Users\Administrator.gitconfig) --global 全局

位于git项目目录中的.git/config:适用于特定git项目的配置。–local当前项目

注意:对于同一配置项,三个配置文件的优先级是1<2<3

Git 使用一系列配置文件来保存你自定义的行为。 它首先会查找系统级的 /etc/gitconfig 文件,该文件含有系统里每位用户及他们所拥有的仓库的配置值。

如果你传递 --system 选项给 git config,它就会读写该文件。

接下来 Git 会查找每个用户的 ~/.gitconfig 文件(或者 ~/.config/git/config 文件)。 你可以传递 --global 选项让 Git 读写该文件。

最后 Git 会查找你正在操作的仓库所对应的 Git 目录下的配置文件(.git/config)。 这个文件中的值只对该仓库有效,它对应于向 git config 传递 --local 选项。

以上三个层次中每层的配置(系统、全局、本地)都会覆盖掉上一层次的配置,所以 .git/config 中的值会覆盖掉 /etc/gitconfig 中所对应的值。

Note:

Git 的配置文件是纯文本的,所以你可以直接手动编辑这些配置文件,输入合乎语法的值。 但是运行 git config 命令会更简单些。

二、设置用户名与邮箱(用户标识,必要)

1、当你安装Git后首先要做的事情是设置你的用户名称和e-mail地址。

这是非常重要的,因为每次Git提交都会使用该信息。它被永远的嵌入到了你的提交中:

1

2

$ git config --global user.name "***"  #名称

$ git config --global user.email ****@qq.com   #邮箱

只需要做一次这个设置,如果你传递了–global 选项,因为Git将总是会使用该信息来处理你在系统中所做的一切操作。

如果你希望在一个特定的项目中使用不同的名称或e-mail地址,你可以在该项目中运行该命令而不要–global选项。 总之–global为全局配置,不加为某个项目的特定配置。

修改如上图2所示;

2、添加或删除配置项

1)、添加配置项

1

2

3

4

5

6

7

8

git config [--local|--global|--system]  section.key value

 

[--local|--global|--system]  #可选的,对应本地,全局,系统不同级别的设置

section.key #区域下的键

value #对应的值

--local 项目级

--global 当前用户级

--system 系统级

2)、删除配置项

1

git config [--local|--global|--system] --unset section.key

3、更多配置项

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

git config --global color.ui true   #打开所有的默认终端着色

git config --global alias.ci commit   #别名 ci 是commit的别名

[alias] 

co = checkout 

ci = commit 

st = status 

pl = pull 

ps = push 

dt = difftool 

l = log --stat 

cp = cherry-pick 

ca = commit -a 

b = branch

 

user.name  #用户名

user.email  #邮箱

core.editor  #文本编辑器 

merge.tool  #差异分析工具 

core.paper "less -N"  #配置显示方式 

color.diff true  #diff颜色配置 

alias.co checkout  #设置别名

git config user.name  #获得用户名

git config core.filemode false  #忽略修改权限的文件 

4、所有config命令参数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

语法: git config [<options>]       

         

文件位置       

    --global                  #use global config file 使用全局配置文件

    --system                  #use system config file 使用系统配置文件

    --local                   #use repository config file    使用存储库配置文件

    -f, --file <file>         #use given config file    使用给定的配置文件

    --blob <blob-id>          #read config from given blob object    从给定的对象中读取配置

         

动作       

    --get                     #get value: name [value-regex]    获得值:[值]名[正则表达式]

    --get-all                 #get all values: key [value-regex]    获得所有值:[值]名[正则表达式]

    --get-regexp          #get values for regexp: name-regex [value-regex]    得到的值根据正则

    --get-urlmatch            #get value specific for the URL: section[.var] URL    为URL获取特定的值

    --replace-all             #replace all matching variables: name value [value_regex]    替换所有匹配的变量:名称值[ value_regex ]

    --add                     #add a new variable: name value    添加一个新变量:name值

    --unset                   #remove a variable: name [value-regex]    删除一个变量名[值]:正则表达式

    --unset-all               #remove all matches: name [value-regex]    删除所有匹配的正则表达式:名称[值]

    --rename-section          #rename section: old-name new-name    重命名部分:旧名称 新名称

    --remove-section          #remove a section: name    删除部分:名称

    -l, --list                #list all    列出所有

    -e, --edit            #open an editor    打开一个编辑器

    --get-color               #find the color configured: slot [default]    找到配置的颜色:插槽[默认]

    --get-colorbool           #find the color setting: slot [stdout-is-tty]    发现颜色设置:槽[ stdout是TTY ]

         

类型       

    --bool                    #value is "true" or "false"    值是“真”或“假”。

    --int                     #value is decimal number    值是十进制数。

    --bool-or-int             #value is --bool or --int    值--布尔或int

    --path                    #value is a path (file or directory name)    值是路径(文件或目录名)

         

其它       

    -z, --null                #terminate values with NUL byte    终止值与null字节

    --name-only               #show variable names only    只显示变量名

    --includes                #respect include directives on lookup    尊重包括查找指令

    --show-origin             #show origin of config (file, standard input, blob, command line)    显示配置(文件、标准输入、数据块、命令行)的来源

三、解决git每次提交和拉取代码需要输入用户名和密码问题

输入以下命令行:

1

git config --global credential.helper store

再输入一次正确的用户名和密码,就可以了


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计