How to manage multiple GitHub accounts on a single machine using SSH
Blog
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
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"
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"
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
$ ssh-add ~/.ssh/id_work
and make sure both the keys are in your client list using
$ ssh-add -l
Note: Each of them will prompt for the password
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
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
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
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 [email protected]: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[email protected]: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.
Copyright ©2024 Preplaced.in
Preplaced Education Private Limited
Ibblur Village, Bangalore - 560103
GSTIN- 29AAKCP9555E1ZV