DEV Community

Cover image for Multiple GitHub accounts on one laptop
Simon Osipov
Simon Osipov

Posted on

Multiple GitHub accounts on one laptop

Imagine the next situation:

  • You have only one laptop
  • You have your personal GitHub account
  • Your employer stores code on GitHub as well
  • You need to commit your personal code to your personal repositories but also work code to the employer`s repositories.
  • You can`t do this from your personal account but do not want to create an additional one (with corporate email)

What to do in this situation? There is a way, how you can configure your laptop to commit to work repositories with work credentials, and to personal one with your personal one.

This solution is based on 2 aspects:

  • correcting SSH config
  • git URL re-writing

The main advantage of this approach is that it doesn't require any additional work to get it right. You will not need to change remote URLs or remember to clone things differently. The second part ( the URL rewriting) will take care of it.

First of all, let's correct our .ssh config. Assuming you have 2 ssh keys, your personal (github_personal) and your work (github_work). How to create ssh keys you can read in the GitHub docs.

~/.ssh/config

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github_personal

# Work GitHub
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/github_work

Host *
  AddKeysToAgent yes
Enter fullscreen mode Exit fullscreen mode

Both this configs have same user and domain, but we will take care about it later. Next - global git config.

~/.gitconfig

Here we need to add our default name and email (the one we used in ssh creation for our personal account)

[user]
    name = My Name
    email = personal@personal.email

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

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

What happens here? First, set our default name and email. Second, we point to use local .gitconfig file for all repositories located by mask ~/path/work_dir/. And the last, replace github.com (default account for Github) with the profile we set under github-work in .ssh/config.

The last part is modification of local .gitconfig for all our working repositories:

~/path/work_dir/.gitconfig
It is easy - just replace your email with your corporate one.

[user]
    email = work@work.email
Enter fullscreen mode Exit fullscreen mode

That is all! As long as you keep all your work repos under ~/path/work_dir/ and personal stuff elsewhere, git will use the correct SSH key when doing pulls/clones/pushes to the server, and it will also attach the correct email address to all of your commits.

How to check? Clone repository via SSH, cd to that folder and execute git config --get user.email

Top comments (12)

Collapse
 
bobbyiliev profile image
Bobby Iliev

This is super handy! Very well explained!

Collapse
 
osipovsimon profile image
Simon Osipov

Thanks a lot!

Collapse
 
anthonyjdella profile image
Anthony

Thanks. What if you have 1 using GitHub and 1 using GitLab?
I'm guessing it's the same principle.

Collapse
 
osipovsimon profile image
Simon Osipov

No difference, but you will need to replace
insteadOf part with url pattern from GitLab (assuming this one is work)

Collapse
 
mfurmaniuk profile image
Michael

Very cool, I'm still a command line type person and having this would be great when I want to do some personal stuff but keep it in my own repos.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

GUI clients like GitKraken also make this ridiculously easy

Collapse
 
osipovsimon profile image
Simon Osipov

IF you use GUI clients)
In my case, I am "one IDE" kinda guy, so I have terminal and VS Code. )

But thanks for the comment!

Collapse
 
harshitkumar31 profile image
Harshit Kumar

Works like a charm.

Collapse
 
jeff_codes profile image
Jeff Edmondson

Just make sure to change the remote "origin" url in the YOUR_PROJECT/.git/config file

[remote "origin"]
    url = git@github-work:... 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
detzam profile image
webstuff

okay. You could've explained it better but...
So what you're saying is: you make those ssh files and in each project you create a .gitconfig and when you go all git add . / git commit -m"smthing" git push origin smthing .... IT WILL go to the correct repos?
OR you have to chnage your config email and username for each add/pull/push?

Collapse
 
osipovsimon profile image
Simon Osipov • Edited

No, sir. Sorry for that, will try to explain better.
You do this once:

  • You modify global config for ssh, so you have separate accounts for work and personal
  • You create working dir, where you will store all working repos
  • You modify global git config
  • You modify git config in repository you created step above

If you do clone, pull, push inside this repo, you will use WORK account automatically (no need to switch or any other things, just git pull/clone/push

If you clone/push/pull outside this repo - it will use PERSONAL account

So you dont need to modify gitconfig for every working repo. as long as you have
~/path/work_dir/repo1
~/path/work_dir/repo2
~/path/work_dir/repo3

Collapse
 
mrdt12 profile image

thanks, I usualy change .git/config? to add multiple branch, like github and azure