Skip to content

Instantly share code, notes, and snippets.

@tuannh99
Last active September 25, 2019 06:01
Show Gist options
  • Save tuannh99/0d54e02d9be2498ae0859070331625b9 to your computer and use it in GitHub Desktop.
Save tuannh99/0d54e02d9be2498ae0859070331625b9 to your computer and use it in GitHub Desktop.
Multiple SSH keys for different github accounts

Multiple SSH Keys config for different github accounts

SSH keys

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment