Git - includeIf hasconfig:remote.*.url not working Git - includeIf hasconfig:remote.*.url未起作用

7
我希望在同一台电脑上使用两个不同的GitHub账号,所以我为它们都设置了ssh密钥。现在它们都可以正常工作。每次创建新仓库时我不想再次配置我的电子邮件和名称,因此我找到了git的“includeIf”部分。
我正在使用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
然而,当我为我的工作帐户克隆repo,比如git clone git@github.com-Work:Work/repo.git,并在repo内运行git config user.name时,我得到的是Home而不是Work。运行git config remote.origin.url返回git@github.com-Work:Work/repo.git
有什么想法为什么这不起作用?
2个回答

9
当然,要改变的第一件事是@LeGEC已经提到的。
但是,还有一个额外的细节需要注意(这已经在问题中完成了,但是我想强调一下,由于我无法评论,所以我会将其发布为答案供将来参考)。
指定的URL必须以*/**的全局模式结尾。 **不足以满足要求,因此您可能会发现自己处于git不包括附加配置的情况。
例如:
git@github.com-Work:*/**

替代

git@github.com-Work:**

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


5
一个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:*/**应该足够了。

我链接了一个新的配置文件,它运行得非常好。谢谢。 - NaeNate
1
关于答案中有关 git@github.com-Work:** 的最后一条评论,对于未来的读者而言,这并不足够。它需要是 git@github.com-Work:*/**(请参见本帖中的其他答案)。 - ksgj1

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