【问题标题】:Forcing Git user to set e-mail address in working copy强制 Git 用户在工作副本中设置电子邮件地址
【发布时间】:2018-04-05 11:22:22
【问题描述】:

显然可以强制 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

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

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

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

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

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

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

【问题讨论】:

    标签: git git-config


    【解决方案1】:

    documentation of git config 解释:

    user.useConfigOnly

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

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

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

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

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

    【讨论】:

    • 令人着迷的是,解决方案如此简单,尽管我花了几个小时试图找出我做错了什么。我一直在查看的其他消息来源说,将user.email 设置为"(none)" 是我必须做的。谢谢,尽管我正在运行 Windows,但明确取消设置电子邮件地址仍然有效。
    • 但是,git 不知道选项-g 和命令unset,我不得不使用git config --global --unset user.email
    • --global --unset 是正确的方法。这是我的回答错误;我现在修好了。感谢您指出这一点。
    猜你喜欢
    • 2014-06-23
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 2012-04-16
    • 1970-01-01
    相关资源
    最近更新 更多