解决 Git 报错 fatal: unable to auto-detect email address 的排查指南
排查与解决 Git 初次提交代码提示 unable to auto-detect email address 用户名邮箱缺失的完整方案。
问题现象
在新建的服务器环境或新初始化的 Git 仓库中执行 git commit 时,操作被终止并输出:
Author identity unknown
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'ubuntu@ip-10-0-1-20.(none)')
提交未生成,暂存区变更未固化。
触发根因
- Git 架构规定:每一个 Commit 对象都必须永久包含作者名称(Author Name)和作者电子邮箱(Author Email)作为审计元数据。
- 本地系统从未配置过
user.name和user.email,且系统的全限定域名(FQDN)无法自动推导出合法的默认邮箱。
解决步骤与操作命令
步骤一:配置全局用户身份信息(推荐)
在终端中设置全局身份信息(对该操作系统用户下的所有 Git 仓库通用):
git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com"
步骤二:针对特定工作仓库配置局部独立身份(可选)
若某仓库为公司内部项目,需要使用企业邮箱而非个人邮箱,进入该仓库目录后执行不带 --global 的设置:
cd /path/to/work/repo
git config user.name "John Internal"
git config user.email "john@company.com"
该配置将单独保存在 .git/config 中,优先级高于全局配置。
步骤三:验证当前生效的 Git 配置
查看当前生效的用户身份:
git config user.name
git config user.email
验证确认
再次运行代码提交命令:
git commit -m "feat: initial commit"
终端打印 [main (root-commit) a1b2c3d] feat: initial commit,提交顺利完成并正确记录作者信息。


