根据远程克隆URL,为Git配置使用不同的user.email和user.name。

57
我像这样配置我的全局 ~/.gitconfig 文件中的 user.nameuser.email 属性:
git config --global user.email "mkobit@example.com" 
git config --global user.name "mkobit"

这是我在个人项目、开源项目等方面想要的默认配置。

当我在处理特定领域的项目时,例如公司领域,我会在克隆每个存储库时为其配置,以便它使用不同的user.name/user.email

git clone ssh://git@git.mycorp.com:1234/groupA/projectA.git
cd projectA
git config user.email "mkobit@mycorp.com"
git config user.name "m.kobit"

一个不错的选择是设置这种仓库的克隆别名:

git config --global alias.clonecorp 'clone \ 
        -c user.name="m.kobit" -c user.email="mkobit@mycorp.com"'
git clonecorp ssh://git@git.mycorp.com:1234/groupA/projectA.git

这两种方法都可能存在错误,因为它们都依赖于我聪明而且按照正确的步骤进行。证据表明,我很可能在某个时候搞砸。

有没有一种方法可以配置Git,使得来自特定域名(例如此处的mycorp.com)的仓库可以以特定方式进行配置?


我一直在寻找完全相同的东西。这真是令人失望。特别是如果您在开发或自动构建环境中维护同一存储库的多个副本或经常删除/重新克隆。类似于管理不同的ssh_ids即可。 - abdus_salam
4个回答

62

发布了 Git 2.13 版本,引入了一个称为“条件包含”的功能。在 2.13 中,唯一支持的配置是文件系统路径。在这种情况下,它很容易使用,因为我已经将它们分开了。

发布说明中提供的示例为:

You can configure two conditional includes in your home directory's ~/.gitconfig file:

[includeIf "gitdir:~/work/"]
    path = .gitconfig-work
[includeIf "gitdir:~/play/"]
    path = .gitconfig-play

Now you can put whatever options you want into those files:

$ cat ~/.gitconfig-work
[user]
name = Serious Q. Programmer
email = serious.programmer@business.example.com

$ cat ~/.gitconfig-play
[user]
name = Random J. Hacker
email = rmsfan1979@example.com

旧答案

Git 2.8中,添加了一个名为user.useconfigonly的全局配置项,它要求用户在提交之前设置他们的user.emailuser.name。以下是Github博客文章中相关的文本:

But if, say, you want Git to use one email address for your open source projects and a different one for your work projects, you've undoubtedly made the mistake of committing to a new Git repository without having first set your email address in that repository. In this situation, Git emits a warning, but it creates the commit anyway, using an email address that it guesses from the local system hostname. If you're trying to do something as complicated as different addresses for different projects, this is almost certainly not what you want.

Now you can tell Git not to guess, but rather to insist that you set user.name and user.email explicitly before it will let you commit:

git config --global user.useconfigonly true
这并不能解决基于某些克隆URL的自动配置,但是可以通过在开始时强制进行配置来使过程变得更加不容易出错。

3
我想在这里补充说明,使用尾随斜杠~/work/在条件语句中非常重要。谢谢! - smnbbrv
你能否验证一下是否正确设置了呢?可以进入子文件夹中的存储库并运行 git config user.name 命令来检查。我尝试访问它时无法获取值。 - user14492
2
请注意,现在支持更多的关键字,有关完整列表和其他信息,请参见 https://git-scm.com/docs/git-config#_conditional_includes。 - Stefan

9
我发现自己也遇到了同样的情况:当我将代码推送到非公司仓库后才意识到我使用了公司邮箱。所以我编写了一个小型的git-hook,您可能会发现它很有用:https://github.com/DrVanScott/git-clone-init
基于可配置的模式文件,它将在 git clone 时初始化用户的 email 和 name。

9
截至 2.36.0,Git 现在通过 远程 URL 支持此功能。
~/.gitconfig 文件:
[includeIf "hasconfig:remote.*.url:git@github.com*/**"]
    path = .gitconfig-public
[includeIf "hasconfig:remote.*.url:git@ghe.corp.lol*/**"]
    path = .gitconfig-work

然后将您的自定义内容添加到上述引用的配置中。

2
当我在处理特定领域的项目,例如企业领域时,我会在克隆时为每个仓库进行配置。
请确保使用 Git 2.22(2019年第二季度发布)如果您的条件包括使用模式的配置IncludeIf:
参见 commit 19e7fda(由Nguyễn Thái Ngọc Duy(pclouds于2019年3月26日提交)。
(由Junio C Hamano -- gitster --于2019年4月22日合并至commit 078b254 “配置”:在“includeIf”模式中正确匹配“**”。
当前的wildmatch()调用includeIf的gitdir模式未传递WM_PATHNAME标志。
没有这个标志,'*'被视为与'**'几乎相同(因为'*'也匹配斜杠),只有一个例外:
'/**/'可以匹配单个斜杠。
模式'foo/**/bar'匹配'foo/bar'。
但是'/*/',本质上是没有WM_PATHNAME的wildmatch引擎看到的内容,必须匹配两个斜杠('*'不匹配任何内容)。
这意味着'foo/*/bar'无法匹配'foo/bar'。它只能匹配'foo//bar'。
这导致当前的wildmatch()调用大多数情况下都能正常工作,直到用户依赖'/**/'不匹配任何路径组件。
而且'*'匹配斜杠而不应该,但人们可能还没有注意到这一点。修复方法很简单。
使用 Git 2.36 (2022 年第二季度),您还可以使用 git config hasconfig:remote.*.url: 命令。

意思是,使用 "[includeIf <condition>]" 语法的配置文件的条件包含机制已经学会基于与存储库交互的远程存储库的 URL 进行决策。
; include only if a remote with the given URL exists (note
; that such a URL may be provided later in a file or in a
; file read after this file is read, as seen in this example)
[includeIf "hasconfig:remote.*.url:https://example.com/**"]
    path = foo.inc
[remote "origin"]
    url = https://example.com/git

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接