blog name

Git - Use different keys for the same host

Description

Let's say that you are working on a project and you have to use a different SSH key (other than your personal one) on the same host, e.g. GitHub/GitLab/BitBucket. For example, you have your personal key and company key:

id_rsa
id_rsa_company

All you have to do is to edit the ssh config file: ~/.ssh/config and add the following config:

# Personal
Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa
# Company
Host gitlab.com-company
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa_company

Let's say that following GitLab repo you have to clone with the new key and the default clone URL looks  like this:

git@gitlab.com:[COMPANY]/[PROJECT].git

All you need to do is to update the host while cloning gitlab.com -> gitlab.com-company, so it will look like these:

git@gitlab.com-company:[COMPANY]/[PROJECT].git

After that, everything else is the same.