DEV Community

mg
mg

Posted on

Managing multiple GitHub SSH logins on a single machine

Do you have more than one account for GitHub, i.e. one for professional work dev and another for personal side projects?

If you're using SSH to clone repositories this might help...

Setting up your SSH keys

I won't copy the existing GitHub docs for generating keys


Once you've created the keys you should add them to your GitHub accounts, again their article would be better than mine...


Now that you've got at least 2 SSH keys you need to add some config to ~/.ssh/config, if that file doesn't exist yet you can create it.

In that file, you need to add an entry like this for each GitHub account

Host github.com
    Hostname github.com
    User git
    AddKeysToAgent yes
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_fizz
Enter fullscreen mode Exit fullscreen mode

For each one you need to change the Host and the IdentityFile. i.e. if you had a personal and a work account you could set it up like this:

Host github.com
    Hostname github.com
    User git
    AddKeysToAgent yes
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_fizz

Host work-github.com
    Hostname github.com
    User git
    AddKeysToAgent yes
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_buzz
Enter fullscreen mode Exit fullscreen mode

Now you've done that you should be able to be able to test you're connections like so:

$ ssh -T git@github.com
Hi <Personal GitHub Username>! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@work-github.com
Hi <Work GitHub Username>! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

Now you've done this you can clone repos like this:

### personal ###
$ git clone git@github.com:fizz/buzz.git


### work ###
$ git clone git@work-github.com:fizz/buzz.git
Enter fullscreen mode Exit fullscreen mode

Updating your Git config to manage this for you

You might want to set up you're gitconfig files to handle this for you. There are a few ways of doing this but I do it like this.
In my root .gitconfig file, I have this configured:

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig-work

[includeIf "gitdir:~/personal/"]
    path = ~/personal/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode

And then in my ~/personal/ and ~/work/ directory, I have gitconfig that is specific to each user. For example:

.gitconfig-personal

[user]
  email = buzz.fizz@personal.co
  name = mg
  username = buzz
Enter fullscreen mode Exit fullscreen mode

.gitconfig-work

[user]
  email = fizz.buzz@work.co
  name = mg
  username = fizz

[url "git@work-github.com:"]
  insteadOf = "git@github.com:"

Enter fullscreen mode Exit fullscreen mode

In the work based config I have a URL remapping.
So in you're work directory you no longer need to do $ git clone git@work-github.com:fizz/buzz.git you can do $ git clone git@github.com:fizz/buzz.git as you would when you normally clone something from GitHub.

(Thanks to @ccoveille for making me aware of this!)


I recently found out how to do it and thought I'd share, there might be better ways of doing it. Please let me know if there is!

Top comments (10)

Collapse
 
mbgeorge48 profile image
mg

Thanks! I agree about having .gitconfig that manages different config in different files. My setup uses them like this:

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig-work

[includeIf "gitdir:~/personal/"]
    path = ~/personal/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode

Then config for each is:

[user]
  email = fizz.buzz@work.co
  name = mg
  username = fizz
  signingkey = 111111

[commit]
  gpgsign = true
Enter fullscreen mode Exit fullscreen mode
[user]
  email = buzz.fizz@personal.co
  name = mg
  username = buzz
  signingkey = 222222

[commit]
  gpgsign = true
Enter fullscreen mode Exit fullscreen mode

I guess I could have included some info around that in my post, but thanks for pointing it out and raising awareness of it!

Collapse
 
ccoveille profile image
Christophe Colombier • Edited

if you want to keep using your ssh trick

you can add that in your ~/work/.gitconfig-work file

[url "git@work-github.com:"]
  insteadOf = "git@github.com:"
Enter fullscreen mode Exit fullscreen mode

Then your settings will be automatic

EDIT: fixed inverted settings

Collapse
 
mbgeorge48 profile image
mg

Just added it! Thanks!!

Thread Thread
 
ccoveille profile image
Christophe Colombier

Maybe you could edit the post accordingly

Thread Thread
 
mbgeorge48 profile image
mg

Good shout! Was just testing it and found that I needed to switch the URLs around so it looks like this

[url "git@work-github.com:"]
  insteadOf = "git@github.com:"
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
ccoveille profile image
Christophe Colombier • Edited

Lol indeed. That's the issue when coding replying on Dev.to comment from my couch in front of Netflix, 🤣

Collapse
 
ccoveille profile image
Christophe Colombier • Edited

This is interesting, but it has some caveats.

If you are working with tool such as gh (GitHub CLI), git-machete, or any tools that avoid you to use github.com thing

There is also an issue. You can face issues if you're cloning a project from work without using your work-git alias

Your trick relies on the ssh layer. So not git one, so if you clone with http:// for once and you have no git config setting to overload http to ssh you won't use the right one

I would recommend something else. Already documented by many people using conditional include in git config file

dev.to/arnellebalane/setting-up-mu...

dev.to/marcobiedermann/how-to-work...

dev.to/jacktt/multiple-git-configs...

Collapse
 
ccoveille profile image
Christophe Colombier

Hey @mbgeorge48 and anyone interested here

@paulund described another variation of the pattern you set up, and I humbly helped you to improve.

There is no .ssh/config changes to do with his technique

Collapse
 
jangelodev profile image
João Angelo

Hi mg,
Thanks for sharing

Collapse
 
saffiullahfahim profile image
Saffiullah Fahim

Thank share with us. It gives us more ideas to manage multiple GitHub Account.