DEV Community

Cover image for Configure more than one git accounts locally.
Gishan Chaminga Abeysinghe
Gishan Chaminga Abeysinghe

Posted on

Configure more than one git accounts locally.

After joining my first job they gave me a new mac mini, previously I used to work on my laptop but mac mini feels more convenient now. As you all know new company means you will get your new working email id. then you might need to use it create a new github account. (github is just for an example, this process valid for any git remote provider)

Then the real struggle starts. How to manage personal and work github accounts in the same machine.

First Step

Generate 2 ssh keys, means you have to have two public keys and two private keys in your .ssh folder
ex:- id_rsa_work id_rsa_personal id_rsa_work.pub id_rsa_personal.pub

ssh-keygen -t rsa
Enter fullscreen mode Exit fullscreen mode

Second Step

copy public ssh keys, work ssh goes to work github and personal ssh goes to personal github. so now you have added your public keys respectively.

pbcopy < ~/.ssh/id_rsa_work.pub
pbcopy < ~/.ssh/id_rsa_personal.pub
Enter fullscreen mode Exit fullscreen mode

Third Step

Now we have to create another file in .ssh folder called config and paste the code below

# Personal GitHub
Host personal
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_personal

# Work GitHub
Host work
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_work

Host *
  IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

Fourth Step

Scenario 1 - Cloning a repo

Suppose you want to clone a repo from your working github account.

git clone git@github.com:work-account/project.git
Enter fullscreen mode Exit fullscreen mode

you have to replace git@github.com with work. so it will look follows. actully work is the name you gave in the config file.

git clone work:work-account/project.git
Enter fullscreen mode Exit fullscreen mode

Scenario 2 - New repo / Existing repo

git remote add origin git@github.com:work-account/project.git
Enter fullscreen mode Exit fullscreen mode

change to

git remote add origin work:work-account/project.git
Enter fullscreen mode Exit fullscreen mode

I hope this will make your life easier and happy version controlling.

cheers!!

Top comments (5)

Collapse
 
rohanvilloth profile image
Rohan Villoth

I don't create new GitHub accounts these days, I just add the new work mail too to my current account. But yeah, your approach seems interesting. Would be useful for managing repos with another user's credentials too.

Collapse
 
gishan_abeysinghe profile image
Gishan Chaminga Abeysinghe

Yeah that's easy too

Collapse
 
poshitharavi profile image
Poshitha Ravindu

Very insightful, Thanks Gishan

Collapse
 
drainuzzo profile image
Simone

Any way to push changes autimatically to more than one account? For example a personal and enterprise githubs?

Collapse
 
gishan_abeysinghe profile image
Gishan Chaminga Abeysinghe

for that I guess you need to use different origins