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.
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 pull or
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_SSH_COMMAND="ssh -i /path/to/your/private/key" git clone email@example.com:username/repo.git
/path/to/your/private/key with the path to your SSH private key, and replace
firstname.lastname@example.org:username/repo.git with the SSH URL of the repository you want to clone.
In CircleCI this is typically
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).