深入理解 Git 的系统、全局、项目三级配置体系,掌握优先级规则与常用配置项,打造高效的 Git 开发环境。

Git 配置 — 让 Git 按你的方式工作

Git 的配置系统分为三个层级,理解它们的优先级和作用范围,才能正确定制你的 Git 行为。很多人遇到问题就是因为不清楚配置写在了哪里。


一、三级配置体系

Git 的配置从大到小分为三层,每层有不同的配置文件和作用范围:

1
2
3
4
5
系统配置 (system)    →  Git 安装目录下     →  影响所有用户
↓ 被覆盖
全局配置 (global) → 用户主目录下 → 影响当前用户的所有仓库
↓ 被覆盖
项目配置 (local) → 项目 .git 目录下 → 仅影响当前项目

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
git config --list --show-origin

输出示例:

1
2
3
4
5
file:C:/Program Files/Git/etc/gitconfig    diff.astextplain.textconv=astextplain
file:C:/Program Files/Git/etc/gitconfig core.autocrlf=true
file:C:/Users/John/.gitconfig user.name=John
file:C:/Users/John/.gitconfig user.email=john@example.com
file:.git/config core.repositoryformatversion=0

看输出路径判断配置类型

  • Program Files/Git/etc/gitconfig系统配置
  • Users/用户名/.gitconfig全局配置
  • .git/config项目配置

2.2 分层级查看

1
2
3
4
5
6
7
8
# 只看系统配置
git config --system --list

# 只看全局配置
git config --global --list

# 只看项目配置
git config --local --list

2.3 查看单个配置项

1
2
3
4
5
# 查看用户名
git config user.name

# 查看指定层级的配置
git config --global user.name

三、系统配置详解

系统配置位于 Git 安装目录,影响本机上所有用户的所有仓库。一般在安装 Git 时自动生成,日常开发中很少修改。

系统配置项一览表

配置项含义推荐值配置命令
diff.astextplain.textconv将二进制文件当作文本比较差异astextplaingit config --system diff.astextplain.textconv astextplain
filter.lfs.cleanGit LFS 清理过滤器Git LFS 命令git config --system filter.lfs.clean "git-lfs clean -- %f"
filter.lfs.smudgeGit LFS 检出过滤器Git LFS 命令git config --system filter.lfs.smudge "git-lfs smudge -- %f"
filter.lfs.processLFS 过滤处理命令Git LFS 命令git config --system filter.lfs.process "git-lfs filter-process"
filter.lfs.required要求必须使用 Git LFStrue/falsegit config --system filter.lfs.required true
http.sslbackendSSL 加密后端openssl/schannelgit config --system http.sslbackend openssl
http.sslcainfoSSL 证书文件路径文件路径git config --system http.sslcainfo "路径"
core.autocrlf自动换行符转换true/false/inputgit config --system core.autocrlf true
core.fscache文件系统缓存true/falsegit config --system core.fscache true
core.symlinks是否支持符号链接true/falsegit config --system core.symlinks false
pull.rebasepull 默认用 merge 还是 rebasetrue/falsegit config --system pull.rebase false
credential.helper凭据管理器manager/store/cachegit config --system credential.helper manager
init.defaultbranch默认分支名分支名git config --system init.defaultbranch master

系统配置修改需谨慎:大部分系统配置由 Git 安装程序设定(如 core.autocrlfcredential.helper),除非你清楚影响范围,否则不建议手动修改。


四、全局配置详解

全局配置是最常用的配置层级,影响当前用户的所有仓库。你的用户名、邮箱、默认分支名等都在这里设置。

4.1 必须设置的全局配置

1
2
3
4
5
# 设置用户名(出现在每次提交记录中)
git config --global user.name "你的名字"

# 设置邮箱(出现在每次提交记录中)
git config --global user.email "your@email.com"
重要

user.nameuser.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默认分支名maingit config --global init.defaultBranch main
pull.rebasepull 用 rebase 代替 mergetruegit config --global pull.rebase true
rebase.autoStashrebase 时自动暂存未提交的修改truegit 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/storegit config --global credential.helper manager

4.3 常用全局配置详解

pull.rebase = true(强烈推荐)

默认情况下 git pull 等同于 git fetch + git merge,当远程有新提交时会产生一个合并提交:

1
2
3
4
5
# 默认行为(pull.rebase = false)
* a1b2c3d (HEAD) Merge remote-tracking branch 'origin/main' ← 多余的合并提交
|\
| * d4e5f6g (origin/main) 别人的提交
* | h7i8j9k (main) 你的提交

开启 rebase 后,你的提交会被”接”在远程提交的后面,历史更干净:

1
2
3
4
# 开启后(pull.rebase = true)
* a1b2c3d (HEAD -> main) 你的提交 ← 干净!
* d4e5f6g 别人的提交
* h7i8j9k 之前的提交
1
git config --global pull.rebase true

rebase.autoStash = true

当你有未提交的修改时执行 rebase,Git 会报错。开启 autoStash 后,Git 会自动暂存(stash)你的修改,rebase 完成后再恢复:

1
git config --global rebase.autoStash true

core.autocrlf(换行符)

不同操作系统的换行符不同:Windows 用 CRLF\r\n),Linux/macOS 用 LF\n)。

操作系统推荐值行为
Windowstrue提交时转 LF,检出时转 CRLF
macOS/Linuxinput提交时转 LF,检出时不转换
1
2
3
4
5
# Windows
git config --global core.autocrlf true

# macOS / Linux
git config --global core.autocrlf input

五、项目配置详解

项目配置只影响当前仓库,适合在团队协作中统一某些设置,或为特定项目覆盖全局配置。

项目配置项一览表

配置项含义可修改说明
core.repositoryformatversion仓库格式版本只读自动生成,不可修改
core.filemode是否跟踪文件权限变更可配置Windows 下建议 false
core.bare是否为裸仓库只读创建仓库时决定
core.logallrefupdates记录所有引用更新可配置默认 true
core.symlinks是否支持符号链接可配置默认根据系统决定
core.ignorecase是否忽略文件名大小写可配置Windows/macOS 默认 true

项目配置的典型用法

1
2
3
4
5
6
7
8
9
# 为某个项目单独设置用户名(比如公司项目用公司邮箱)
git config --local user.name "John Doe"
git config --local user.email "john@company.com"

# 忽略大小写(Windows 常用)
git config --local core.ignorecase true

# 关闭文件权限跟踪(Windows 常用)
git config --local core.filemode false

六、推荐配置一键脚本

以下是推荐的 Git 全局配置,复制执行即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 基础身份
git config --global user.name "你的名字"
git config --global user.email "your@email.com"

# 默认分支
git config --global init.defaultBranch main

# 拉取策略(避免多余的 merge 提交)
git config --global pull.rebase true

# 自动暂存(rebase 时不怕有未提交的修改)
git config --global rebase.autoStash true

# 换行符(Windows 用 true,macOS/Linux 用 input)
git config --global core.autocrlf true

验证配置:设置完成后,用以下命令确认:

1
git config --global --list

七、凭据管理(credential.helper)

当你用 HTTPS 方式克隆私有仓库时,Git 会要求输入用户名和密码。配置 credential.helper 后,Git 会记住你的凭据,下次就不用再输入了。

7.1 凭据存储方式对比

类型配置值存储位置安全性适用平台
Git Credential Managermanager系统凭据管理器Windows/macOS/Linux
GCM Core(旧版)manager-core系统凭据管理器Windows
明文文件store~/.git-credentials 文件(明文)全平台
内存缓存cache内存中(超时清除)macOS/Linux
Windows 凭据wincredWindows 凭据管理器Windows(旧版)
macOS 钥匙串osxkeychainmacOS KeychainmacOS
1
2
3
4
5
# 推荐:使用系统凭据管理器(安全 + 持久)
git config --global credential.helper manager

# 不推荐:明文保存密码(方便但不安全)
git config --global credential.helper store

不要用 store 保存敏感账号store 模式会把密码以明文写在 ~/.git-credentials 文件中,任何能访问你电脑的人都能看到。推荐使用 manager

7.2 查看当前凭据配置

1
2
3
# 查看 credential.helper 配置
git config --global credential.helper
# 输出:manager

7.3 Windows 查看已保存的 Git 凭据

如果你用的是 credential.helper=manager,凭据保存在 Windows 凭据管理器中:

方法 1:命令行查看

1
2
# 列出 Windows 凭据管理器中所有凭据
cmdkey /list

输出示例:

1
2
3
目标: LegacyGeneric:target=git:https://gitee.com
类型: 通用
用户名: your_username

方法 2:图形界面查看

打开路径:控制面板凭据管理器Windows 凭据

你会看到以 git:https://gitee.comgit:https://github.com 开头的条目。

方法 3:通过 Git 命令测试

1
2
# 输入后会显示当前使用的凭据
echo protocol=https\nhost=gitee.com | git credential fill

7.4 修改或删除凭据

删除 Windows 凭据

1
2
3
4
5
# 删除 Gitee 的凭据
cmdkey /delete:LegacyGeneric:target=git:https://gitee.com

# 删除 GitHub 的凭据
cmdkey /delete:LegacyGeneric:target=git:https://github.com

重新添加凭据

删除凭据后,下次 git pullgit push 时会重新提示输入用户名和密码,输入后会自动保存。

修改 store 模式的凭据

如果用的是 credential.helper=store,直接编辑文件:

1
2
3
4
5
# Linux/macOS
vim ~/.git-credentials

# Windows(文件位于用户目录)
# C:\Users\你的用户名\.git-credentials

文件格式为:https://用户名:密码@主机地址

7.5 HTTP vs SSH 认证方式

特性HTTPS 方式SSH 方式
仓库地址https://gitee.com/user/repo.gitgit@gitee.com:user/repo.git
认证方式用户名 + 密码/TokenSSH 密钥对
凭据管理credential.helper~/.ssh/ 目录下的密钥
首次使用需要输入账号密码需要生成并配置 SSH Key
适合场景临时使用、公司内网长期开发推荐
推荐

如果你经常推送代码,建议配置 SSH 方式。一次配置,永久免密。搜索”Git SSH 密钥配置”可以找到详细教程。


八、删除配置

如果设错了配置,可以用 --unset 删除:

1
2
3
4
5
# 删除全局的用户名配置
git config --global --unset user.name

# 删除项目的某个配置
git config --local --unset core.filemode

总结:配置速查卡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
┌─────────────────────────────────────────────────────────┐
│ Git 配置速查卡 │
├──────────────┬──────────────────────────────────────────┤
│ 查看配置 │ git config --list --show-origin │
│ 查看全局 │ git config --global --list │
│ 查看项目 │ git config --local --list │
├──────────────┼──────────────────────────────────────────┤
│ 设置用户名 │ git config --global user.name "名字" │
│ 设置邮箱 │ git config --global user.email "邮箱" │
│ 默认分支 │ git config --global init.defaultBranch main │
│ 拉取用 rebase │ git config --global pull.rebase true │
│ 自动暂存 │ git config --global rebase.autoStash true │
│ 换行符 Win │ git config --global core.autocrlf true │
├──────────────┼──────────────────────────────────────────┤
│ 删除配置 │ git config --global --unset 配置项 │
└──────────────┴──────────────────────────────────────────┘