iTerm2 Profiles and Git Keys

If you have different key pairs that you use with different accounts (e.g. different companies, private etc.), when using git, by default git will use your first key pair from this command.

ssh-add -L

This could be a problem, because it may not be the key you want to use. For example, Bitbucket has a limit where the same private key cannot be used with 2 different accounts. So if you have a separate/different company account you will need to create a new key pair.

You can specify a different key pair to use by setting GIT_SSH_COMMAND.

Once you have created your new key pair, we are going to make use of it by default when starting a new profile window/tab in iTerm2. Add this line to your iTerm2 profile under ‘Send text at start’. Path is the path to your new private key.

export GIT_SSH_COMMAND='ssh -o IdentitiesOnly=yes -i ~/.ssh/path/to-new-key-pair/id_rsa -F /dev/null'

Open a new iTerm2 tab with that profile. When you run git commands in this tab, the specified key for that profile will be used.

e.g. running a git clone command using your new key

git clone git@some-host.org:some-org/some-repo.git