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!!
Top comments (5)
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.
Yeah that's easy too
Very insightful, Thanks Gishan
Any way to push changes autimatically to more than one account? For example a personal and enterprise githubs?
for that I guess you need to use different origins