DEV Community

Cover image for Two github accounts one laptop
Sumit Roy
Sumit Roy

Posted on • Updated on

Two github accounts one laptop

Problem Statement

A unique problem arises when my personal laptop left this world and even after lots of software and hardware fixes, I was unable to fix it. Now I was left with my office laptop and sorrow.

So basically my personal github account had my personal laptop's ssh key and work's github account had my office's laptop's ssh key. Now I needed to access my personal github account with my office's laptop. But but but..... you can add one ssh key in one github account only. So what to do now? How can I manage both the accounts with same laptop.


There's a very easy and interesting solution to this problem. I will explain it step by step.

Step 1:-

Add a new ssh key pair to your office's laptop

ssh-keygen -t rsa -f ~/.ssh/id_rsa.home
ssh-keygen -t rsa -f ~/.ssh/
Enter fullscreen mode Exit fullscreen mode

This will generate two ssh key pairs with different names for identifying keys to be used for personal github account and official account.
Okay, now how to configure which key to use for which account?

Step 2:-

Now is the time to edit ssh config file so that it can pick ssh key according to the server it connects to

# Personal GitHub account:
  User git
  IdentityFile ~/.ssh/id_rsa.home
  PreferredAuthentications publickey
  PasswordAuthentication no
  IdentitiesOnly yes

# Work GitHub account:
  User git
  IdentityFile ~/.ssh/
  PreferredAuthentications publickey
  PasswordAuthentication no
  IdentitiesOnly yes

Enter fullscreen mode Exit fullscreen mode

Lines starting from "#" are considered as comments as usual. And then few key value pairs of hostname, user, file to be used for ssh and few extra settings.
There's a catch also. While adding remote url in local repo we have to modify the url according to the hostname we provided in the config file. So for e.g my remote url is

which is my perosnal github account this will become

But what about github username while commit??

Step 3:-

This we can handle by adding some alias for changing git config so that commit will have user email id according to account type(official and personal)

#git alias
alias gitmeHome='git config'
alias gitmeWork='git config'
Enter fullscreen mode Exit fullscreen mode

And voila! every time you need to switch realms just call this alias and you are ready to rule that one.

Points to be noted:-

  • I only demonstrated this for two git accounts but this can be applied to differentiate any two server or any number of servers. In fact for each we should have different ssh key so that even if one gets compromised we still have other safe.
  • For changing git config file we can change username also if that is necessary.
  • I am writing blog almost after more than 1 year, so pardon me for my mistakes.

I hope I clearly explained this topic, if you have any doubts please write in the comments below.

Blog reviewed by - Deepika Verma

Liked my post?

Top comments (5)

m0ntassar profile image
Montassar JDIDI • Edited

It will also be a good idea to create a new user on your laptop for personal projects.
I do that since I have many long-term contracts with different customers, keeping a customer data and config in a dedicated user account helps a lot

sroy8091 profile image
Sumit Roy

Yes, but you can't add one ssh key in two different github account. That was problem I am giving the solution of.

ltvan profile image
Van Ly

You can use git config per repository for username.

nirajdewangan profile image
Niraj Kumar Dewangan

Thank you the beautiful content.

How to push the code to different repository?
I am new to git.

pallabganguly profile image
Pallab Ganguly

This literally made my day today :D