bigywater.tech
常见报错 · 26

Git Author identity unknown / Please tell me who you are:这台 macOS 默认自动拼身份也能提交,要 useConfigOnly 才报错,优先级实测

“Author identity unknown *** Please tell me who you are” 是新装 git 的第一道坎。这篇在本机把它复现出来,发现在这台 macOS 上不设 user.useConfigOnly 的话 git 其实会自动猜出身份并提交;再把几处配置的优先级、按目录切换身份和配错之后的补救逐个测一遍。

自动
这台 macOS 上没配身份时 git commit 默认成功,身份是自动拼的“登录名@主机名”
4 层
测得的优先级:全局 < 仓库 < -c < 环境变量(--author 只改作者)
3 个
一串提交用 rebase --exec 重置作者后被改写的提交数,哈希全部变化

这个报错什么时候出现

实测 · 本机执行输出
    $ git commit -m first 2>&1 | head -8; git log --format='作者 %an <%ae> | 提交者 %cn <%ce>' -1
      [main (root-commit) 478c6eb] first
       Committer: <登录名> <<登录名>@<主机名>>
      Your name and email address were configured automatically based
      on your username and hostname. Please check that they are accurate.
      You can suppress this message by setting them explicitly. Run the
      following command and follow the instructions in your editor to edit
      your configuration file:
      
      作者 <登录名> <<登录名>@<主机名>> | 提交者 <登录名> <<登录名>@<主机名>>
    $ git config user.useConfigOnly true; git commit -m first    (退出码 128)
      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: no email was given and auto-detection is disabled
    $ git var GIT_AUTHOR_IDENT 2>&1    (退出码 128)
      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: no email was given and auto-detection is disabled
    $ git -c user.useConfigOnly=false var GIT_AUTHOR_IDENT 2>&1; hostname
      <登录名> <<登录名>@<主机名>> 1767196800 +0800
      <主机名>

没配身份且无法自动猜、或者设了 user.useConfigOnly=true 时;这台机器上默认能自动猜。

哪些命令要身份

实测 · 本机执行输出
  git add          退出码 0   OK
  git log          退出码 0   OK
  git commit       退出码 128 Author identity unknown
  git tag(轻量)      退出码 0   OK
  git tag -a       退出码 128 Committer identity unknown
  git stash        退出码 0   OK
  git revert       退出码 128 Author identity unknown
  git branch       退出码 0   OK

commit、revert、tag -a 要;add、log、轻量 tag、branch、stash 不要。

优先级

实测 · 本机执行输出
    $ git config --show-origin --get-all user.email | sed "s#<tmp>#<tmp>#"; git config --show-origin --get-all user.name | sed "s#$HOME#~#"
      file:<tmp>/_home/gitconfig	global@example.com
      file:.git/config	repo@example.com
      file:~/gitconfig	GlobalName
      file:.git/config	RepoName
  只有全局 + 仓库配置:
      作者 RepoName <repo@example.com> | 提交者 RepoName <repo@example.com>
  再加环境变量 GIT_AUTHOR_NAME / GIT_COMMITTER_EMAIL:
      作者 EnvAuthor <repo@example.com> | 提交者 RepoName <env-committer@example.com>
  同时有 GIT_AUTHOR_NAME=EnvAuthor 和 -c user.name=CliName:
      作者 EnvAuthor <repo@example.com> | 提交者 CliName <repo@example.com>
  --author 指定:
      作者 Other <other@example.com> | 提交者 RepoName <repo@example.com>

全局 < 仓库 < -c < 环境变量,作者和提交者各自独立,--author 只改作者。

按目录换身份

实测 · 本机执行输出
  在 work/proj 里提交:作者 Me <me@company.example> | 提交者 Me <me@company.example>
  在 personal 里提交:作者 Me <me@personal.example> | 提交者 Me <me@personal.example>
    $ git config --show-origin --get-all user.email | sed "s#<tmp>#<tmp>#"
      file:<tmp>/_home/gitconfig	me@personal.example

includeIf gitdir 条件配置。

配错了怎么改

实测 · 本机执行输出
    $ git log --format='%h %an <%ae> %s'
      08506a7 Wrong <wrong@example.com> c3
      9d42fc5 Wrong <wrong@example.com> c2
      aeb44a3 Wrong <wrong@example.com> c1
    $ git config user.name Right; git config user.email right@example.com; git commit -q --amend --reset-author --no-edit; git log --format="%h %an <%ae> %s"
      fedd650 Right <right@example.com> c3
      9d42fc5 Wrong <wrong@example.com> c2
      aeb44a3 Wrong <wrong@example.com> c1
    $ git rebase -q -r --root --exec "git commit -q --amend --reset-author --no-edit" 2>&1 | tail -2; git log --format="%h %an <%ae> %s"
      24f0e89 Right <right@example.com> c3
      d1d18f9 Right <right@example.com> c2
      65d1ab9 Right <right@example.com> c1
  空字符串的身份:
    $ git config user.name ""; git commit -q --allow-empty -m e 2>&1 | tail -2
      
      fatal: empty ident name (for <right@example.com>) not allowed

amend --reset-author 改最近一次;rebase --exec 改一串,哈希都会变。

分步演示

身份未知
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64;报错文本都是真实复现时原样捕获的(命令行工具统一用 LC_ALL=C 取英文原文,路径换成 );所有服务都是本机上自己起的、只监听 127.0.0.1,不访问外网;计时用 time.perf_counter,数字每次运行会有小幅抖动。本篇用 git 2.54.0(Apple Git-157);用 HOME 与 GIT_CONFIG_GLOBAL 指向临时目录隔离了配置,起点是没有任何身份配置;登录名和主机名在输出里换成了 <登录名>、<主机名>
    • 没有验证:Linux 上没有可解析主机名时的报错(自动拼身份可能失败,从而直接出现 Please tell me who you are);Windows 的凭据与配置位置;GitHub 用 noreply 邮箱关联提交的规则;签名提交(gpg / ssh)需要的 user.signingkey;git 老版本的提示文字
    京ICP备2026067502号-1京公网安备11010502063487号