Let's say we need one GitHub account for our personal use, and another one for our work/professional use. How do we do this?
NOTICE: Git is [thankfully] very strict on telling the truth about the history, and what's explained here is also not an exception. This method is NOT a way of "impersonating" one account with another! Rather "multi-account" here means "being able to use multiple accounts on the same machine SEPARATELY". If you don't know what all these mean, no worries, just ignore this notice.
Here are the steps:
👉1. Create and edit
~/.ssh/config on you machine (On Windows, that would mean a path like
👉2. Add this to it:
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa Host github.com-personal HostName github.com User git IdentityFile ~/.ssh/id_rsa_personal
Explanation: Basically what this config is saying is very simple, if it ever sees the string
firstname.lastname@example.org in a repository address, it will use the
~/.ssh/id_rsa private key, and if it sees the string
email@example.com in a repository address, it will use the
~/.ssh/id_rsa_work private key. You'll see an example in the end.
👉3. As you see, we need two pairs of SSH keys (the ones which their private keys have been addressed by
IdentityFiles in the config snippet above). Now create these two pairs inside the
.ssh folder of your machine by
cding to it and running the below command twice:
cd ~/.ssh ssh-keygen -t rsa -b 4096 -C "your-work-email-address" # After running the below command for creating the personal-use # key pair, make sure to enter the correct name i.e. "id_rsa_personal", # similar to the one that we used in the config. ssh-keygen -t rsa -b 4096 -C "your-personal-email-address"
NOTICE: When prompted for passphrases, you may only hit Enter on your keyboard to skip it.
👉4. Now add the public key of each of these pairs to its corresponding GitHub profile settings. I.e., one goes into your personal GitHub account's profile settings, and the other one goes into your work/professional one's profile settings.
👉5. Now set the account that you use more as the global one for your machine's git account, and the one that you use less as the local one (on a per-project basis). E.g., I use the work account more, so:
git config --global user.name <work-username> git config --global user.email <work-email> cd <a-specific-personal-project> git config --local user.name <personal-username> git config --local user.email <personal-email>
If you like you can check and make sure all configs have been set properly:
git config --global --list git config --local --list
👉6. Now use
firstname.lastname@example.org for work/professional use, and
email@example.com for personal use. E.g.:
git remote add origin firstname.lastname@example.org:username/repo.git when in work repositories, and
git remote add origin email@example.com:username/repo.git when in personal repositories! 🤹♀️
You're also invited to join our small fledging Discord community called TIL (stands for "Today-I-Learned") where we share things we learn and articles we write in the realm of web programming with each other. Join us using this invite link.
Thanks for reading this article, you're very welcome to suggest any fixes or improvements in the comments section below.
Credit and Acknowledgment: Thanks Jeffery Way for writing a guide on this, here I've only tried to explain it in a different way, by putting it in my own words, and summarizing it a bit.)