0%

Working with multiple GitHub accounts

Suppose we have 2 github IDs, working account userA and personal account userB. This blog shows how to setup a git repo to commit & push with the proper identity.

SSH setups

For password-free github access, we use SSH (Secure Shell, basically a protocol for remote communication) to push to / pull from github repos. For identity verification, you will have a public key stored in github, and a private key stored locally. Some algorithm (RSA) will check if these two keys agree with each other.

For multiple github accounts, we need to generate the public/private key pair for each account:

1
2
3
4
5
6
7
ssh-keygen				# enter ~/.ssh/id_rsa_userA, <empty>, <empty>
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_userA

ssh-keygen # enter ~/.ssh/id_rsa_userB, <empty>, <empty>
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_userB

Then, add the generated public keys to github for each account:

1
2
cat ~/.ssh/id_rsa_userA.pub		# login to userA and add to the SSH key of userA
cat ~/.ssh/id_rsa_userB.pub # login to userA and add to the SSH key of userB

Finally, configure 2 target remote hosts in ~/.ssh/config. This is for specifying the identity file to use when pushing to / pulling from github with SSH:

1
2
3
4
5
6
7
8
9
Host github-userA
Hostname github.com
User git
IdentityFile ~/.ssh/id_rsa_userA

Host github-userB
Hostname github.com
User git
IdentityFile ~/.ssh/id_rsa_userB

Git configs

In your git repo, first setup the identity that will be displayed in commit messages:

1
2
git config user.name "userA" 
git config user.email "userA@example.com"

Then configure the identity for account verification when pushing to / pulling from github:

1
2
3
# suppose we have created somerepo under userA at github
git remote add origin github-userA:userA/<somerepo>.git
git push -u origin master

You are all set! Next time for this repo you can simply do git pull / git push. Have fun!