Managing multiple Bitbucket user SSH keys on one device
If you have more than one Bitbucket Cloud account (such as a personal account and a work account), some additional configuration is required to use two (or more) accounts on the same device. This additional configurations ensures that Git connects to Bitbucket as the correct user for each repository cloned to your device. Due to the differences between operating systems and SSH-based access methods (Personal SSH Keys and Access Keys), this guide should be read alongside the relevant SSH setup guide for your operating system.
Check that SSH is installed and the SSH Agent is started (see the relevant SSH setup guide for your operating system).
Create an SSH key pair for each account, such as:
ssh-keygen -t ed25519 -b 4096 -C "{username@emaildomain.com}" -f {ssh-key-name}
Add each private key to the SSH agent, such as:
ssh-add ~/.ssh/{ssh-key-name}
Add each private key to the SSH configuration, such as:
#bitbucket_username1 account Host bitbucket.org-bitbucket_username1 HostName bitbucket.org User git IdentityFile ~/.ssh/{ssh-key-bitbucket_username1} IdentitiesOnly yes #bitbucket_username2 account Host bitbucket.org-bitbucket_username2 HostName bitbucket.org User git IdentityFile ~/.ssh/{ssh-key-bitbucket_username2} IdentitiesOnly yes
Where
bitbucket_username1
andbitbucket_username2
are the Bitbucket usernames of the two accounts the SSH keys were created for. Your Bitbucket username is listed under Bitbucket profile settings on your Bitbucket Personal settings page.Add the public keys to the corresponding accounts on Bitbucket Cloud (see the relevant SSH setup guide for your operating system).
Clone repositories or update the
git remote
for repositories already cloned.To clone a repository, use the
git clone
command with the updated hostbitbucket.org-{bitbucket_username}
, such as:git clone git@bitbucket.org-{bitbucket_username}:{workspace}/{repo}.git
To update the Git remote for repositories already cloned, run the
git remote
command from within the repository with the updated hostbitbucket.org-{bitbucket_username}
, such as:git remote set-url origin git@bitbucket.org-{bitbucket_username}:{workspace}/{repo}.git
Update the Git configuration for the repository to the relevant user by navigating into the relevant repository and updating the Git configuration, such as:
git config user.name "{bitbucket_username}" git config user.email "{user@example.com}"
Running
git config
without the--global
option will set the user details for an individual repository, rather than user-wide.
Was this helpful?