Multiple SSH keys and Github users

Kernel Panic
Kernel Panic
Published in
2 min readOct 27, 2018

--

Git is a great tool. I’ve been using SVN and Mercurial but for me Git hit the spot. There are times I’m sitting comfortable in chair git pulling or git pushing feeling the flow and everything is working as expected. But sometimes I want to change git account to another — private one and there is the hitch. So how to use multiple github accounts in everyday work?

First solution

I can set global config for my private account

git config --global user.name "Your Name Here"
git config --global user.email your@email.com

And I can specify individual user.name and user.email to use in specific repo.

git config user.name "Your Personal Name Here"
git config user.email your-personal@email.com

It works over https but what about ssh? I want to use my keys for the auth process.

Second solution

In this option I assume that we have two ssh keys created and ready to use. If not, follow this step-by-step guide at help.github.com.

Back to the topic. I can make ssh config to handle it for me. First of all I need to create or edit ~/.ssh/config and configure two or more hosts in there.

# Personal Github
Host github-personal
HostName github.com
User personal
IdentityFile ~/.ssh/id_rsa_personal
# Work
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Later if I want to write personal code I have to provide address with changed hostname.

git clone git@github-personal:personal/personal.github.io.git

If I already have cloned repo and I want to change account without cloning it again, I change url in [remote “origin”] section in .git/config file of the repo, or simply

git remote set-url origin git@github-personal:personal/personal.github.io.git

And then I have to change user for this specific repo in case of commit user mismatch.

git config user.name "Your Personal Name Here"
git config user.email your-personal@email.com

That’s it

I’ve been using second solution with success for about a month now. Before that I’ve tried some other tools but there is always some kind of obstacle. Different system, different environment configuration or installation restrictions. The advantage of this approach is that it works as long as you are using git.

--

--