DEV Community

Cover image for How to Manage Multiple GitHub Accounts on macOS (Like an IT Moonlighter) 🌙💻
Shyam Tala
Shyam Tala

Posted on

How to Manage Multiple GitHub Accounts on macOS (Like an IT Moonlighter) 🌙💻

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 "personal@email.com" -f ~/.ssh/personal

# Work GitHub account
ssh-keygen -t ed25519 -C "work@email.com" -f ~/.ssh/work
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

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:

ssh -T git@github.com-personal
ssh -T git@github.com-work
Enter fullscreen mode Exit fullscreen mode

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

Add:

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

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work
Enter fullscreen mode Exit fullscreen mode

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 = personal@email.com" > ~/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode

For work:

echo -e "[user]\n    name = Your Work Name\n    email = work@email.com" > ~/.gitconfig-work
Enter fullscreen mode Exit fullscreen mode

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


6. Cloning and Pushing Without Headaches 🔄

Always use the right SSH alias when cloning repositories:

# Personal repos
git clone git@github.com-personal:username/repo.git

# Work repos
git clone git@github.com-work:username/repo.git
Enter fullscreen mode Exit fullscreen mode

Already cloned the repo? Update the remote:

git remote set-url origin git@github.com-personal:username/repo.git
Enter fullscreen mode Exit fullscreen mode

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

8. Troubleshooting Common Issues 🛑

SSH Connection Fails? ❌

Run:

ssh-add ~/.ssh/personal
ssh-add ~/.ssh/work
Enter fullscreen mode Exit fullscreen mode

Test again: ssh -T git@github.com-personal

Git Using the Wrong Email? 🤦‍♂️

Check:

git config user.email
Enter fullscreen mode Exit fullscreen mode

Fix it:

git config user.email "correct@email.com"
Enter fullscreen mode Exit fullscreen mode

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! 🚀

Top comments (0)