Managing Multiple GitHub Accounts on macOS: A Complete Guide
Learn how to seamlessly switch between personal and work GitHub accounts using SSH keys and Git's conditional configuration
Managing Multiple GitHub Accounts on macOS: A Complete Guide
If you're juggling personal projects and work repositories, you've probably faced the challenge of managing multiple GitHub accounts on one machine. Here's a complete, battle-tested setup that makes switching between accounts automatic and painless.
The Problem
Using Git with multiple GitHub accounts on the same machine typically causes these headaches:
- Accidentally committing to work repos with your personal email
- SSH authentication failures when Git tries the wrong key
- Manually switching credentials before every push
- Confusion about which account is active
The Solution: SSH Keys + Conditional Git Config
We'll combine two powerful features:
- SSH host aliases - Route different repos through different SSH keys
- Git's
includeIf- Automatically switch identity based on directory
This setup gives you:
- ✅ Automatic account switching based on where you're working
- ✅ No manual intervention needed
- ✅ Clear separation between personal and work contexts
- ✅ Zero chance of committing with the wrong email
Step 1: Generate Separate SSH Keys
Create dedicated Ed25519 keys for each account:
# Personal account key
ssh-keygen -t ed25519 -a 100 -C "your-personal@email.com" -f ~/.ssh/id_ed25519_personal
# Work account key
ssh-keygen -t ed25519 -a 100 -C "your-work@email.com" -f ~/.ssh/id_ed25519_workImportant: Use strong passphrases for both keys. macOS Keychain will remember them.
Step 2: Add Keys to SSH Agent and Keychain
# Start the SSH agent
eval "$(ssh-agent -s)"
# Add both keys to macOS Keychain
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work
# Verify both keys are loaded
ssh-add -lStep 3: Configure SSH with Host Aliases
Create or edit ~/.ssh/config:
nano ~/.ssh/configAdd this configuration:
# Personal GitHub Account
Host github.com
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
# Work GitHub Account
Host github.com-work
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
Critical detail: The IdentitiesOnly yes directive prevents SSH from trying all available keys, which would cause authentication failures.
Step 4: Add SSH Keys to GitHub
Copy your public keys:
# Copy personal key
cat ~/.ssh/id_ed25519_personal.pub | pbcopy- Go to GitHub Settings → SSH Keys
- Click "New SSH key"
- Add a descriptive title like "Mac Personal"
- Paste the key and save
Repeat for your work account:
# Copy work key
cat ~/.ssh/id_ed25519_work.pub | pbcopyLog into your work GitHub account and add this key the same way.
Step 5: Test Both Connections
# Test personal account
ssh -T git@github.com
# Expected: Hi <personal-username>! You've successfully authenticated...
# Test work account
ssh -T git@github.com-work
# Expected: Hi <work-username>! You've successfully authenticated...If both succeed, your SSH setup is perfect! 🎉
Step 6: Configure Git for Automatic Identity Switching
Edit your global Git config:
nano ~/.gitconfigYour existing config might look like this:
[user]
name = Your Name
email = personal@email.com
[core]
editor = vi
[push]
default = current
autoSetupRemote = true
[init]
defaultBranch = mainAdd this at the end (important - must be last):
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-workNow create the work-specific config:
nano ~/.gitconfig-workAdd your work configuration:
[user]
name = Your Work Name
email = work@company.com
[core]
editor = vi
sshCommand = ssh -i ~/.ssh/id_ed25519_work -o IdentitiesOnly=yes
[push]
default = current
autoSetupRemote = true
[init]
defaultBranch = mainStep 7: Create Work Directory
mkdir -p ~/workThis is where all your work repositories will live.
How It Works: The Directory Magic
Here's the key insight: includeIf only activates inside Git repositories.
# Just the work directory (not a repo yet)
cd ~/work
git config user.email
# Output: personal@email.com (default config)
# Inside a Git repository within ~/work/
cd ~/work/some-repo
git config user.email
# Output: work@company.com (work config applied!)This confused me at first, but it's actually perfect! The conditional config only applies when you're actually working in a repository, not just browsing directories.
Real-World Usage
Cloning Personal Repositories
Clone anywhere outside ~/work/ using the standard GitHub URL:
cd ~/Desktop
git clone git@github.com:username/personal-project.git
cd personal-project
git config user.email # ✅ Shows personal emailCloning Work Repositories
Clone into ~/work/ using the -work host alias:
cd ~/work
git clone git@github.com-work:company/project.git
cd project
git config user.email # ✅ Shows work emailPro tip: Notice the -work suffix in the clone URL. This tells SSH to use your work key.
Common Gotchas and Solutions
Issue 1: "Repository not found" with HTTPS URLs
If you try to clone with HTTPS instead of SSH:
git clone https://github.com/company/repo.git # ❌ Won't workSolution: Always use SSH URLs for proper account separation:
git clone git@github.com-work:company/repo.git # ✅ Works!Issue 2: Wrong Email in Commits
If you see the wrong email in git log, you likely cloned with the wrong host or outside the proper directory.
Solution: Check your remote URL:
git remote -vFor work repos, it should show git@github.com-work:...
If it's wrong, update it:
git remote set-url origin git@github.com-work:company/repo.gitIssue 3: includeIf Not Working
If the work config isn't being applied, verify:
- Trailing slash is present:
"gitdir:~/work/"not"gitdir:~/work" - includeIf is at the end of ~/.gitconfig
- You're inside a Git repository, not just the directory
Debug with:
cd ~/work/your-repo
git config --list --show-origin | grep user.emailThis shows which file is setting your email.
Verification Checklist
Before you start working, verify everything is set up correctly:
# ✅ SSH keys exist
ls ~/.ssh/id_ed25519_*
# ✅ Both connections work
ssh -T git@github.com
ssh -T git@github.com-work
# ✅ Personal email is default
cd ~/Desktop
mkdir test && cd test && git init
git config user.email # Should show personal email
cd .. && rm -rf test
# ✅ Work email activates in ~/work/
cd ~/work
mkdir test && cd test && git init
git config user.email # Should show work email
cd .. && rm -rf testWhy This Setup is Superior
Automatic switching: No need to remember which account you're using. The directory structure makes it obvious.
Fail-safe: It's nearly impossible to commit with the wrong email because the config switches automatically.
Clean separation: Personal and work contexts are physically separated in your filesystem.
SSH security: No password managers or credential helpers needed. SSH keys are more secure and easier to manage.
Future-proof: Works with any number of accounts. Just add more host aliases and includeIf rules.
Summary
- Generate separate SSH keys for each account
- Configure SSH with host aliases (
github.comandgithub.com-work) - Use Git's
includeIfto auto-switch config based on directory - Keep all work repos in
~/work/, everything else is personal - Clone work repos with
git@github.com-work:prefix
This setup has saved me countless headaches. No more "oops, I committed to the work repo with my personal email" moments. The automatic switching based on directory is elegant and foolproof.
Happy coding! 🚀
Have questions or run into issues? The most common problems are forgetting the -work suffix when cloning or missing the trailing slash in the includeIf directive.