Igor's lucky notes
Note written on June 17, 2024

How to work with multiple Github ssh keys

This is a very common issue, mainly when we've got an account for personal use and another one for work, so here is how it works

  1. Generate your SSH key
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  1. Tell SSH about your new key
ssh-add ~/.ssh/id_rsa
  1. Create a config file in your ./.ssh with this content
Host github-my-work-account
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa
  1. And when you add your remote, it's just replace the host from URI:
git remote add origin git@github-my-work-account:username/repo.git

and done!

It works with Bitbucket or Gitlab, it's just adding on config some new properties

Host github-my-work-account
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

Host gitlab-my-personal-account
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa2

Host bitbucket-my-work2-account
  HostName bitbucket.com
  User git
  IdentityFile ~/.ssh/id_rsa3

And when you'll be cloning the repo, follow the same steps

git remote add origin git@gitlab-my-personal-account:username/repo.git
git remote add origin git@bitbucket-my-work2-account:username/repo.git

When you push new stuff to the remote, git will handle the correct credentials!