Git 用户信息配置
Git 提交需要记录作者信息,安装后第一步就是配置用户名和邮箱。
配置用户名和邮箱
Bash
# 配置用户名
git config --global user.name "Your Name"
# 配置邮箱
git config --global user.email "your.email@example.com"
--global表示全局配置,对所有仓库生效。不加参数则配置当前仓库。
查看配置
Bash
# 查看用户名
git config user.name
# 查看邮箱
git config user.email
# 查看所有配置
git config --list
配置级别
| 级别 | 参数 | 作用范围 | 配置文件 |
|---|---|---|---|
| 系统 | --system | 所有用户 | /etc/gitconfig |
| 全局 | --global | 当前用户 | ~/.gitconfig |
| 本地 | 无或--local | 当前仓库 | .git/config |
常用配置项
Bash
# 设置默认分支名为 main
git config --global init.defaultBranch main
# 设置默认编辑器
git config --global core.editor "code --wait"
# 设置别名
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 color.ui auto
修改和删除配置
Bash
# 修改配置(直接覆盖)
git config --global user.name "New Name"
# 删除配置
git config --global --unset user.name
用户名和邮箱会出现在每次提交记录中,建议使用真实信息便于识别。
要点总结
- 使用
git config配置用户信息 --global全局配置,对当前用户所有仓库生效- 配置级别优先级:本地 > 全局 > 系统
- 可配置别名、编辑器、默认分支等选项
- 使用
--unset删除配置项
📝 发现内容有误?点击此处直接编辑