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

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

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
  • you got credentials set up to access that repo, right? – eftshift0 Aug 07 '20 at 18:10
  • @eftshift0 Yes, I use the same RSA keys on GitKraken(and before on SourceTree) to clone repos. They worked on those tools correctly. – Dr.jacky Aug 07 '20 at 18:13
  • Ok.... but did you set them up so that git can use them? In my case, I need to run `ssh-add` providing the private key to be able to access our repos with git. – eftshift0 Aug 07 '20 at 18:15
  • @eftshift0 Yes, I've added them as I mentioned in the question. I followed this: https://stackoverflow.com/a/3486339/421467. When I run `ssh-add -l` it shows both keys with the emails. – Dr.jacky Aug 07 '20 at 18:37
  • @eftshift0 Please look at the updated question. The key has not been used at all! But, I added it before. And now, I created a new pub/private key and used them but problem still exists. – Dr.jacky Aug 07 '20 at 18:56

2 Answers2

7

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!

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
  • 1
    For anyone else using `.gitconfig` to switch ssh identities, it didn't work for me until I removed all keys from the ssh agent. Otherwise, doing a git clone via ssh always used the first key the agent already had. As only git was using ssh keys in my system, I was fine with managing the ssh identities completely via `.gitconfig`. – LittleCornerDev Jan 21 '22 at 06:15
2

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.

Zhenkai
  • 318
  • 3
  • 15