Appearance
初始配置
安装完 Git 后,第一件事是配置你的用户信息。这些信息会包含在每次提交中,是团队协作的重要标识。
用户信息配置
配置用户名和邮箱
bash
# 配置全局用户名(所有仓库使用)
git config --global user.name "Zhang San"
# 配置全局邮箱
git config --global user.email "zhangsan@example.com"重要:邮箱应与 GitHub/GitLab 账户邮箱一致,这样提交才能关联到你的账户头像和贡献图。
为特定仓库配置不同用户信息
工作项目和个人项目可能需要不同的身份:
bash
# 进入项目目录
cd /path/to/work-project
# 配置仅在此仓库生效的用户信息
git config user.name "Zhang San(工作)"
git config user.email "zhangsan@company.com"配置层级:system / global / local
Git 配置有三个层级,优先级从低到高:
| 层级 | 作用范围 | 文件位置 | 命令参数 |
|---|---|---|---|
| system | 所有用户、所有仓库 | /etc/gitconfig | --system |
| global | 当前用户、所有仓库 | ~/.gitconfig | --global |
| local | 当前仓库 | .git/config | --local(默认) |
bash
# 查看 system 级配置
git config --system --list
# 查看 global 级配置
git config --global --list
# 查看 local 级配置(需在仓库内执行)
git config --local --list
# 查看最终生效的所有配置(合并后)
git config --list优先级:local > global > system
当同一配置在多个层级都有定义时,优先级高的生效。
默认编辑器设置
Git 在需要输入文本时(如提交信息、rebase 操作)会打开编辑器。
bash
# 设置为 VS Code
git config --global core.editor "code --wait"
# 设置为 Vim
git config --global core.editor "vim"
# 设置为 nano(更友好的终端编辑器)
git config --global core.editor "nano"
# 设置为 Emacs
git config --global core.editor "emacs"
# Windows 设置为 Notepad++
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"VS Code 用户注意:
--wait参数让 Git 等待你关闭 VS Code 标签页后再继续。
默认分支名设置
从 Git 2.28 开始,可以设置新仓库的默认分支名(取代 master):
bash
# 设置默认分支名为 main
git config --global init.defaultBranch main主流平台(GitHub、GitLab)已将默认分支从 master 改为 main。建议统一设置为 main。
换行符处理(core.autocrlf)
不同操作系统使用不同的换行符:
- Windows:CRLF(
\r\n) - Linux/macOS:LF(
\n)
混用会导致 git diff 显示大量无意义的换行符差异。
bash
# Windows 开发者(推荐)
# 检出时转换为 CRLF,提交时转换为 LF
git config --global core.autocrlf true
# Linux/macOS 开发者
# 检出不转换,提交时将 CRLF 转换为 LF(防止误提交 CRLF)
git config --global core.autocrlf input
# 不做任何转换(不推荐,容易产生问题)
git config --global core.autocrlf false使用 .gitattributes 更精确控制
更推荐在项目中使用 .gitattributes 文件,不依赖个人配置:
bash
# .gitattributes 内容示例
* text=auto # 文本文件自动处理
*.sh text eol=lf # Shell 脚本强制 LF
*.bat text eol=crlf # 批处理文件强制 CRLF
*.png binary # 二进制文件不处理查看所有配置及来源
bash
# 查看所有生效配置,并显示来自哪个配置文件
git config --list --show-origin
# 示例输出:
# file:/etc/gitconfig core.symlinks=false
# file:~/.gitconfig user.name=Zhang San
# file:~/.gitconfig user.email=zhangsan@example.com
# file:.git/config core.repositoryformatversion=0bash
# 查看某个具体配置的值及来源
git config --show-origin user.email
# file:~/.gitconfig zhangsan@example.com
# 只查看某个配置的值
git config user.name
# Zhang San其他常用配置
配置颜色输出
bash
# 开启彩色输出(通常默认开启)
git config --global color.ui auto配置 diff 工具
bash
# 设置 diff 工具
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'配置 merge 工具
bash
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'配置推送行为
bash
# Git 2.0+ 推荐:只推送当前分支
git config --global push.default current
# 或者:推送到同名的远程分支
git config --global push.default simple配置拉取行为
bash
# 拉取时使用 rebase 而不是 merge(保持线性历史)
git config --global pull.rebase true开启大文件系统支持提示
bash
git config --global advice.detachedHead false # 关闭分离 HEAD 警告完整的推荐 global 配置
将以下内容保存到 ~/.gitconfig:
ini
[user]
name = Zhang San
email = zhangsan@example.com
[core]
editor = code --wait
autocrlf = input # Linux/macOS 使用 input,Windows 使用 true
pager = less -FX
[init]
defaultBranch = main
[push]
default = current
[pull]
rebase = true
[color]
ui = auto
[diff]
tool = vscode
colorMoved = default
[merge]
tool = vscode
conflictstyle = diff3
[alias]
st = status
co = checkout
br = branch
lg = log --oneline --graph --all --decorate配置文件直接编辑
bash
# 用编辑器直接打开配置文件
git config --global --edit
# 或直接编辑文件
code ~/.gitconfig删除配置
bash
# 删除某个配置项
git config --global --unset user.email
# 删除某个配置段
git config --global --remove-section alias总结
| 操作 | 命令 |
|---|---|
| 设置用户名 | git config --global user.name "名字" |
| 设置邮箱 | git config --global user.email "邮箱" |
| 设置编辑器 | git config --global core.editor "编辑器" |
| 查看所有配置 | git config --list --show-origin |
| 编辑配置文件 | git config --global --edit |
正确配置是使用 Git 的第一步,特别是用户信息配置,将永久关联到你的每次提交中。