强制 Git 用户在工作副本中设置电子邮件地址

Forcing Git user to set e-mail address in working copy

显然可以强制 Git 用户使用以下 gitconfig 设置为每个新存储库添加电子邮件地址:

git config --global user.email "(none)"
git config --global user.useConfigOnly true

我已经做到了。我的 gitconfig -l 输出(截断):

core.symlinks=true
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
diff.astextplain.textconv=astextplain
rebase.autosquash=true
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.required=true
filter.lfs.process=git-lfs filter-process
credential.helper=manager
core.autocrlf=true
core.eol=native
user.name=David Wright
user.email=(none)
user.useconfigonly=true

但它似乎不起作用。我可以在不设置电子邮件地址的情况下提交。提交中列出的电子邮件地址(哦,奇迹)是“(none)”。

我正在使用 git version 2.13.0.windows.1

我想这样做是因为我在同一台计算机上使用两个不同的帐户。我能够在 ssh-config 文件中应用不同的 ssh-keys 实现类似的设置:

# github account
Host github.com
Port 22
HostName github.com
User git
IdentityFile <file>

但是git本身显然没有这种可能性。

无论如何,我希望能够强制我必须为每个存储库设置一个用户电子邮件地址,并且我希望它成为我的 global[=33= 的一部分] git配置。我知道使用预提交挂钩可以做到这一点,但据我所知,这个挂钩需要在克隆后复制到每个 repo,这不是我要找的。

documentation of git config说明:

user.useConfigOnly

Instruct Git to avoid trying to guess defaults for user.email and user.name, and instead retrieve the values only from the configuration. For example, if you have multiple email addresses and would like to use a different one for each repository, then with this configuration option set to true in the global config along with a name, Git will prompt you to set up an email before making new commits in a newly cloned repository. Defaults to false.

在 Unix-like 系统(Linux、macOS 等)上,当未设置 user.email 时,Git 会尝试生成电子邮件地址连接 OS 用户名、@ 和主机名。

通过将 user.useConfigOnly 设置为 true,此行为被抑制并且 Git 仅使用它在 configuration files 中找到的 user.email 的值,如果有的话.

由于您已将 (none) 配置为 user.email,Git 将其用作提交者电子邮件地址。

使用 git config --global --unset user.email 删除它,Git 将开始要求您为每个存储库(新或现有)没有设置 user.email