git config的范围参数: local, global, system
1. --local
: 作用于当前仓库
--local
: 这个参数的作用范围是当前仓库。其配置的参数保存在当前 git 仓库下的 .git/config
中,只对当前仓库有效。
示例指令:
git config --local user.name "myNameLocal"
git config --local user.email "1234@xxx.com"
执行完上述指令之后,文件.git/config
中会增加如下两行内容:
2. --global
: 作用于当前用户的全局范围的 Git 仓库
--global
: 作用于当前用户的全局范围的 Git 仓库。其配置的参数保存在当前用户下的 ~/.gitconfig
中,只对当前用户有效。
示例指令:
git config --local user.name "myNameGlobal"
git config --local user.email "1234123@xxx.com"
执行完上述指令之后,文件~/.gitconfig
中会增加如下两行内容:
3. --system
: 作用于系统范围内的 Git 仓库
--system
: 作用于当前系统范围的 Git 仓库。其配置的参数保存在系统下的 /etc/gitconfig
中,对当前系统中的所有用户有效。注意,修改系统配置通常需要sudo
权限。
示例指令:
sudo git config --system user.name "myNameSystem"
sudo git config --system user.email "123412312@xxx.com"
执行完上述指令之后,文件/etc/gitconfig
中会增加如下两行内容:
4. 注意事项
- 这三种配置只配置其中一个就可以正常使用
git
。笔者一般使用--global
参数对git
进行配置。 - 可以通过
git config --list
指令进行查看当前的git
配置.
比如可以使用下列指令查看--local
配置的参数:
git config --list --local
终端输出结果:
- 当存在多个配置时或者当这个三种配置都已经进行配置了,git 对于这三者有如下的优先级:
local > global > system
, 也就是 配置到当前仓库--local
的参数优先级最高。
参考链接
[1] 极客教程. Git 修改git config [EB/OL]. https://geek-docs.com/git/git-questions/531_git_resetting_git_config.html, xxxx-xx-xx/2025-08-12.