Tech Chorus

Map SSH Keys To Git Projects

written by Sudheer Satyanarayana on 2021-11-29

Using Git with SSH is a common practice among software developers. The convenience of not having to remember passwords is a huge productivity boost in software development workflows.

In a previous blog post, I wrote about managing SSH keys.

SSH Key Mapping

Having many SSH keys can cause few issues:

Git allows you to set the environment variable GIT_SSH_COMMAND. If you set this environment variable, Git uses the specified command for push and pull operations.

Example command:

export GIT_SSH_COMMAND='ssh -i /home/myuser/.ssh/id_xxx -o IdentitiesOnly=yes'

If you set an environment variable like this and then use git pull or git push, Git will use the identity id_xxx.

IdentitiesOnly=yes instructs SSH client to use the explicitly defined identity id_xxx.

Take this one step further and add the above export command in a shell script. Invoke the script via source command to load the SSH identity you want to use before git pull or git push.

File path: /home/myuser/bin/my-git-identity.sh

export GIT_SSH_COMMAND='ssh -i /home/myuser/.ssh/id_xxx -o IdentitiesOnly=yes'

The next time you have to use this specific SSH identity for git pull, invoke it:

source /home/myuser/bin/my-git-identity.sh
Tags: ssh git Linux command line source code management shell