2 Github Accounts 1 Computer - The Shortest Guide Possible
One thing that’s given me headaches for too long was switching between my two Github accounts - work and personal - on the same computer. Today I decided to put an end to it, and it was as simple as ABC. Seriously, look.
A - SSH Keys Generation
I’m sure most people who are acquainted with git are familiar with this procedure. So I’ll simply link to very good instructions and leave you to it.
Note: If you wish to be compatible with this guide, name your private keys id_rsa_git_personal
and id_rsa_git_work
.
B - SSH Config File
Create a new file in ~/.ssh/config
and pour all this content into it:
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_git_personal
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_git_work
You don’t need to replace anything so long as you used my recommended names from above.
C - Specifying Your Remotes
For each repository, edit its .git/config
file and make sure that:
- For personal-account repositories, the remote URL uses the SSH profile for the personal account, and your personal username:
[remote "origin"]
url = github.com-personal:<your_personal_user_name>/<your_repo_name>.git
- For work-account repositories, the remote URL uses the SSH profile for the work account, and your work username:
[remote "origin"]
url = github.com-work:<your_work_user_name>/<your_repo_name>.git
C’est tout. Now each repository knows which remote to connect to and which SSH key to use for authentication.
I can’t believe I had all those headaches.