Use a different SSH key

Three keys with tags showing the logos for Github, Gitlab and git-scm

I like to use a dedicated SSH identity for different authenticators. For example I have a dedicated SSH key each for github and gitlab.

How do I use a different SSH key to clone a repository?

GIT_SSH_COMMAND='ssh -i ~/.ssh/github' git clone git@github.com:github/docs.git

How do I use a different SSH key for interacting with a remote?

You can set it each time

GIT_SSH_COMMAND='ssh -i ~/.ssh/github' git fetch

If you use the same key for all your interactions with the remote you can configure it

git config core.sshCommand 'ssh -i ~/.ssh/github'

The environment variable takes precedent so you can override the configured command if need be.

How do I…?

Is there something missing here? Let me know!