How to use a Specific SSH Key for Git Clone


In some cases, you might need to use a specific SSH key when cloning a Git repository. This could be due to having multiple keys for different accounts or repositories, or because the key you need to use is not in the default location. Here's how you can specify a particular SSH key when running the git clone command.



This method requires Git 2.3.0 or later. If you're using an older version of Git, you might need to use an ssh-agent or modify your SSH config to specify the private key to use for a particular host.



The GIT_SSH_COMMAND environment variable allows you to define the ssh command that will be used during the Git operations that require SSH connections, such as git clonegit fetchgit pull or git push.

You can set this environment variable to include the -i option, which allows you to specify the path to the SSH private key that should be used.

Here's an example of how to use GIT_SSH_COMMAND when running git clone:

GIT_SSH_COMMAND="ssh -i /path/to/your/private/key" git clone

Replace /path/to/your/private/key with the path to your SSH private key, and replace with the SSH URL of the repository you want to clone.

In CircleCI this is typically ~/.ssh/<key_type>_<fingerprint>.


This command sets the GIT_SSH_COMMAND environment variable for the duration of the git clone command, causing Git to use the specified SSH key for authentication. After the git clone command completes, the GIT_SSH_COMMAND environment variable will no longer be set (unless it was set before you ran the command).


Additional Resources

Was this article helpful?
0 out of 1 found this helpful



Article is closed for comments.