Gishan Chaminga Abeysinghe
Posted on November 13, 2021
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
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
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
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
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
Scenario 2 - New repo / Existing repo
git remote add origin git@github.com:work-account/project.git
change to
git remote add origin work:work-account/project.git
I hope this will make your life easier and happy version controlling.
cheers!!
Posted on November 13, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.