export

Configuring SSH for multiple Bitbucket accounts

2023-08-23 | 2 min read
Armand Rego
If you work in multiple Bitbucket accounts on the same machine, a small amount of upfront effort configuring your SSH keys will significantly improve your developer experience.

The pain point

There are multiple reasons why you might need to work in multiple Bitbucket accounts:

  • You might have one account for work, and one for your own personal projects.
  • You might be a freelancer with multiple clients, working in their own Bitbucket environments.

Whatever the reason, you will need to manage an SSH key for each Bitbucket account, and modifying your SSH config on every push/pull is not the answer!

An exasperated developer manually changing SSH key for the 1452th time. Photo by Tim Gouw on Unsplash

The solution

Assuming you've already created your SSH keys in the normal way, all you need to do is modify your .ssh/config file to look something like this:

1
Host bitbucket-work
2
 HostName bitbucket.org
3
 User git
4
 IdentityFile ~/.ssh/id_rsa_bitbucket_work
5
 IdentitiesOnly yes
6

7
Host bitbucket-personal
8
 HostName bitbucket.org
9
 User git
10
 IdentityFile ~/.ssh/id_rsa_bitbucket_personal
11
 IdentitiesOnly yes

You need to replace id_rsa_bitbucket_work and id_rsa_bitbucket_personal with the names of your SSH keys.

To clone a repo with your newly configured keys, simply replace bitbucket.org with either bitbucket-work or bitbucket-personal in the URL string. For example, to clone a repository in your work account:

git clone git@bitbucket.org:my-org/my-repo.git

will become

git clone git@bitbucket-work:my-org/my-repo.git

Specify the repository git user

The last thing to do is set the git user on a repo-by-repo basis in the normal way, depending on which account you've cloned the repo from, for example:

1
git config user.name "Joe Bloggs"
2
git config user.email "joe@weirdsheeplabs.com"

And that's it, now you can easily work in multiple Bitbucket accounts from the same device!

© Weird Sheep Labs Ltd 2024
Weird Sheep Labs Ltd is a company registered in England & Wales (Company No. 15160367)
85 Great Portland St, London, W1W 7LT