How to Run Multiple GitHub Accounts on One Machine (Mac)
SSH keys, conditional gitconfig, automatic URL rewriting; the complete setup for running personal and work GitHub accounts on one Mac without ever mixing up identities again.

Why I Wrote This Article?
Setting up multiple GitHub accounts on one machine should be a solved problem by now. It isn't! Well, atleast for me Every time I do it - new device, new job, new side project - I make the same mistakes. You'd think after years of this, the steps would be second nature. They're not.
So I wrote this down. Once and for all. This guide covers SSH key setup, SSH config, automatic identity switching and a few other life saving tricks. The URL rewriting trick that saves you from manually editing clone URLs is one of them. If you feel similar, this guide is for you. I hope it helps you as much as it has helped me.
Note: This guide is Mac-focused. I'll write a separate post for Windows later.
Prerequisites (The Obvious Stuff)
Before you start, make sure you have:
- Git installed (
git --versionto check) - Two GitHub accounts (personal + work) already created
- Terminal access (Terminal.app, iTerm2, Warp, etc.)
- Basic familiarity with the command line
The Problem
If you're using two GitHub accounts without proper SSH configuration, Git defaults to a single identity. That means:
- Commits get attributed to the wrong account
- Push/pull operations fail with permission errors
- You end up in a cycle of re-authenticating every time you switch contexts
The clean solution involves three things:
- Separate SSH keys : one per GitHub account
- SSH config aliases : so SSH picks the right key automatically
- Automatic Git identity : so you never forget to set
user.name/user.email
Once configured, everything routes automatically. No switching, no remembering.
Step 1: Generate SSH Keys
Your Mac likely already has a default SSH key (id_ed25519 or id_rsa) : this is the key generated when you first set up SSH. We'll treat that as your work key and generate a new one for personal use.
1ssh-keygen -t ed25519 -C "your_personal_email@example.com"When prompted for the file location, save it with a distinct name:
1/Users/yourusername/.ssh/id_ed25519_personalAfter this, you should have two key pairs in ~/.ssh/:
| File | Purpose |
|---|---|
id_ed25519 | Work account (default) |
id_ed25519.pub | Work public key |
id_ed25519_personal | Personal account |
id_ed25519_personal.pub | Personal public key |
Step 2: Add Keys to SSH Agent
Load both keys into the SSH agent so macOS remembers them across sessions:
1eval "$(ssh-agent -s)"
2ssh-add --apple-use-keychain ~/.ssh/id_ed25519
3ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personalThe --apple-use-keychain flag stores the passphrase in macOS Keychain, so you don't have to type it every time.
Step 3: Configure SSH
Edit (or create) ~/.ssh/config:
1nano ~/.ssh/configAdd the following:
1# Work GitHub
2Host github-work
3 HostName github.com
4 User git
5 IdentityFile ~/.ssh/id_ed25519
6 IdentitiesOnly yes
7 UseKeychain yes
8 AddKeysToAgent yes
9
10# Personal GitHub
11Host github-personal
12 HostName github.com
13 User git
14 IdentityFile ~/.ssh/id_ed25519_personal
15 IdentitiesOnly yes
16 UseKeychain yes
17 AddKeysToAgent yesImportant: IdentitiesOnly yes is critical. Without it, the SSH agent may offer all loaded keys to GitHub, and GitHub accepts whichever key matches first : which could be the wrong account. This flag forces SSH to use only the specified IdentityFile.
Step 4: Add Public Keys to GitHub
Copy each public key:
1# Personal key
2cat ~/.ssh/id_ed25519_personal.pub | pbcopy
3
4# Work key (if not already added)
5cat ~/.ssh/id_ed25519.pub | pbcopyThen for each account:
- Go to GitHub >> Settings >> SSH and GPG keys >> New SSH key
- Name: Anything you like.
- Select Authentication Key
- Paste the corresponding public key and save
Step 5: Automatic Identity with Conditional Includes
This is the part I wish I'd set up from the start. Instead of manually running git config user.email in every repo (and inevitably forgetting), you can tell Git to automatically set your identity based on the directory.
First, organize your repos into separate directories:
1~/Repositories/Personal/ # All personal repos go here
2~/Repositories/Work/ # All work repos go hereThe global ~/.gitconfig keeps the work identity as default and conditionally overrides it for personal repos:
1[user]
2 name = username-1
3 email = [EMAIL_ADDRESS_1]
4
5[includeIf "gitdir:~/Repositories/Personal/"]
6 path = ~/.gitconfig-personalCreate ~/.gitconfig-personal:
1[user]
2 name = username-2
3 email = [EMAIL_ADDRESS_2]Now any repo inside ~/Repositories/Personal/ automatically uses my personal identity. Everything else defaults to the company identity. No more forgotten git config commands. This would have saved me from that embarrassing week of pushing to a company repo with personal email.
Step 6: Auto-Rewrite Clone URLs
Here's a trick that saves you from manually editing clone URLs every time. When you copy a clone URL from GitHub, it looks like:
1git@github.com:kXborg/tinyML.gitBut with our SSH config, you need to change github.com to github-personal or github-work. Doing this manually every time is tedious and easy to forget.
The fix : add URL rewrite rules using insteadOf. In my case, the global ~/.gitconfig defaults to the work SSH alias:
1[url "git@github-work:"]
2 insteadOf = git@github.com:And in ~/.gitconfig-personal (which only applies inside ~/Repositories/Personal/), override the rewrite to use the personal alias:
1[user]
2 name = kXborg
3 email = kukilp213@gmail.com
4
5[url "git@github-personal:"]
6 insteadOf = git@github.com:Now when you clone inside ~/Repositories/Personal/:
1git clone git@github.com:kXborg/tinyML.git
2# >> Git rewrites to git@github-personal:kXborg/tinyML.git ✅And when you clone anywhere else (work by default):
1git clone git@github.com:workID/some-project.git
2# >> Git rewrites to git@github-work:workID/some-project.git ✅No manual URL editing. Fully automatic.
Step 7: Clone Repos Using the Right Host
With URL rewriting from Step 6, you can just clone normally. Git handles everything behind the scenes:
1# Inside ~/Repositories/Personal/
2git clone git@github.com:kXborg/tinyML.git
3# >> Rewrites to git@github-personal:username-2/tinyML.git ✅
4
5# Inside ~/Repositories/Work/
6git clone git@github.com:workID/some-project.git
7# >> Rewrites to git@github-work:workID/some-project.git ✅If you prefer to be explicit (or haven't set up URL rewriting), use the SSH alias directly:
1git clone git@github-personal:kXborg/tinyML.git
2git clone git@github-work:workID/some-project.gitFor existing repos that were cloned with github.com, update the remote:
1git remote set-url origin git@github-personal:kXborg/orbital.gitVerification
Test both SSH connections:
1ssh -T git@github-work
2ssh -T git@github-personalExpected output for each:
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
Each should show the corresponding GitHub username. If you see the wrong username, double-check your SSH config's IdentityFile paths and make sure IdentitiesOnly yes is set.
Verify your Git identity in a repo:
1cd ~/Downloads/Repositories/Work/some-repo
2git config user.email
3# Should output: 'email_1'
4
5cd ~/Downloads/Repositories/Personal/orbital
6git config user.email
7# Should output: 'email_2'Common Pitfalls
| Problem | Cause | Fix |
|---|---|---|
| Commits show wrong email | Forgot git config user.email in repo | Use conditional includes (Step 5) |
Permission denied (publickey) | SSH using wrong key | Add IdentitiesOnly yes to SSH config |
| Clone URL requires manual editing | Using github.com instead of alias | Use URL rewriting (Step 6) |
| Keychain keeps asking for passphrase | Missing UseKeychain / AddKeysToAgent | Add both to SSH config |
| Cloned with HTTPS instead of SSH | GitHub defaults to HTTPS URL | Use the SSH tab when copying clone URL |
Quick Reference Commands Table
1# Generate a new SSH key
2ssh-keygen -t ed25519 -C "email@example.com"
3
4# Add key to SSH agent (macOS)
5ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal
6
7# Test SSH connection
8ssh -T git@github-personal
9
10# Check current Git identity
11git config user.name && git config user.email
12
13# Update remote URL for existing repo
14git remote set-url origin git@github-personal:user/repo.gitFinal Thoughts
Managing multiple GitHub accounts sounds like a 5 minute task, but the devil is in the details : wrong identities, permission errors, and clone URLs that need manual editing. With SSH aliases, conditional gitconfig includes, and URL rewriting, the workflow becomes truly hands-off.
Set it up once. Never think about it again.
← Previous Post
Slicer Settings Deep Dive & Surviving Multi-Day Prints on the Ender 3 V2 Neo
Next Post →
YOLOv26 Breakdown: NMS-Free Detection Meets LLM-Inspired Training
If the article helped you in some way, consider giving it a like. This will mean a lot to me. You can download the code related to the post using the download button below.
If you see any bug, have a question for me, or would like to provide feedback, please drop a comment below.