How to use a different SSH credential for a specific git repository
Edit on GitHubIf you have multiple SSH keys and want to use a specific one for a particular Git repository, you can do so by configuring it on the SSH config file:
# ~/.ssh/config
Host gitlab.foo-repo
HostName gitlab.com
IdentitiesOnly yes
IdentityFile ~/.ssh/gitlab_rsa.foo-repo
Here we have two key points:
- The
Host
line is the name of the repository. It doesn’t need to be a real hostname, but it needs to be unique. The trick here is to use that domain as the git URL domain. Later theHostName
line is the actual hostname of the repository, likegit@gitlab.foo-repo:user/repo.git
. This is the one that will be used to connect to the server. - SSH by defult will try to use all the keys available in the
~/.ssh
folder, starting by the default key (~/.ssh/id_rsa
) and then the rest of the keys. So, if you already have another key for the same server, it will try to connect with that key first. The trick here is to use theIdentitiesOnly yes
line, so it will only use the key specified in theIdentityFile
line.
Written on April 25, 2025