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.

  1. Check that SSH is installed and the SSH Agent is started (see the relevant SSH setup guide for your operating system).

  2. Create an SSH key pair for each account, such as:

    1 ssh-keygen -t ed25519 -b 4096 -C "{username@emaildomain.com}" -f {ssh-key-name}
  3. Add each private key to the SSH agent, such as:

    1 ssh-add ~/{ssh-key-name}
  4. Add each private key to the SSH configuration, such as:

    1 2 3 4 5 6 7 8 9 10 11 12 13 #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 and bitbucket_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.

  5. Add the public keys to the corresponding accounts on Bitbucket Cloud (see the relevant SSH setup guide for your operating system).

  6. Clone repositories or update the git remote for repositories already cloned.

    • To clone a repository, use the git clone command with the updated host bitbucket.org-{bitbucket_username}, such as:

      1 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 host bitbucket.org-{bitbucket_username}, such as:

      1 git remote set-url origin git@bitbucket.org-{bitbucket_username}:{workspace}/{repo}.git
  7. Update the Git configuration for the repository to the relevant user by navigating into the relevant repository and updating the Git configuration, such as:

    1 2 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.

Additional Help