git:includeIf 不使用 git 克隆

git: includeIf not working with git clone

.git配置:

[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。

现在,在 terminal/iTerm2 上,我重定向到 /Users/drjacky/Projects/CompanyName/HERE,然后 运行 这个: git clone httpUrlOfMyCompanyRepogit clone sshOfMyCompanyRepo;没关系,它没有权限:

remote: Repository not found. fatal: repository 'https://github.com/CompanyName/reponame.git/' not found

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

当我运行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

备注:

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

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

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

来源:

更新:

根据这个comment:
personal/everything 其他项目的个人 .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!

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

我最终在 .gitconfig + ~/.ssh/config

中使用了 url.<base>.insteadOfurl.<base>.pushInsteadOf

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

以及来自原始 post

~/.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 密钥。