正确命令为:全局设置用 git config --global user.name "Your Name" 和 git config --global user.email "you@example.com";当前仓库设置省略 --global。

git config 设置用户名和邮箱的正确命令
Git 提交时必须有 user.name 和 user.email,否则会报错:commit is not possible because you have no identity。设置分全局(所有仓库)和局部(当前仓库),优先级:本地 > 全局。
- 设全局(推荐新手起步用):
git config --global user.name "Your Name"和git config --global user.email "you@example.com" - 设当前项目(比如公司项目要用企业邮箱):
git config user.name "Your Work Name"(不带--global) - 检查是否生效:
git config user.name(查当前仓库)、git config --global user.email(查全局)
为什么提交记录里显示的不是你刚设的邮箱?
Git 不校验邮箱真实性,也不联网验证,它只按配置值原样写进 commit 对象。但 GitHub/GitLab 等平台会比对 commit 中的 user.email 和账户绑定邮箱——不一致就显示为 “ghost” 或 “unverified”。常见原因:
- 设置了全局邮箱,但忘了在某个 repo 里覆盖为工作邮箱(比如用个人邮箱提交了公司代码)
- 复制粘贴时多空格或中英文引号,导致实际存入的是
" your@work.com "(前后空格也会不匹配) - 用了
git commit --author="Name <wrong>"</wrong>覆盖了配置,该次 commit 就按手动指定的来
Windows 上 Git Bash 和 Windows Terminal 配置不一致?
Git for Windows 安装时默认勾选 “Use the OpenSSL library”,这会影响 credential helper 行为,但不影响 user.name 读取;真正容易出问题的是路径和 shell 环境差异:
- Git Bash 读
~/.gitconfig(即C:\Users\XXX\.gitconfig) - PowerShell 或 CMD 下运行 git,也可能读同一文件,但若设置了
GIT_CONFIG_GLOBAL环境变量,就会优先读那个路径 - 用 VS Code 内置终端提交时,如果它启动的是 PowerShell,而你只在 Git Bash 里配过,就可能读不到——统一用
git config --list --show-origin查来源最稳
改完配置后旧 commit 的作者信息能更新吗?
不能自动更新。Git 的 commit 是不可变对象,修改 author 需要重写历史。仅建议在未推送到远程前操作:
- 改最新一次 commit:
git commit --amend --author="New Name <new>" --no-edit</new> - 改最近 3 次:
git rebase -i HEAD~3,把要改的行前面改成edit,保存后对每条执行git commit --amend --author="..." --no-edit,再git rebase --continue - 已推送到远程?
git push --force-with-lease可覆盖,但团队协作中需提前沟通——别人基于旧 commit 继续开发的话,强制推送会导致他们 merge 冲突甚至丢提交
别指望靠改 .gitconfig 让历史 commit 自动“刷新”,那只是给未来 commit 用的。










