Suppose we have 3 SSH keys
- ~/.ssh/id_rsa
- ~/.ssh/id_rsa_sky
- ~/.ssh/id_rsa_heart
Your SSH config would look like this:
$ cat ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
Host github.com-sky
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_sky
IdentitiesOnly yes
Host github.com-heart
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_heart
IdentitiesOnly yes
By default, git
would use the first SSH key that is configured with Host github.com
For those repositories that you want to use different SSH key, you need to change their git config:
$ cd /path/to/that/repository
$ cat .git/config
...
[remote "origin"]
url = [email protected]:[REPO-ORGANIZATION]/[REPO-NAME].git
fetch = +refs/heads/*:refs/remotes/origin/*
...
With this update, git
will attempt to use ~/.ssh/id_rsa_sky
to connect to github
The same goes with another github account/SSH key.