Multi-account Git Magic With SSH config

1 min read

If you do software development work these days, it’s possible you have multiple GitHub, GitLab, Bitbucket or other accounts. And even if you only have to deal with multiple services, for security reasons it is best to avoid key reuse.

My personal need comes from interoperating with multiple clients as well as my company and of course my own personal git repositories. I have about five GitHub accounts right now, one GitLab account, and several BitBucket accounts.

Of course the first question is “why not just use your personal account and have the clients add you to their [project|team|org]?” The answer is not so simple. Some clients request a new GitHub account and sometimes it’s nice to be able to ditch an account win a contract ends. Eventually it just becomes a process.

This, however, makes it a huge pain to work with the tool via ssh as GitHub and GitLab map keys to the accounts on the backend. Luckily this is relatively easy to manage once you implement a few stanzas in your ssh config.

So with a little bit of effort in my ~/.ssh/config:

Host personal.github.com

   HostName github.com

   IdentityFile ~/.ssh/id_rsa_personal

   IdentitiesOnly yes


Host client.github.com

   HostName github.com

   IdentityFile ~/.ssh/id_rsa_client

   IdentitiesOnly yes

Then as I do checkouts for myself or for my clients, I modify the clone command slightly:

git clone git@personal.github.com:personal/personal-repo.git

git clone git@client.github.com:client/client-repo.git

With the way that ssh works, the HostName in the stanza is substituted for the hostname supplied on the git clone line and the associated private key is used to authenticate against the public key that one has associated with the account on GitHub/GitLab.

And that’s it - a quick and easy method for dealing with multiple git accounts.

Image of Stephen Sadowski

Stephen Sadowski

Leader focusing on quality, delivery, technical debt management, and leadership education about DevOps and SRE practices