In case you ever need 2 accounts on Github, there's a way to register multiple SSH keys so that you will always commit to the chosen repo using the appropriate account. This post was written because I had to combine methods from a few folks.
I'm writing this guide for Windows but the concept also applies to Linux, minus some syntax. Make sure you have Git âĨ 2.13
Step 1: Generate SSH keys
Open up git-bash
. Let's say you have a main account with an existing SSH key, your ~/.ssh folder should have the following:
$ ~/.ssh/id_rsa
$ ~/.ssh/id_rsa.pub
You can delete â remake them with another name to differentiate between account#1, #2, etc... Leaving them the same is fine too, but for the purpose of this post, I'll rename them to something else
Generate your 2nd SSH key:
ssh-keygen -t rsa -C "personal@email.com"
Rename it to something else when prompted. You should now have the following
$ ~/.ssh/id_rsa_work
$ ~/.ssh/id_rsa_work.pub
$ ~/.ssh/id_rsa_personal
$ ~/.ssh/id_rsa_personal.pub
Step 2: Generate/Edit config file:
This config file is there to differentiate between different accounts since Git âĨ 2.13. Let's generate and/or edit it:
cd ~/.ssh
touch config
nano config
Put this into your config:
#Your existing/1st account:
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_work
#2nd account
Host me.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal
The 2 main lines you need to pay attention to are the 1st and 4th line of each block.
- Host: {X}.github.com â The X will be different with each account you add. I chose
me
only because it's short and easy to remember. This can just begithub.com
if you have an existing account and don't want to edit it - IdentityFile â This points to your SSH key, remember to change this or you'll be very confused.
Step 3: Set the remote origin of the repo(s) according to your account
If the SSH link to your repo is git@github.com:USERNAME/REPO.git
To use the 1st account:
git remote set-url origin git@work.github.com:USERNAME/REPO.git
To use the 2nd account
git remote set-url origin git@personal.github.com:USERNAME/REPO.git
If the command returns a no origin
error, remember to add origin
first!
Step 4: User details:
Let's change dirs to your appropriate repo and set git config user details since you want to distinguish your accounts
cd ~/personal_repo
git config user.name "personal_user"
git config user.email "personal@email.com"
And... we should be done! The most important thing to remember is to set your working repos to match with your Host in the config
file.
Source(s):