DEV Community

Cover image for How to manage multiple GitHub accounts on your machine using ssh
Akashdeep Patra
Akashdeep Patra

Posted on

How to manage multiple GitHub accounts on your machine using ssh

Like myself, if you are contributing professionally, chances are that you also have encountered this conundrum more than once, where you are using a single machine to work on your professional projects and personal ones at the same time but you have two different GitHub accounts for the same. And by default GitHub doesn't have a robust solution to handle this, (well not as of now at least). This is my way of getting over the problem and a way where you can save logging in and out multiple times (especially after GitHub mandates 2-factor auth).ps: Follow the below steps in the same order

Please note that I'm using a mac but for your respective systems the steps are almost the same especially if you have WSL on your Windows device

Create multiple ssh keys

Since Github has officially migrated to ssh as the desired auth system, we'll first need to create two different ssh keys(Refer to this link) with the personal and work email with appropriate passwords (I'm using RSA as the preferred algorithm but you can definitely choose anything check this link for more info).

ssh-keygen -t rsa -C "your_own_email@yopmail.com
Enter fullscreen mode Exit fullscreen mode

when prompted please name the key with something significant, in my case, I named it id_self

ssh-keygen -t rsa -C "your_work_email@yopmail.com
Enter fullscreen mode Exit fullscreen mode

in case of the company email name this differently than the personal account, this is necessary
in my case I named it id_work

Add the keys to your ssh client

Now both of my keys are located in the ssh directory
~/.ssh/id_self
~/.ssh/id_work
now let's add both of the keys to the ssh client

$ ssh-add ~/.ssh/id_self
Enter fullscreen mode Exit fullscreen mode
$ ssh-add ~/.ssh/id_work
Enter fullscreen mode Exit fullscreen mode

and make sure both the keys are in your client list using

$ ssh-add -l
Enter fullscreen mode Exit fullscreen mode

Note: Each of them will prompt for the password

Modify the ssh config

This config file will contain additional data that your ssh client can use, if you don't see a config file in your ssh directory please create one using

cd ~/.ssh/
$ touch config
Enter fullscreen mode Exit fullscreen mode

and open it inside any text editor.
Now add the following details to your config

#work account
Host github.com-work
    HostName github.com
    User git
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_work
#personal account
Host github.com-self
    HostName github.com
    User git
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_self

Enter fullscreen mode Exit fullscreen mode

Note: here as you can see in place of source host instead of GitHub I have added suffixes as "-work" & "-self", This doesn't necessarily have to be like this, just add any unique string for Identification

Configuration after cloning or creating any repo

After initializing or cloning any repo just open your .git/config file and change the origin or any remote URL from github.com to your target hostname. For example, I am working on a project for my workplace and the ssh URL for that is
git@github.com:my-company/work-project.git, now I changed the remote URL either directly opening the .git/config file or using git remote set-url command, now my changed URL is
git@github.com-work:my-company/work-project.git, remember we added a custom hostname in the ssh config earlier ? that is used here as well, both these proxy URLs needs to be same for the ssh proxy to work, (You might need to enter the password once after you tried connecting to the remote repo)

Do the same thing for your personal projects as well, and you're good to go.

Top comments (0)