Git - includeIf hasconfig:remote.*.url not working



我想在同一台计算机上使用两个独立的GitHub帐户,所以我为它们都设置了ssh密钥。两个都工作得很好。我不想每次创建新的repo时都配置我的电子邮件和名称,所以我环顾四周,发现git的";包括If";部分

我使用的是git版本2.37.3

这些是我现在的配置文件。

~/.gitconfig

[user]
email = "home@example.com"
name = "Home"
[includeIf "hasconfig:remote.*.url:git@github.com-Work:*/**"]
email = "work@example.com"
name = "Work"

~/.ssh/config

Host github.com-Home
HostName github.com
User git
IdentityFile ~/.ssh/Home
Host github.com-Work
HostName github.com
User git
IdentityFile ~/.ssh/Work

当我克隆像git clone git@github.com-Home:Home/repo.git这样的repo并在repo中运行git config user.name时,我得到了Home的预期输出

但是,当我为我的工作帐户(如git clone git@github.com-Work:Work/repo.git(克隆repo并在repo中运行git config user.name时,我得到的是Home而不是Work。运行git config remote.origin.url返回git@github.com-Work:Work/repo.git

你知道为什么不起作用吗?

当然,首先要改变的是@LeGEC已经提到的内容。

然而,还有另一个额外的细节需要注意(这已经在问题中完成了,但我想强调一下,由于我无法发表评论,我将把它作为答案发布,以供将来参考(。

指定的URL末尾必须具有*/**的glob模式。**是不够的,因此您可能会发现git不包括额外的配置。

示例:

git@github.com-Work:*/**

而不是

git@github.com-Work:**

这是我不知道的事情,所以我想知道为什么它不起作用。对我来说,解决方案就是问题!

includeIf...语句只允许包含另一个文件(而不是:同一文件中的配置部分(。

您必须将email, name参数放在一个单独的文件中,并在主配置文件中使用path = ...指向该文件。


引用git help config:中的相关示例

; 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

模式git@github.com-Work:*/**应该足够了。

最新更新