Configure multiple SSH-keys for GIT on the same device

Sheriff Sanni
3 min readAug 10, 2020

--

Git Version Control Systems

If you are here, you most likely have come to a situation where you require more than one git account on your device. Not to worry, this can be resolved quickly.

My assumption for this specific scenario is:

  1. A personal Github account.
  2. A work Github account.
  3. A work BitBucket account.
  4. An Azure account

1. Create a new ssh-key and add it to the Personal GitHub account

a. Generate SSH key using this. Name the key personal_github not id_rsa.
b. Add it to your personal GitHub account using this.

2. Create a new ssh-key and add it to the Work GitHub account

a. Generate SSH key using this. Name the key work_github not id_rsa.
b. Add it to your personal GitHub account using this.

3. Create a new ssh-key and add it to the Work BitBucket account

a. Generate SSH key using this. Name the key work_bitbucket not id_rsa.
b. Add it to your work BitBucket account by scrolling to the section on Add the public key to your Account settings.

4. Create a new ssh-key and add it to the Azure account

a. Generate SSH key using this. Name the key work_azure not id_rsa.
b. Add it to your work Azure account using this.
c. Add your SSH private key to the ssh-agent and store your passphrase in the keychain. ssh-add -K ~/.ssh/work_github

5. Modify SSH Config File on your PC (~/.ssh/config)

Open up the file in any text editor of your choice or create it if it does not exist.

# Personal GitHub account
Host personal.github.com
HostName github.com
AddKeysToAgent yes
UseKeychain yes
User git
IdentityFile ~/.ssh/personal_github
# Work GitHub account
Host work.github.com
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/work_github
# Work BitBucket account
Host work.bitbucket.com
HostName bitbucket.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/work_bitbucket
#Work Azure
Host work.ssh.dev.azure.com
HostName ssh.dev.azure.com
AddKeysToAgent yes
UseKeychain yes
User git
IdentityFile ~/.ssh/work_azure

For the above, pay close attention to the Host, HostName and IdentityFile

# Host would be used in 6. below.
# HostName says which Git Version Control System is in use.
# IdentityFile specifies the SSH file in use.

6. Clone the project repo

To clone any repo now, we always need to modify the ssh command so as to select who we are cloning it as.

Let say below is the ssh command for cloning a project from GitHub & Bitbucket.

git@github.com:[GitHub Account || Group || Org]/[project].git
git@bitbucket.com:[Bitbucket Account || Group || Org]/[project].git
git@ssh.dev.azure.com:[Azure Account || Group || Org]/[project]

To Clone it using my personal GitHub account:

git@personal.github.com:[GitHub Account || Group]/[project].git

To Clone it using my work GitHub account:

git@work.github.com:[GitHub Account || Group]/[project].git

To Clone it using my work Bitbucket account:

git@work.bitbucket.com:[BitBucket Account || Group]/[project].git

To Clone it using my work Azure account:

git@work.ssh.dev.azure.com:[Azure Account || Group || Org]/[project]

Do not forget to add —> git clone before the commands above to actually clone the repos

The key element here is how the Host was named in the ~/.ssh/config file.

With this, I trust you should be able to tweak and configure any other SSH key which you might have.

5. Set the right user for your git repo

This is to ensure that your commits are tagged with the right username and email.

Navigate to your repo directory in terminal, then run the command below to confirm the username and email in use by the repo.

git config --list

If both are wrong, kingly follow the steps below:

git config --localgit config --local user.email [Email address you want to use]git config --local user.name [User name you want to use]

Thank you 💪

--

--