i already read this very similar question and although i found what should be the answer, it does not work for me.
I have a personal github account, and another one for the work, and i use two different ssh keys for them, so i would like to be able to match automatically the right key. One solution that is fine for me is to always clone the work repo in a specific folder or subfolders, so i was thinking of using the match
directive to achieve this, but it does not work :
# For gitHub personal account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
# For GitHub work account
Match host github.com exec "pwd | grep /path/to/work/directory"
IdentityFile ~/.ssh/id_ed25519_github_work
when i'm inside the work directory and i try to clone a git repo, it does not work. I get an ERROR: Repository not found.
which is a git error I think, so I suppose ssh didn't match the right key.
I must add that the identity file is correct, if i just change the first identity file for the work one, it works.
I tried lots of variations of this idea, for example putting the match assertion before the personal github host, or duplicating all the directives (hostname, user, identitiesOnly) under the match directive for work account.
I've read the ssh_config manual but cannot figure it out.
Also i know that the match syntax works, indeed if I only have the work config in my ssh config file, i can clone the work github repo inside the work directory and not outside, so it works :
## For gitHub personal account
#Host github.com
# HostName github.com
# User git
# IdentityFile ~/.ssh/id_ed25519_github
# IdentitiesOnly yes
# For GitHub work account
Match host github.com exec "pwd | grep /path/to/work/directory"
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_work
IdentitiesOnly yes
Reading through man ssh_config
I thought order would matter, so I tried with the more specific declaration first. Which for you would have been:
# For GitHub work account
Match host github.com exec "pwd | grep /path/to/work/directory"
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_work
IdentitiesOnly yes
# For gitHub personal account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
But that didn't work for me. Let me know if it does for you.
What did work for me was explicitly negating the second one:
# For GitHub work account
Match host github.com exec "pwd | grep /path/to/work/directory"
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_work
IdentitiesOnly yes
# For gitHub personal account
Match host github.com !exec "pwd | grep /path/to/work/directory"
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes