Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brianzelip/c2bd615cfc8a3334acd4 to your computer and use it in GitHub Desktop.
Save brianzelip/c2bd615cfc8a3334acd4 to your computer and use it in GitHub Desktop.
Configure multiple git accounts on same computer using ssh

Multiple SSH Keys settings for different github account

Create different public key

Create different ssh key according the article Generating SSH keys.

Example:

$ ssh-keygen -t rsa -C "[email protected]" -f id_rsa_personal

Or for your work account:

$ ssh-keygen -t rsa -C "[email protected]" -f id_rsa_work

For example, 2 keys created at:

~/.ssh/id_rsa_work
~/.ssh/id_rsa_personal

Then, add these two keys as following:

$ ssh-add ~/.ssh/id_rsa_work
$ ssh-add ~/.ssh/id_rsa_personal

You can delete all cached keys before:

$ ssh-add -D

Finally, check your saved keys:

$ ssh-add -l

Remember to add created keys to your Github account. This guide will help you.

Modify the ssh config

Open ~/.ssh/config file:

$ cd ~/.ssh/
$ touch config
$ vim config # please use your favorite editor

Then add something like:

# work account
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work

# personal account
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal

Finally, test your added hosts:

$ ssh -T [email protected]
Hi WorkUser! You've successfully authenticated, but GitHub does not provide shell access.

Or:

$ ssh -T [email protected]
Hi PersonalUser! You've successfully authenticated, but GitHub does not provide shell access.

Clone you repo and modify your Git config

Clone your repo using a configured host (in this case, "github.com-personal"):

    git clone [email protected]:repoowner/repo.git

Enter repository and modify git config:

$ git config user.name "personal"
$ git config user.email "[email protected]" 

Done!

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