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

Dav*_*ght 4 git git-config

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

git config --global user.email "(none)"
git config --global user.useConfigOnly true
Run Code Online (Sandbox Code Playgroud)

我所做的。我的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
Run Code Online (Sandbox Code Playgroud)

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

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

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

# github account
Host github.com
Port 22
HostName github.com
User git
IdentityFile <file>
Run Code Online (Sandbox Code Playgroud)

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

无论如何,我希望能够强制我必须为每个存储库设置一个用户电子邮件地址,并且我希望它成为我全局gitconfig 的一部分。我知道有可能使用预提交钩子来做到这一点,但据我所知,克隆后需要将这个钩子复制到每个 repo,这不是我想要的。

axi*_*iac 5

文档git config解释:

user.useConfigOnly

指示 Git 避免尝试猜测user.email和的默认值user.name,而是仅从配置中检索值。例如,如果您有多个电子邮件地址并希望为每个存储库使用不同的电子邮件地址,那么true在全局配置中将此配置选项设置为与名称一起,Git 将提示您在进行新提交之前设置电子邮件在新克隆的存储库中。默认为false.

在类 Unix 系统(Linux、macOS 等)上,如果user.email未设置,Git 会尝试通过连接操作系统用户名@和主机名来生成电子邮件地址。

通过设置user.useConfigOnlytrue这种行为被抑制和Git只使用值的user.email它在找到的配置文件,如果有的话。

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

使用git config --global --unset user.email删除它,Git 将开始要求您user.email为每个未设置的存储库(新的或现有的)输入电子邮件地址(或至少会抱怨未设置)user.email