How To Manage Multiple Accounts On Github
Why Would I Need This?
Sometimes you have multiple accounts on github. For example personal account, and work account.
If that is the case, you might be in trouble pushing your code to github. Github doesn’t let you use the same ssh key on two accounts. So in order to use two accounts you need to have two keys.
But then the question is how do you tell ssh which one to use when?
How Would I do It?
Create keys
First of course, you need to create multiple keys
ssh-keygen -t ed25519 -f ~/.ssh/personal
ssh-keygen -t ed25519 -f ~/.ssh/work
Please note that recent OS on Mac doesn’t accepts lower encryptions, so better to use ed25519 to be safe. This step should give you four files under ~/.ssh/:
personal
personal.pub
work
work.pub
Now that you have the keys, you need to tell ssh to use them.
ssh-add ~/.ssh/personal
ssh-add ~/.ssh/work
Now, you need to add the public keys to github.
SSH Configuration
Now that ssh knows about the keys, we should configure which host uses which key.
This is a little tricky, because the host is always going to be github!
Edit ~/.ssh/config, or create one if it doesn’t exist. Add these lines:
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/work
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/personal
By doing this we give pseudo host names to our accounts so that we can differentiate them.
To test if we have done a good job, run this command:
ssh -v git@github.com-personal
You will see a long output. To make sure it worked, close to the end of the output, you should see
Hi {yourusername}! You've successfully authenticated, but GitHub does not provide shell access.
If you see this, it means that we have been successful in configuring ssh.
Otherwise, you need to troubleshoot what you missed in process.
Git Origin
Now only one thing remains to do: let git know about our pseudo domain names.
We cannot simply git clone git@github.com:user/project.git we need our pseudo domain names somewhere in there!
We need to replace the git@github part with github.com-personal or github.com-work respectively. For example github.com-personal:user/project.git
Conclusion
We went through how we can configure multiple git account to use ssh communication in the same machine.