This is a good article for how to get multiple accounts, with differing ssh keys, to work with github: https://medium.com/@xiaolishen/use-multiple-ssh-keys-for-different-github-accounts-on-the-same-computer-7d7103ca8693
I started with this, but this scheme means I can't just cut-paste the repo link from browser to command line; I'd have to rewrite the host every time I want to use the second key. Example:
# This will always pick up my main ssh key
git clone git@github.com:Hefeweizen/junk.git
# To use an alternate key, I have to do some work.
# The browser copy-to-clipboard button will load `git@github.com:Organization/junk.git into my paste buffer.
# after pasting to the command line, I have to manually rewrite the domain, to work with my ssh_config:
git clone ssh-key-for-organization:Organization/junk.git
Referencing man ssh_config
I found the Match exec
keywords. In the snippet below, I check what directory I'm in. If I'm in a subdirectory with 'organization' in the name, I'll use the ssh key for that organization.
Host github.com
HostName github.com
User git
IdentitiesOnly yes
Match Host github.com exec "pwd | grep '/organization' > /dev/null"
IdentityFile ~/.ssh/specific_ssh_key
Match Host github.com !exec "pwd | grep '/organization' > /dev/null"
IdentityFile ~/.ssh/default_ssh_key
This solution is sufficient for one or two extra keys. If you're looking to have a larger array of keys, then the default key scenario will get verbose quickly as you list all the exceptions. If you've got ideas on how to make that simple, please shout.