如何防止 Git 自动检测 user.email?

How do I prevent Git from auto-detecting user.email?

我没有特意设置任何全局 user.email 或全局 user.name

我喜欢使用 git config user.emailgit config user.name 命令为每个回购设置 user.email 和 user.name。

在 git 版本 2.17.0 的 macOS 上,如果我忘记为我的 Mac 上的回购设置 user.emailuser.name,当我 运行 git commit 我得到这个错误:

*** 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 'lone@mymac.(none)')

这很好,因为它提醒我需要为我的存储库设置 user.emailuser.name 配置。

但是在我的 git 版本 2.11.0 的 Debian 系统上,如果我忘记为回购设置 user.emailuser.name,而我 运行 git commit 它以 Lone Learner <lone@mydeb> 作为作者提交更改。我猜它会自动检测 /etc/passwd 中的 user.name 并自动检测 user.email<user>@<host>.

我想在 Debian 或任何系统 Git 上禁用 user.nameuser.email 的自动检测。如果我没有明确设置 user.nameuser.email,我希望 Git 以上面 Mac 示例中所示的失败方式或其他方式失败。有没有办法通过 ~/.gitconfig 或其他方式来实现?

自 Git 2.8 起,使用:

git config --global user.useConfigOnly true

这将避免 "autodetection" 在您的 Debian 环境中完成。
在“How do I make git block commits if user email isn't set?”查看更多信息。