SSH with multiple Bitbucket accounts

After hours of wasted time trying to figure this out and countless Google searches later, I found this Medium article that best explains how to set up SSH keys for multiple accounts on Bitbucket.

Here's the TLDR version:

Generate SSH Keys

cd into ssh directory

1
$ cd ~/.ssh

Create Default key if it doesn't exist

1
2
$ ssh-keygen -t rsa
$ ssh-add ~/.ssh/id_rsa

Creating other keys

1
2
$ ssh-keygen -t rsa -C "companyName" -f "companyName"
$ ssh-add ~/.ssh/companyName

Create SSH Config

~/.ssh/config

The value for Host is an alias

1
2
3
4
5
6
7
8
Host personal
  Hostname bitbucket.org
  User <personal_username>
  IdentityFile ~/.ssh/id_rsa
Host work
  Hostname bitbucket.org
  User <work_username>
  IdentityFile ~/.ssh/<other_key>

Set up Git remote origin for multiple accounts

Set up a remote origin with your personal account as you normally would:
$ git remote add origin ssh://git@bitbucket.org/<user_name>/<repo_name>.git

Set up your work account using the Host alias
$ git remote add origin ssh://git@<alias>/<user_name>/<repo_name>.git

Example:
$ git remote add origin ssh://git@work/<user_name>/<repo_name>.git