git: includeIf not working with git clone

Viewed 2340

.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

I've added the second ssh pub via ssh-add.

Now, on the terminal/iTerm2, I redirect to /Users/drjacky/Projects/CompanyName/HERE, then run this: git clone httpUrlOfMyCompanyRepo or git clone sshOfMyCompanyRepo; Doesn't matter, it doesn't have access:

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

And when I run git config user.email, under CompanyName folder path, it shows my personal email.

When I run 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

Notes:

  • I've uninstalled SourceTree but didn't help.
  • git version 2.28.0
  • After cloning the repo with a tool successfully, it works with the correct without a problem.

Update: I've check SSH section on Github dashboard, and it shows the SSH key has not been used at all!

enter image description here

2 Answers

I had to change Host github.com/Company to Host github.com-Company in ssh/config. Then, clone like this: git clone git@github.com-Company:Company/repoName.git

But, still prefer to have a better solution, instead of changing ssh address / HTTP URL.

Sources:

Update:

According to this comment:
Personal .gitconfig for personal/everything else projects:

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

Work .gitconfig for company projects:

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

And changed the global .gitconfig to this:

[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

Thanks to leddzip!

For some reason, Dr.jacky's solution didn't work for my case.

I ended up using url.<base>.insteadOf and url.<base>.pushInsteadOf in .gitconfig + ~/.ssh/config

url.<base>.insteadOf and url.<base>.pushInsteadOf can update the github url on the fly

For example, following .gitconfig snippet can update the git clone git@github.com:company/xxxx dynamically to git clone git@github.com-company:company/xxxx

[url "git@github.com-company:company"]
    insteadOf = git@github.com:company
    pushInsteadOf = git@github.com:company

and with the ~/.ssh/config from original post

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

git can pick the right ssh key without any issue.

Related