Git 是分布式版本控制系统,所以,每个机器都必须自报家门:user.name 和 user.email
如果不配置用户信息,是无法进行后续操作的。
全局配置
git config --global user.name "Your username"
git config --global user.email "Your email"
可以用下面的命令为 Git 启用额外的颜色
git config --global color.ui true
局部配置
需要在当前仓库下进行
git config user.name "Your username"
git config user.email "Your email"
git 级别优先级
- 仓库级 - local 【最高】
- 全局级 - global 【中等】
- 系统级 - system 【最低】
全局级配置文件 - global
-
Linux 的 .gitconfig 文件一般在当前用户主目录 cd ~
-
Windows 的 .gitconfig 文件路径一般是 C:\Users\Administrator
通过命令查看全局级配置文件:git config --global -l
仓库级配置文件 - local
Git 仓库级配置文件都在当前仓库下的 .git/config 里面
Windows 中的 .git 文件是隐藏的,需要在文件管理器中勾选【隐藏的项目】
通过命令查看仓库级配置文件:git config --local -l
系统级配置文件 - system
Git 系统级配置文件都 Git 安装目录下的 /etc/gitconfig 里面
通过命令查看系统级配置文件:git config --system -l
查看配置文件
命令参数:–list
简写:-l
格式:git config [–local | --global | --system] -l
- 仓库级 config:git config --local -l
- 全局级 config:git config --global -l
- 系统级 config:git config --system -l
- 当前生效的配置:git config -l
编辑配置文件
命令参数:–edit
简写:-e
格式:git config [–local | --global | --system] -e
- 仓库级 config:git config --local -e
- 全局级 config:git config --global -e
- 系统级 config:git config --system -e
- 默认编辑仓库级 config:git config -e
使用 :q 来退出编辑器
注:执行这个命令的时候,Git 会用配置文件中设定的编辑器打开配置文件
增加配置项
命令参数:–add
格式:git config [–local | --global | --system] --add section.key value
- 默认是添加在 local 配置文件,且 section,key,value 一项都不能少
例如我们执行命令:git config --global --add idol.name Liyuu 就能够给全局级配置文件中添加配置项
获取配置项
命令参数:–get
格式:git config [–local | --global | --system] --get section.key
-
默认是获取 local 配置文件里的配置项
-
直接使用 git config section.key 省略 --get 也可以
-
如果获取一个 section 不存在的 key,不会返回任何值
-
如果获取一个不存在的 section 的 key,会报错
删除配置项
命令参数:–unset
格式:git config [–local | --global | --system] --unset section.key