git:includeIf 在 git clone 中不起作用

6

.gitconfig:

[user]
    name = Dr.jacky
    email = personal.email@gmail.com
    signingKey = ""
[includeIf "gitdir:/Users/drjacky/Projects/CompanyName/"]
    path = /Users/drjacky/gitconfigcompanyname/.gitconfig
[core]
    excludesfile = /Users/drjacky/.gitignore_global
    autocrlf = input
[difftool "sourcetree"]
    cmd = opendiff \"$LOCAL\" \"$REMOTE\"
    path = 
[mergetool "sourcetree"]
    cmd = /Applications/Sourcetree.app/Contents/Resources/opendiff-w.sh \"$LOCAL\" \"$REMOTE\" -ancestor \"$BASE\" -merge \"$MERGED\"
    trustExitCode = true
[commit]
    template = /Users/drjacky/.stCommitMsg
    gpgSign = false
[gpg]
    program = gpg
[tag]
    forceSignAnnotated = false

gitconfigcompanyname/.gitconfig:

[user]
    name = My Name
    email = my.company.email@company.com

.ssh/config:


# --- Sourcetree Generated ---
Host Personal-GitHub
    HostName github.com
    User Drjacky
    PreferredAuthentications publickey
# IdentityFile /Users/drjacky/.ssh/Personal-GitHub
    IdentityFile ~/.ssh/id_rsa
    UseKeychain yes
    AddKeysToAgent yes
# ----------------------------
# Company Work GitHub
Host github.com/Company
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_companyname
    UseKeychain yes
    AddKeysToAgent yes

我已经通过 ssh-add 命令添加了第二个 SSH 公钥。

现在,在终端/iTerm2 中,我切换到路径 /Users/drjacky/Projects/CompanyName/HERE,然后运行以下命令:git clone httpUrlOfMyCompanyRepo 或者 git clone sshOfMyCompanyRepo;不管用哪个命令,都无法访问:

远程:未找到仓库。 致命错误:'https://github.com/CompanyName/reponame.git/' 仓库未找到

当我在 CompanyName 文件夹路径下运行 git config user.email 命令时,它显示的是我的个人电子邮件地址。

当我运行 git config --list 命令时:

credential.helper=osxkeychain
user.name=Dr.jacky
user.email=personal.email@gmail.com
user.signingkey=
includeif.gitdir:/Users/drjacky/Projectz/CompanyName/.path=/Users/drjacky/gitconfigcompanyname/.gitconfig
core.excludesfile=/Users/drjacky/.gitignore_global
core.autocrlf=input
difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/Sourcetree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
mergetool.sourcetree.trustexitcode=true
commit.template=/Users/drjacky/.stCommitMsg
commit.gpgsign=false
gpg.program=gpg
tag.forcesignannotated=false
(END)

git bugreport:

[System Info]
git version:
git version 2.28.0
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Darwin 19.6.0 Darwin Kernel Version 19.6.0: Sun Jul  5 00:43:10 PDT 2020; root:xnu-6153.141.1~9/RELEASE_X86_64 x86_64
compiler info: clang: 11.0.3 (clang-1103.0.32.62)
libc info: no libc information available
$SHELL (typically, interactive shell): /bin/zsh

注释:

  • 我已经卸载了SourceTree但没有帮助。
  • git版本号为2.28.0。
  • 使用工具成功克隆存储库后,正确地运行没有问题。

更新: 我检查了Github仪表板上的SSH部分,它显示SSH密钥根本没有被使用!

输入图像描述


你已经设置了访问该仓库的凭据,对吗? - eftshift0
@eftshift0 是的,我在GitKraken(以及之前在SourceTree上)使用相同的RSA密钥来克隆存储库。它们可以在这些工具上正常工作。 - Dr.jacky
好的...但是你设置了它们以便git可以使用吗?在我的情况下,我需要运行ssh-add并提供私钥才能使用git访问我们的存储库。 - eftshift0
@eftshift0 是的,我已经按照问题中提到的添加了它们。我遵循了这个链接:https://dev59.com/-3E95IYBdhLWcg3wMa51#3486339。当我运行 ssh-add -l 时,它显示了带有电子邮件的两个密钥。 - Dr.jacky
@eftshift0 请查看更新后的问题。密钥根本没有被使用过!但是,我之前已经添加了它。现在,我创建了一个新的公/私钥并使用它们,但问题仍然存在。 - Dr.jacky
2个回答

7

我必须在ssh/config中将 Host github.com/Company 更改为 Host github.com-Company,然后像这样克隆:

git clone git@github.com-Company:Company/repoName.git

但是,仍然希望有更好的解决方案,而不是更改ssh地址/ HTTP URL。

来源:

更新:

根据此评论
个人用于个人/其他所有项目的.gitconfig

[user]
    name = Dr.jacky
    email = email@personal.com
[core]
    sshCommand = ssh -i ~/.ssh/id_rsa -F /dev/null

为公司项目工作使用.gitconfig

[user]
    name = Work Name
    email = work@company.com
[core]
    sshCommand = ssh -i ~/.ssh/id_rsa_companyname -F /dev/null

我将全局的 .gitconfig 文件更改为以下内容:

[user]
    name = Dr.jacky
    email = email@personal.com
    signingKey = ""
[includeIf "gitdir:/Users/drjacky/*"]
    path = /Users/drjacky/gitconfigpersonal/.gitconfig
[includeIf "gitdir:/Users/drjacky/Projectz/CompanyName/"]
    path = /Users/drjacky/gitconfigcompanyname/.gitconfig

感谢 leddzip


1
对于其他使用.gitconfig切换ssh身份的人,直到我从ssh代理中删除了所有密钥,它才能正常工作。否则,通过ssh进行git克隆始终会使用代理已经拥有的第一个密钥。由于只有git在我的系统中使用ssh密钥,因此我可以完全通过.gitconfig来管理ssh身份。 - LittleCornerDev

2
由于某些原因,Dr.jacky的解决方案对我的情况不起作用。
最终我使用了url.<base>.insteadOfurl.<base>.pushInsteadOf.gitconfig~/.ssh/config中。 url.<base>.insteadOfurl.<base>.pushInsteadOf可以动态更新Github URL。
例如,以下.gitconfig片段可以将git clone git@github.com:company/xxxx动态更新为git clone git@github.com-company:company/xxxx
[url "git@github.com-company:company"]
    insteadOf = git@github.com:company
    pushInsteadOf = git@github.com:company

并且使用原始帖子中的~/.ssh/config


# Company Work GitHub
Host github.com/Company
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_companyname
    UseKeychain yes
    AddKeysToAgent yes

Git可以轻松选择正确的SSH密钥,不会出现任何问题。


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