Juggling Your Git Personalities: A Guide to Mastering Multiple GitHub Accounts
Tags: git, github, webdev, tutorial
Published: 2024-05-03 13:11:30

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:

```bash
cd ~/.ssh
```

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):

```bash
ssh-keygen -t rsa -C "weekendwarriorrick@yahoo.com" -f "WeekendWarriorRick"
ssh-keygen -t rsa -C "professionalpete@boringcorp.com" -f "ProfessionalPete"
```

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:

```bash
ssh-add -K ~/.ssh/WeekendWarriorRick
ssh-add -K ~/.ssh/ProfessionalPete 
```

---

## 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").

```bash
pbcopy < ~/.ssh/WeekendWarriorRick.pub
pbcopy < ~/.ssh/ProfessionalPete.pub 
```

- 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):

```bash
touch config
```

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

```bash
code config
```

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
```

---

## 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:

```bash
git clone git@github.com-WeekendWarriorRick:WeekendWarriorRick/MyWeekendProject.git
```

Remember:

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

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

# Repeat for ProfessionalPete with his info
```

---

## Bonus Tip:

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

```bash
# 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
```

Now you can push and pull like a pro using:

```bash
git push
git pull
```

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