DEV Community

Cover image for Multiple GitHub Accounts On One Machine - A Quick Guide πŸ€Ήβ€β™€οΈπŸš€
aderchox
aderchox

Posted on

Multiple GitHub Accounts On One Machine - A Quick Guide πŸ€Ήβ€β™€οΈπŸš€

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 C:\Users\<your-username>\.ssh\config).

πŸ‘‰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
Enter fullscreen mode Exit fullscreen mode

Explanation: Basically what this config is saying is very simple, if it ever sees the string git@github.com in a repository address, it will use the ~/.ssh/id_rsa private key, and if it sees the string git@github.com-work 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"
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

If you like you can check and make sure all configs have been set properly:

git config --global --list
git config --local --list
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰6. Now use git@github.com for work/professional use, and git@github.com-personal for personal use. E.g.: git remote add origin git@github.com:username/repo.git when in work repositories, and git remote add origin git@github.com-personal: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.)

Top comments (1)

Collapse
 
stakedesigner profile image
StakeDesigner

wow good