深入理解 Git 的系统、全局、项目三级配置体系,掌握优先级规则与常用配置项,打造高效的 Git 开发环境。
Git 配置 — 让 Git 按你的方式工作
Git 的配置系统分为三个层级,理解它们的优先级和作用范围,才能正确定制你的 Git 行为。很多人遇到问题就是因为不清楚配置写在了哪里。
一、
三级配置体系
Git 的配置从大到小分为三层,每层有不同的配置文件和作用范围:
1 | |
1.1 配置文件位置
| 配置类型 | 文件路径 |
|---|---|
| 系统配置 | C:\Program Files\Git\etc\gitconfig |
| 全局配置 | C:\Users\你的用户名\.gitconfig |
| 项目配置 | 你的项目/.git/config |
| 配置类型 | 文件路径 |
|---|---|
| 系统配置 | /usr/local/etc/gitconfig |
| 全局配置 | ~/.gitconfig |
| 项目配置 | 你的项目/.git/config |
1.2 优先级规则
优先级从高到低:
1 | |
同一个配置项如果在多层都有定义,越具体的层越优先。
二、
查看配置
2.1 查看所有配置(含来源)
1 | |
输出示例:
1 | |
看输出路径判断配置类型:
Program Files/Git/etc/gitconfig→ 系统配置Users/用户名/.gitconfig→ 全局配置.git/config→ 项目配置
2.2 分层级查看
1 | |
2.3 查看单个配置项
1 | |
三、
系统配置详解
系统配置位于 Git 安装目录,影响本机上所有用户的所有仓库。一般在安装 Git 时自动生成,日常开发中很少修改。
系统配置项一览表
| 配置项 | 含义 | 推荐值 | 配置命令 |
|---|---|---|---|
diff.astextplain.textconv | 将二进制文件当作文本比较差异 | astextplain | git config --system diff.astextplain.textconv astextplain |
filter.lfs.clean | Git LFS 清理过滤器 | Git LFS 命令 | git config --system filter.lfs.clean "git-lfs clean -- %f" |
filter.lfs.smudge | Git LFS 检出过滤器 | Git LFS 命令 | git config --system filter.lfs.smudge "git-lfs smudge -- %f" |
filter.lfs.process | LFS 过滤处理命令 | Git LFS 命令 | git config --system filter.lfs.process "git-lfs filter-process" |
filter.lfs.required | 要求必须使用 Git LFS | true/false | git config --system filter.lfs.required true |
http.sslbackend | SSL 加密后端 | openssl/schannel | git config --system http.sslbackend openssl |
http.sslcainfo | SSL 证书文件路径 | 文件路径 | git config --system http.sslcainfo "路径" |
core.autocrlf | 自动换行符转换 | true/false/input | git config --system core.autocrlf true |
core.fscache | 文件系统缓存 | true/false | git config --system core.fscache true |
core.symlinks | 是否支持符号链接 | true/false | git config --system core.symlinks false |
pull.rebase | pull 默认用 merge 还是 rebase | true/false | git config --system pull.rebase false |
credential.helper | 凭据管理器 | manager/store/cache | git config --system credential.helper manager |
init.defaultbranch | 默认分支名 | 分支名 | git config --system init.defaultbranch master |
系统配置修改需谨慎:大部分系统配置由 Git 安装程序设定(如
core.autocrlf、credential.helper),除非你清楚影响范围,否则不建议手动修改。
四、
全局配置详解
全局配置是最常用的配置层级,影响当前用户的所有仓库。你的用户名、邮箱、默认分支名等都在这里设置。
4.1 必须设置的全局配置
1 | |
user.name 和 user.email 是 Git 提交的身份标识。如果没设置,git commit 会直接报错拒绝提交。GitHub/Gitee 等平台也是靠邮箱关联你的账号。
4.2 全局配置项一览表
| 配置项 | 含义 | 推荐值 | 配置命令 |
|---|---|---|---|
user.name | 提交者姓名 | 你的名字 | git config --global user.name "你的名字" |
user.email | 提交者邮箱 | 你的邮箱 | git config --global user.email "your@email.com" |
init.defaultBranch | 默认分支名 | main | git config --global init.defaultBranch main |
pull.rebase | pull 用 rebase 代替 merge | true | git config --global pull.rebase true |
rebase.autoStash | rebase 时自动暂存未提交的修改 | true | git config --global rebase.autoStash true |
core.autocrlf | 换行符自动转换 | Windows: true其他: input | git config --global core.autocrlf true |
safe.directory | 信任的目录(解决权限报错) | * 或路径 | git config --global safe.directory "*" |
credential.helper | 凭据管理器 | manager/store | git config --global credential.helper manager |
4.3 常用全局配置详解
pull.rebase = true(强烈推荐)
默认情况下 git pull 等同于 git fetch + git merge,当远程有新提交时会产生一个合并提交:
1 | |
开启 rebase 后,你的提交会被”接”在远程提交的后面,历史更干净:
1 | |
1 | |
rebase.autoStash = true
当你有未提交的修改时执行 rebase,Git 会报错。开启 autoStash 后,Git 会自动暂存(stash)你的修改,rebase 完成后再恢复:
1 | |
core.autocrlf(换行符)
不同操作系统的换行符不同:Windows 用 CRLF(\r\n),Linux/macOS 用 LF(\n)。
| 操作系统 | 推荐值 | 行为 |
|---|---|---|
| Windows | true | 提交时转 LF,检出时转 CRLF |
| macOS/Linux | input | 提交时转 LF,检出时不转换 |
1 | |
五、
项目配置详解
项目配置只影响当前仓库,适合在团队协作中统一某些设置,或为特定项目覆盖全局配置。
项目配置项一览表
| 配置项 | 含义 | 可修改 | 说明 |
|---|---|---|---|
core.repositoryformatversion | 仓库格式版本 | 只读 | 自动生成,不可修改 |
core.filemode | 是否跟踪文件权限变更 | 可配置 | Windows 下建议 false |
core.bare | 是否为裸仓库 | 只读 | 创建仓库时决定 |
core.logallrefupdates | 记录所有引用更新 | 可配置 | 默认 true |
core.symlinks | 是否支持符号链接 | 可配置 | 默认根据系统决定 |
core.ignorecase | 是否忽略文件名大小写 | 可配置 | Windows/macOS 默认 true |
项目配置的典型用法
1 | |
六、
推荐配置一键脚本
以下是推荐的 Git 全局配置,复制执行即可:
1 | |
验证配置:设置完成后,用以下命令确认:
1 | |
七、
凭据管理(credential.helper)
当你用 HTTPS 方式克隆私有仓库时,Git 会要求输入用户名和密码。配置 credential.helper 后,Git 会记住你的凭据,下次就不用再输入了。
7.1 凭据存储方式对比
| 类型 | 配置值 | 存储位置 | 安全性 | 适用平台 |
|---|---|---|---|---|
| Git Credential Manager | manager | 系统凭据管理器 | 高 | Windows/macOS/Linux |
| GCM Core(旧版) | manager-core | 系统凭据管理器 | 高 | Windows |
| 明文文件 | store | ~/.git-credentials 文件 | 低(明文) | 全平台 |
| 内存缓存 | cache | 内存中(超时清除) | 中 | macOS/Linux |
| Windows 凭据 | wincred | Windows 凭据管理器 | 高 | Windows(旧版) |
| macOS 钥匙串 | osxkeychain | macOS Keychain | 高 | macOS |
1 | |
不要用
store 保存敏感账号:store 模式会把密码以明文写在 ~/.git-credentials 文件中,任何能访问你电脑的人都能看到。推荐使用 manager。
7.2 查看当前凭据配置
1 | |
7.3 Windows 查看已保存的 Git 凭据
如果你用的是 credential.helper=manager,凭据保存在 Windows 凭据管理器中:
方法 1:命令行查看
1 | |
输出示例:
1 | |
方法 2:图形界面查看
打开路径:控制面板 → 凭据管理器 → Windows 凭据
你会看到以 git:https://gitee.com 或 git:https://github.com 开头的条目。
方法 3:通过 Git 命令测试
1 | |
7.4 修改或删除凭据
删除 Windows 凭据
1 | |
重新添加凭据
删除凭据后,下次 git pull 或 git push 时会重新提示输入用户名和密码,输入后会自动保存。
修改 store 模式的凭据
如果用的是 credential.helper=store,直接编辑文件:
1 | |
文件格式为:https://用户名:密码@主机地址
7.5 HTTP vs SSH 认证方式
| 特性 | HTTPS 方式 | SSH 方式 |
|---|---|---|
| 仓库地址 | https://gitee.com/user/repo.git | git@gitee.com:user/repo.git |
| 认证方式 | 用户名 + 密码/Token | SSH 密钥对 |
| 凭据管理 | credential.helper | ~/.ssh/ 目录下的密钥 |
| 首次使用 | 需要输入账号密码 | 需要生成并配置 SSH Key |
| 适合场景 | 临时使用、公司内网 | 长期开发推荐 |
如果你经常推送代码,建议配置 SSH 方式。一次配置,永久免密。搜索”Git SSH 密钥配置”可以找到详细教程。
八、
删除配置
如果设错了配置,可以用 --unset 删除:
1 | |
总结:配置速查卡
1 | |