DEV Community

Cover image for Juggling Your Git Personalities: A Guide to Mastering Multiple GitHub Accounts
Mirko
Mirko

Posted on • Updated on

Juggling Your Git Personalities: A Guide to Mastering Multiple GitHub Accounts

Ever feel like you're living a double life? By day, you're a mild-mannered coder, by night... a swashbuckling Git master with a secret lair (your basement) and multiple GitHub accounts! But juggling those accounts can be a pain, like trying to wrangle kittens while wearing oven mitts. Fear not, fellow hero, for this guide will show you how to manage your GitHub personas with the grace of a seasoned spy 🕵️.

The Scenario:
Let's say you have two GitHub identities: "WeekendWarriorRick" (because that's when all the coding magic happens) and "ProfessionalPete" (the responsible one who pays the bills). You want to seamlessly switch between them without causing any multidimensional paradoxes (or at least minimize them).


 Step 1: Forge Your Secret Weapons (SSH Keys)

First things first, we need some digital secret weapons: SSH keys. Imagine them as your Batarangs, but for secure communication with GitHub. Navigate to your .ssh folder (like a digital Batcave) using this handy command:

cd ~/.ssh
Enter fullscreen mode Exit fullscreen mode

Now, whip up some unique SSH keys for each account with this fancy code (replace the email addresses with your own, unless you want Rick to be fixing bugs for your boss):

ssh-keygen -t rsa -C "weekendwarriorrick@yahoo.com" -f "WeekendWarriorRick"
ssh-keygen -t rsa -C "professionalpete@boringcorp.com" -f "ProfessionalPete"
Enter fullscreen mode Exit fullscreen mode

Important Note: When prompted for a passphrase (like a password for your secret lair), you can leave it blank for convenience, but remember, with great power comes great responsibility (and the risk of anyone accessing your accounts).


 Step 2: Train Your Digital Butler (SSH Agent)

The SSH Agent is like your Alfred – it keeps track of your SSH keys so you don't have to juggle them yourself. To add your spanking new keys, use these commands:

ssh-add -K ~/.ssh/WeekendWarriorRick
ssh-add -K ~/.ssh/ProfessionalPete 
Enter fullscreen mode Exit fullscreen mode

 Step 3: Plant Your Flag on GitHub (Public Keys)

Now, head over to your GitHub accounts and add the public keys (the shiny counterparts of your private keys) like planting flags on conquered territories. You can find instructions on how to do this in GitHub's deepest, darkest secrets... I mean, documentation (it's probably under "SSH and GPG keys").

pbcopy < ~/.ssh/WeekendWarriorRick.pub
pbcopy < ~/.ssh/ProfessionalPete.pub 
Enter fullscreen mode Exit fullscreen mode
  • Sign in to Github Account
  • Go to Settings > SSH and GPG keys > New SSH Key
  • Paste your copied public key and give it a Title of your choice.

 Step 4: Craft Your Master Control Panel (SSH Config File)

The SSH config file is your mission control. Here, you'll set up aliases for each GitHub account so you can switch between them with ease. Create the file (if it doesn't exist) with a touch of your fingers (or this command):

touch config
Enter fullscreen mode Exit fullscreen mode

Then, open it in your preferred code editor (I use VSCode):

code config
Enter fullscreen mode Exit fullscreen mode

Now, paste this code snippet into the file, replacing the usernames with your actual ones:

# WeekendWarriorRick to the rescue!
Host github.com-WeekendWarriorRick
  HostName github.com
  User git
  IdentityFile ~/.ssh/WeekendWarriorRick

# ProfessionalPete swoops in!
Host github.com-ProfessionalPete
  HostName github.com
  User git
  IdentityFile ~/.ssh/ProfessionalPete
Enter fullscreen mode Exit fullscreen mode

 Step 5: Deploy Your Skills (Cloning Repositories)

Finally, the moment of truth! Let's clone a repository using your newfound power. Navigate to your desired directory and unleash this command, replacing the username and repository name with your target:

git clone git@github.com-WeekendWarriorRick:WeekendWarriorRick/MyWeekendProject.git
Enter fullscreen mode Exit fullscreen mode

Remember:

For existing repositories, you might need to set the correct user email and name:

git config user.email "weekendwarriorrick@yahoo.com"
git config user.name "Weekend Warrior Rick"

# Repeat for ProfessionalPete with his info
Enter fullscreen mode Exit fullscreen mode

Bonus Tip:

Add remotes with aliases to your repositories so you can push and pull with ease:

# git remote add origin git@{mySSHHostUser}:{githubUser}/{repositoryName}.git
git remote add origin git@github.com-WeekendWarriorRick:WeekendWarriorRick/MyWeekendProject.git

# Repeat for ProfessionalPete's remote
Enter fullscreen mode Exit fullscreen mode

Now you can push and pull like a pro using:

git push
git pull
Enter fullscreen mode Exit fullscreen mode

Congratulations 🥳, code warrior! You can now seamlessly switch between your GitHub accounts and conquer the digital world, one commit at a time.

Top comments (2)

Collapse
 
ccoveille profile image
Christophe Colombier • Edited

I have nothing against you, but I won't recommend using what you describe here.
Your tutorial is decent and looks like the ones, everyone seen multiple times.
It "works", but it comes with many issues.

Using different hosts for GitHub host has huge caveats, it doesn't integrate well with tools, either git machete or GitHub CLI, you can forget to do use the "fake host" you define.

I would recommend anyone reading one of these tutorials

@mbgeorge48 is using the classic includeIf and @paulund is using too but supersedes it by using sshCommand to overload the ssh key directly in git config.

Collapse
 
mirkosaugo profile image
Mirko

Thank u @ccoveille for the advice! I didn't know about that, I really appreciate ur suggestion.