Configure more than one git accounts locally.

gishan_abeysinghe

Gishan Chaminga Abeysinghe

Posted on November 13, 2021

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!!

💖 💪 🙅 🚩
gishan_abeysinghe
Gishan Chaminga Abeysinghe

Posted on November 13, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related