git config 命令
简介
git config 是 Git 中用于配置 Git 的命令。它可以设置用户信息、仓库设置、别名等。
基本概念
配置级别
- 系统级别(–system):
/etc/gitconfig,影响所有用户
- 全局级别(–global):
~/.gitconfig,影响当前用户
- 仓库级别(–local):
.git/config,只影响当前仓库
优先级:仓库 > 全局 > 系统
命令语法
1 2 3 4 5 6 7 8 9 10 11
| git config [--global|--system|--local] --list
git config [--global|--system|--local] <key> <value>
git config [--global|--system|--local] <key>
git config [--global|--system|--local] --unset <key>
|
查看帮助文档
常用选项
| 选项 |
说明 |
--global |
全局配置(用户级别) |
--system |
系统配置(所有用户) |
--local |
仓库配置(默认) |
--list, -l |
列出所有配置 |
--get |
获取配置值 |
--unset |
删除配置项 |
--unset-all |
删除所有匹配的配置项 |
--add |
添加配置项 |
--get-regexp |
使用正则表达式获取配置 |
--edit |
编辑配置文件 |
--file <file> |
指定配置文件 |
基本使用
1. 查看配置
1 2 3 4 5 6 7 8 9 10 11
| $ git config --list
$ git config --global --list
$ git config --local --list
$ git config --system --list
|
2. 设置用户信息
1 2 3 4 5 6 7 8 9
| $ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"
$ git config user.name "Project Name" $ git config user.email "project@example.com"
|
3. 获取配置值
1 2 3 4 5 6 7 8
| $ git config user.name
$ git config user.email
$ git config --global user.name
|
4. 删除配置
1 2 3 4 5
| $ git config --global --unset user.name
$ git config --global --unset-all alias.co
|
5. 编辑配置文件
1 2 3 4 5
| $ git config --global --edit
$ git config --local --edit
|
常用配置
1. 用户信息
1 2 3
| $ git config --global user.name "Your Name" $ git config --global user.email "your.email@example.com"
|
2. 编辑器配置
1 2 3 4
| $ git config --global core.editor "vim" $ git config --global core.editor "code --wait" $ git config --global core.editor "nano"
|
3. 默认分支名
1 2
| $ git config --global init.defaultBranch main
|
4. 合并工具
1 2 3
| $ git config --global merge.tool vimdiff $ git config --global merge.tool "code --wait"
|
5. 别名配置
1 2 3 4 5 6 7 8
| $ git config --global alias.st status $ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global alias.ci commit $ git config --global alias.unstage "reset HEAD --" $ git config --global alias.last "log -1 HEAD" $ git config --global alias.visual "!gitk"
|
6. 颜色配置
1 2 3 4 5 6 7
| $ git config --global color.ui auto
$ git config --global color.branch auto $ git config --global color.diff auto $ git config --global color.status auto
|
7. 换行符配置
1 2 3 4 5 6 7 8
| $ git config --global core.autocrlf true
$ git config --global core.autocrlf input
$ git config --global core.autocrlf false
|
8. 推送配置
1 2 3 4 5
| $ git config --global push.default simple
$ git config --global pull.rebase true
|
9. 凭据存储
1 2 3 4 5 6 7 8 9
| $ git config --global credential.helper store
$ git config --global credential.helper cache
$ git config --global credential.helper manager-core $ git config --global credential.helper osxkeychain
|
实际应用场景
场景1:首次配置 Git
1 2 3 4 5 6 7 8 9
| $ git config --global user.name "Your Name" $ git config --global user.email "your.email@example.com"
$ git config --global core.editor "vim"
$ git config --global color.ui auto
|
场景2:创建常用别名
1 2 3 4 5 6 7 8 9
| $ git config --global alias.st status $ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global alias.ci commit
$ git st $ git co main
|
场景3:项目特定配置
1 2 3 4
| $ cd project $ git config user.name "Project Name" $ git config user.email "project@example.com"
|
场景4:配置合并工具
1 2 3
| $ git config --global merge.tool vscode $ git config --global mergetool.vscode.cmd "code --wait \$MERGED"
|
场景5:配置凭据存储
1 2 3 4
| $ git config --global credential.helper store
|
配置文件位置
全局配置
1 2 3 4 5
| $ cat ~/.gitconfig
$ git config --global --list --show-origin
|
仓库配置
1 2 3 4 5
| $ cat .git/config
$ git config --local --list --show-origin
|
系统配置
1 2 3 4 5
| /etc/gitconfig
$ git config --system --list
|
常见问题和解决方案
问题1:配置不生效
解决方案:
1 2 3 4 5
| $ git config --list --show-origin
$ git config user.name
|
问题2:想删除配置
解决方案:
1 2 3 4 5
| $ git config --global --unset user.name
$ git config --global --unset-all alias.st
|
问题3:配置了错误的编辑器
解决方案:
1 2 3 4 5 6
| $ git config --global core.editor "vim"
$ git config --global --unset core.editor $ git config --global core.editor "vim"
|
最佳实践
1. 首次使用 Git 时配置
1 2 3 4 5
| $ git config --global user.name "Your Name" $ git config --global user.email "your.email@example.com" $ git config --global core.editor "vim" $ git config --global color.ui auto
|
2. 使用别名简化命令
1 2 3 4
| $ git config --global alias.st status $ git config --global alias.co checkout $ git config --global alias.br branch
|
3. 项目特定配置
1 2 3
| $ git config user.name "Project Name" $ git config user.email "project@example.com"
|
4. 定期检查配置
1 2 3 4 5
| $ git config --list
$ git config --list --show-origin
|
总结
git config 是配置 Git 的核心命令:
- 查看配置:
git config --list 查看所有配置
- 设置配置:
git config --global <key> <value> 设置配置
- 获取配置:
git config <key> 获取配置值
- 删除配置:
git config --unset <key> 删除配置
关键要点:
- ✅ 首次使用 Git 时配置用户信息
- ✅ 使用别名简化常用命令
- ✅ 了解配置级别和优先级
- ✅ 为特定项目设置不同的配置