You only have to run each of the three commands once; you can put them in whatever login shell configuration file you have (e.g., .bash_profile
).
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa_my_git
ssh-add ~/.ssh/id_rsa_company_git
Now, when you use git
, ssh
will simply try any key it finds in the agent until it finds one that works.
This is fine as long as you don't have so many keys to try that the remote end won't stop you after X failed attempts, before you can try the correct key. In that case, you can modify your ssh
configuration to create an alias for GitHub that uses a single specific key, so that ssh
can always use the correct key the first time. This approach also eliminates the need to use ssh-agent
at all, though you might still want to if you have a passphrase and only want to type it once, when you first load the key into the agent.
For example, you could put the following in your ~/.ssh/config
file.
Host github-me
Hostname github.com
IdentityFile id_rsa_my_git
Host github-company
Hostname github.com
IdentityFile id_rsa_company_git
Then you would use commands like
git clone git@github-me:me/personal_project
git clone git@github-company:company/work_project
and ssh
will know which key to use based on the host name.