(TIL) Git: Specify the ssh key to use

less than 1 minute read

The following methods will tell git which private key to use.

Environment variable GIT_SSH_COMMAND

For Git version 2.3.0+, you can use the environment variable GIT_SSH_COMMAND:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example

Note that -i can sometimes be overridden by your config file, in which case you should give SSH an empty config file:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone example

Configuration parameter core.sshCommand

For Git version 2.10.0+, you can configure this per repo or globally, so you don’t have to set the environment variable:

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"

~/.ssh/config

You can also specify the private key using the ~/.ssh/config file:

Host github.com
  User git
  Hostname github.com
  IdentityFile ~/.ssh/id_rsa

Via SuperUser.

Tags: ,

Categories:

Updated:

Comments