Juggling Work and Side Projects Without GitHub Drama

So, you’re living the double life—full-time dev by day, side-hustle coding wizard by night. Managing multiple GitHub accounts can feel like having two secret identities. If you’ve ever accidentally committed work code with your personal account or vice versa, you know the pain. 😅

But don’t worry! With SSH keys and a little Git magic, you can seamlessly switch between accounts like a pro. Let’s set things up!


1. Generate SSH Keys for Each GitHub Account 🔑

Think of SSH keys as the ID badges for your GitHub accounts. You need separate badges for work and personal projects.

Open your terminal and run:

# Personal GitHub account
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/personal

# Work GitHub account
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/work

What’s Happening Here?

  • -t ed25519: Uses a secure encryption type (your digital badge).
  • -C "email": Tags the key with your email.
  • -f ~/.ssh/filename: Saves the keys separately.

Congrats! You now have two sets of SSH keys:

  • ~/.ssh/personal (for personal repos)
  • ~/.ssh/work (for work repos)

2. Configure SSH to Know Who’s Who 🎭

Now, let’s teach SSH when to use which key. Open the SSH config file:

sudo nano ~/.ssh/config

Add this:

# Personal GitHub account
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/personal

# Work GitHub account
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/work

Breakdown:

  • Host: Custom alias (makes switching easy).
  • IdentityFile: Tells SSH which key to use.
  • HostName: Points to GitHub.

Save the file (Ctrl + O, Enter, then Ctrl + X) and update permissions:

chmod 600 ~/.ssh/config

Now SSH knows when to wear the "personal" badge and when to wear the "work" badge. ✅


3. Add Your SSH Keys to GitHub 🔗

You need to introduce these SSH keys to GitHub (so it lets you in).

Run:

cat ~/.ssh/personal.pub  # Copy this key for personal GitHub
cat ~/.ssh/work.pub      # Copy this key for work GitHub

Go to GitHub > Settings > SSH and GPG keys → Click New SSH key, paste it in, and save.

Repeat for both accounts. Now GitHub knows who you are! 🎉


4. Test Your SSH Connection 🛠️

Let’s confirm everything is working:

If you see "Hi username! You’ve successfully authenticated", you’re good to go!


5. Set Up Git to Use the Right Account 📝

Now, let’s make sure Git commits are using the correct account.

Step 1: Edit Your Global Git Config

Run:

sudo nano ~/.gitconfig

Add:

[includeIf "gitdir:~/personal/"]
    path = ~/.gitconfig-personal

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work

This tells Git to use different configs based on the project folder.

Step 2: Create Account-Specific Configs

For personal:

echo -e "[user]\n    name = Your Personal Name\n    email = [email protected]" > ~/.gitconfig-personal

For work:

echo -e "[user]\n    name = Your Work Name\n    email = [email protected]" > ~/.gitconfig-work

Now, when you’re in ~/personal/, Git uses [email protected], and in ~/work/, it switches to [email protected]. 🚀


6. Cloning and Pushing Without Headaches 🔄

Always use the right SSH alias when cloning repositories:

# Personal repos
git clone [email protected]:username/repo.git

# Work repos
git clone [email protected]:username/repo.git

Already cloned the repo? Update the remote:

git remote set-url origin [email protected]:username/repo.git

Now you’ll never push personal code to work or vice versa by mistake. 😎


7. Apply Configuration Locally

To apply the configuration for a specific repository, you can use the following command inside the project directory:

git config --local include.path ~/.gitconfig-personal

8. Troubleshooting Common Issues 🛑

SSH Connection Fails? ❌

Run:

ssh-add ~/.ssh/personal
ssh-add ~/.ssh/work

Test again: ssh -T [email protected]

Git Using the Wrong Email? 🤦‍♂️

Check:

git config user.email

Fix it:

git config user.email "[email protected]"

Now your commits will always be tied to the right account.


Conclusion 🎉

That’s it! Now you can switch between GitHub accounts effortlessly—just like balancing your 9-to-5 and your moonlight coding gigs. 🌙💼

No more “oops, wrong account” commits. Just smooth sailing between personal and work projects. Happy coding! 🚀