Friday, December 1, 2017

Configuring multiple SSH keys for Git projects in Bitbucket.


#Scenario
Let's say you have a corporate account associated to your private organization projects (repositories), but also have your personal account to work in two different workstations.

When you try to use a repository which do not use your "default" ssh key, you will get a message like:

even if you already added a second key for it.

What happens is that, since we are working with the same service/domain (bitbucket), only one key will be used for project referring that hostname.

So, in order to work with different projects from different accounts, we could setup an "alias" in a configuration file into our ssh folder:

# ~/.ssh/config

Host bitbucket.org
  Hostname bitbucket.org
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/<keyname>

Host <domain-alias>
  Hostname bitbucket.org
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/<keyname>


and use it into your git configuration:

# <repo-path>/.git/config

[remote "origin"]
    url = git@<domain-alias>:<username>/<projectname>.git
    fetch = +refs/heads/*:refs/remotes/origin/*


In this way you should be able to work projects of different accounts hosted in the same service.

Written in collaboration with John Benavides.