How to use "git clone" with a custom SSH key

Sometimes you need to clone a Git repo with a different SSH key.

Ralph J. Smit Laravel Software Engineer

If you are a developer, you probably have worked with Git repositories. Usually, you get access to those repositories by adding an SSH-key on your device to the Git-provider (like GitHub or GitLab).

But what about situations where you have multiple employers or otherwise need multiple keys? Theoretically, you could use the same SSH-key for every Git-provider. However, I prefer to keep those keys separate. For example, if something happens to one key, you don't need to replace every key (which is very boring).

In this tutorial I'll show you how to use different SSH-keys for different Git-repositories.

How to use a custom SSH-key for Git-commands (quick solution)

When you execute an ssh-command, your computer will use the ~/.ssh/id_rsa key as the SSH-key. You can specify a custom key by using the -i path/to/custom/key flag.

In order to do so with Git, you can use the GIT_SSH_COMMAND to add a custom flag to the ssh command. In this example, I'm saying that I want to use the ~/.ssh/id_rsa_custom key to connect to this GitHub repository:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_custom" git clone git@github.com:user/repo.git your-folder-name
 
# Other commands without GIT_SSH_COMMAND-prefix, until you restart computer.
git fetch
git push origin main

The advantage of this approach is that it is quick & easy. The downside is that you will need to prepend this GIT_SSH_COMMAND="" part every time you restart your computer.

Luckily, there's another easier way, that will let you use Git as normal.

Adding your Git-repository as SSH-connection

The second option is a little bit more work, but it will save you much time in the long run. This option works by doing three things:

  1. Add an SSH connection to your ~/.ssh/config file.
  2. Specify the correct IdentityFile in your connection.
  3. Update the Git repository remote.

First, open up your ~/.ssh/config file and add a new SSH-connection. For example, if your host is "github.com" (the part after the "@" and before the ":" in "git@github.com:your_username/your_repo.git". This could also be something for GitLab, BitBucket, Beanstalk or another Git-provider.

Add the following line:

Host github_ssh_connection
HostName github.com
IdentityFile ~/.ssh/id_rsa_custom

Next, if you want to clone a repo, you can now do:

-git clone git@github.com:user/repo.git your-folder-name
+git clone git@github_ssh_connection:user/repo.git your-folder-name

If you already have cloned the repository and just want to update the Git-repository URL, you can do the following:

git remote set-url origin git@github_ssh_connection:user/repo.git
 
git remote -v
# Verify new remote URL
# origin git@github_ssh_connection:user/repo.git (fetch)
# origin git@github_ssh_connection:user/repo.git (push)

Conclusion

As you've seen, using a custom SSH-key for a Git-repository isn't difficult. You just need to know the right trick. I hope this was helpful to you and saved you some time!

If you want to get notified about similar articles, you can always subscribe to my newsletter.

Published by Ralph J. Smit on in Git .