Tell git to use a specific SSH key for a repository permanently

hirusi profile image Ru Singh Originally published at ruppalsingh.com on ・1 min read

You can have git use a specific SSH key, per repo.

This is great if you have - or would like to use - different SSH keys for each clients. To limit exposure, I try and use separate GitHub/GitLab/BitBucket account + SSH key for each client.

If one key is compromised, none of your other work is exposed.

Show me the code

When in the desired repo on your terminal, configure by:

git config core.sshCommand "ssh -i ~/.ssh/id_ecdsa_clientX"

In this case, you’re directly configuring git.

What is git doing under the hood? It uses an SSH environment variable to execute commands based on your config.

The abstraction is great because:

  • You don’t need to mess with the environment variables on your shell.
  • It is a set-it-and-forget-it configuration. You do not need to supply the env variable to each git command.

Simple and elegant.

Introduced in git 2.10

Please note this works on git 2.10 + — released in late 2016.

You should be covered, but if you wish to check your currently installed version of git, run:

git --version

You can find the 2.10.0 release notes here.

Quote for convenience:

A new configuration variable core.sshCommand has been added to specify what value for GIT_SSH_COMMAND to use per repository.

Posted on Jan 14 by:

hirusi profile

Ru Singh


I en­joy 🌼 med­i­ta­tion, 🍵 tea, and 🎶 mu­sic. As a big fan of open-source and pri­vacy-friendly ser­vices, my web­site par­tic­i­pates on the IndieWeb.


markdown guide