【问题标题】:git: includeIf not working with git clonegit: includeIf 不使用 git clone
【发布时间】:2020-11-28 02:33:21
【问题描述】:

.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 pub。

现在,在终端/iTerm2 上,我重定向到 /Users/drjacky/Projects/CompanyName/HERE,然后运行以下命令: git clone httpUrlOfMyCompanyRepogit 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
  • 使用工具成功克隆 repo 后,它可以正常工作,没有问题。

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

【问题讨论】:

  • 您设置了访问该存储库的凭据,对吧?
  • @eftshift0 是的,我在 GitKraken(以及之前在 SourceTree 上)上使用相同的 RSA 密钥来克隆存储库。他们正确地使用了这些工具。
  • 好的....但是您是否设置了它们以便 git 可以使用它们?就我而言,我需要运行 ssh-add 提供私钥才能使用 git 访问我们的存储库。
  • @eftshift0 是的,正如我在问题中提到的那样,我已经添加了它们。我跟着这个:stackoverflow.com/a/3486339/421467。当我运行ssh-add -l 时,它会显示电子邮件的两个键。
  • @eftshift0 请查看更新后的问题。钥匙根本没用过!但是,我之前加过。现在,我创建了一个新的 pub/private 密钥并使用了它们,但问题仍然存在。

标签: git github ssh-keys git-config


【解决方案1】:

我不得不将ssh/config 中的Host github.com/Company 更改为Host github.com-Company。 然后,像这样克隆: git clone git@github.com-Company:Company/repoName.git

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

来源:

更新:

据此comment:
个人.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

【讨论】:

  • 对于其他使用.gitconfig 切换 ssh 身份的人,在我从 ssh 代理中删除所有密钥之前,它对我不起作用。否则,通过 ssh 执行 git clone 总是使用代理已经拥有的第一个密钥。由于我的系统中只有 git 使用 ssh 密钥,因此我可以通过 .gitconfig 完全管理 ssh 身份。
【解决方案2】:

出于某种原因,Dr.jacky 的解决方案不适用于我的情况。

我最终在.gitconfig + ~/.ssh/config 中使用了url.<base>.insteadOfurl.<base>.pushInsteadOf

url.<base>.insteadOfurl.<base>.pushInsteadOf 可以即时更新 github url

例如关注.gitconfig sn-p 可以将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 密钥。

【讨论】:

    猜你喜欢
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多