Skip to main content

Useful Links

Managing multiple SSH Keys

There are two ways, either via the ssh config or git config.

.ssh/config

In this config file you have a set of entries, each separated by a blank line. It is important to use IdentitiesOnly yes.

Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/<path-to-private-key>
IdentitiesOnly yes

Host work
HostName github.com
User git
IdentityFile "~/.ssh/<path-to-private-key-work>
IdentitiesOnly yes

Host personal
HostName github.com
User git
IdentityFile "~/.ssh/<path-to-private-key-personal>
IdentitiesOnly yes

It is important that the Host is unique. You can have two hosts pointing to the same host name, e.g. a GitHub account for work and a private one. In this case you have to clone the repos differently:

git clone git@work:username/my-work-project.git
git clone git@personal:username/my-personal-project.git

The downside of this approach is, that you have to remember different host names in case you use different private keys for the same host name. The second approach does not require that. In fact, it does not rely on the ssh config at all. But you might need a global config and

References:

.git/config

You basically work with the git config and don't touch the ssh config. Just open the .git/config file in your repo and specify the sshCommand prop in the core section.

[user]
name = John
email = john@gmail.com
[core]
sshCommand = "ssh -i ~/.ssh/<path-to-private-key>"
[remote "origin"]
url = git@github.com:john/ultimate-repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

However, you can also configure that on a more global or abstract level (see first reference).

References:

Resetting ssh-agent on macOS

sudo launchctl stop com.openssh.sshd
sudo launchctl start com.openssh.sshd

Access rights for private key

$ sudo chmod 600 <path-to-key>

More info

Terminal asks for password on push/pull

In this case you have the https url configured in your .git/config. Just replace https://<host> with git@<host>, you can find the exact string when you try to clone the repo from the UI.