2 min read | by Jordi Prats
For Git users, the Secure Shell (SSH) protocol is the go-to choice for authenticating and communicating with remote repositories. How can we configure the correct SSH key to use?
To configure a single key (single account) we can just use or update the default SSH key:
~/.ssh/id_rsa or use
~/.ssh/config to specify a different SSH for github.com (or any other git repository) like so:
Host github.com Hostname github.com IdentityFile ~/.ssh/github IdentitiesOnly yes
With this configuration it would use the key located in
~/.ssh/github instead of the default one just for
When we have multiple accounts we might need a little more flexibility in order to choose for each repository which SSH key to use.
We can achieve this using the envionronment variable GIT_SSH_COMMAND.
We just need to specify the ssh command to use, so if we set the ssh key to use using the -i flag we would be able to select the appropriate SSH key to use for each git command. For example:
GIT_SSH_COMMAND="ssh -i ~/.ssh/account_A" git clone example
And use another key byt simply updating GIT_SSH_COMMAND:
GIT_SSH_COMMAND="ssh -i ~/.ssh/account_B " git push
Since Git version 2.10.0, you can also use the core.sshCommand setting. There's no more need to use the environment variable. Here's how you clone a repo and set this configuration at the same time.
We use configure this setting for each command with the -c option:
git clone -c "core.sshCommand=ssh -i ~/.ssh/account_C" email@example.com:example/example.git
Configure it globally:
git config --global core.sshCommand "ssh -i ~/.ssh/account_C"
We can set it up for each repository as following:
git config core.sshCommand "ssh -i ~/.ssh/account_C"
Since you are configuring a different ssh key per repository (aka a different username) you'll want to update the user.email for that repository as well:
git config firstname.lastname@example.org
Posted on 26/10/2023