Multiple Git Accounts and SSH Keys

If you have multiple accounts with a Git solution such as Bitbucket or GitHub and are using SSH keys to connect to your repository you will find they don’t allow you to use the same SSH key per account and you have to generate a different key per account with a different name.

You can generate an SSH key like so:

cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]"

Once the key is generated you copy and paste it into your Git solution account settings. Copying to the clipboard like so:

pbcopy < ~/.ssh/

With multiple accounts you need to generate a second key with a different name:

ssh-keygen -t rsa -f ~/.ssh/bitbucket_two_rsa -C "[email protected]"

To use multiple keys you’ll need to create a config file in ~/.ssh/config with the following contents as an example:

  User git
  IdentityFile ~/.ssh/id_rsa

Host bitbucket-two
  User git
  IdentityFile ~/.ssh/bitbucket_two_rsa

Now you can clone your repositories for both of your accounts by changing the Host name to match what you have specified in your config file.

git clone [email protected]:username/project.git
git clone git@bitbucket-two:username/project.git