Wednesday, November 2, 2016

BitBucket Git checkout over SSH with multiple accounts

If you have multiple user accounts at BitBucket (e.g. personal and work), you may run into a problem. The first think you will notice that you could not use the same ssh key. Adding it to the second account will give you "the key is already used by someone else" error message. Now if you generate a new key and add it to your ssh-agent (via ssh-add), you will not be able to check out from one of your accounts.

The problem is caused by ButBucket identifying a user by first SSH key he or she supplies. There are several workarounds for this problem, but the one I prefer is to use ssh Host alias. In your ~/.ssh/config file add section like this:
Host bitbucket-cm
    User git
    IdentityFile ~/.ssh/id_rsa.1
    IdentitiesOnly yes
This would basically create a SSH host alias bitbucket-cm for associated with id_rsa.1 key. Now, instead of usual checkout URL, say, 
git clone 
git clone git@bitbucket-cm:org/repo.git