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 | ssh-keygen # enter ~/.ssh/id_rsa_userA, <empty>, <empty> |
Then, add the generated public keys to github for each account:
1 | cat ~/.ssh/id_rsa_userA.pub # login to userA and add to the SSH key of userA |
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 | Host github-userA |
Git configs
In your git repo, first setup the identity that will be displayed in commit messages:
1 | git config user.name "userA" |
Then configure the identity for account verification when pushing to / pulling from github:
1 | # suppose we have created somerepo under userA at github |
You are all set! Next time for this repo you can simply do git pull
/ git push
. Have fun!