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 "your_email@youremail.com"

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

pbcopy < ~/.ssh/id_rsa.pub

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

ssh-keygen -t rsa -f ~/.ssh/bitbucket_two_rsa -C "your_email@youremail.com"

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

Host bitbucket.org
  User git
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa

Host bitbucket-two
  User git
  HostName bitbucket.org
  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 git@bitbucket.org:username/project.git
git clone git@bitbucket-two:username/project.git