Managing Multiple Git Identities on One Machine
Have you ever pushed a commit from your personal project only to realize it went out under your work email? Or worse, tried to clone a personal repo and got a "permission denied" error because your machine was using the wrong SSH key?
If you use separate GitHub accounts for work and personal stuff on the same laptop, this is a problem you will run into sooner or later. The fix isn't complicated, but there are a few gotchas that can waste your time if you don't know about them upfront.
This post walks through the full setup, from generating SSH keys to automatically switching git emails based on which folder you're working in.
Why You Need Separate SSH Keys
GitHub maps each SSH key to exactly one account. You cannot add the same key to two different accounts. If your personal key is already added to your work GitHub account, SSH will authenticate as your work user even when you intended to use your personal one.
So the first step is generating a separate key for each account.
# Work key (default)
ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519
# Personal key
ssh-keygen -t ed25519 -C "you@personal.com" -f ~/.ssh/id_ed25519_personal
Use descriptive file names so you can tell them apart later.
Adding Keys to GitHub
Copy each public key and add it to the matching GitHub account under Settings > SSH and GPG keys > New SSH key:
cat ~/.ssh/id_ed25519.pub # Add to work GitHub account
cat ~/.ssh/id_ed25519_personal.pub # Add to personal GitHub account
Each key must be on exactly one account. If the same key appears on both, GitHub always resolves to whichever account was added first.
Loading Keys into the SSH Agent
ssh-add ~/.ssh/id_ed25519
ssh-add ~/.ssh/id_ed25519_personal
On macOS, you can add AddKeysToAgent yes and UseKeychain yes in your SSH config (next step) so keys persist across reboots.
Setting Up SSH Host Aliases
This is the part that trips most people up.
You cannot have two Host github.com blocks in your SSH config. SSH uses the first match only and ignores the rest. The solution is to create a host alias for your second account.
Edit ~/.ssh/config:
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host github-personal
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
github-personal is just a label. SSH resolves it to github.com through the HostName directive, but picks the key from the matching Host block. This is the only reliable way to route different keys to the same server.
Now verify both keys authenticate to the correct accounts:
ssh -T git@github.com # Hi work-user!
ssh -T git@github-personal # Hi personal-user!
If you see the right username for each, you're good.
Using the Alias in Git Remote URLs
For personal repos, use github-personal instead of github.com in your remote URLs:
# Cloning a personal repo
git clone git@github-personal:personal-user/repo.git
# Updating an existing repo's remote
git remote set-url origin git@github-personal:personal-user/repo.git
Work repos continue using git@github.com: as normal. No changes needed there.
Automatic Git Email Switching
The SSH setup handles authentication, but your commits still need the right email attached to them. You don't want to manually set user.email every time you switch between work and personal projects.
Git has a feature called includeIf that lets you apply different configs based on the directory you're working in.
In your ~/.gitconfig:
[user]
name = Your Name
email = work@company.com
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig-personal
Then create ~/.gitconfig-personal:
[user]
email = you@personal.com
Now any repo inside ~/personal/ automatically uses your personal email, and everything else defaults to your work email.
When Does This Matter?
This setup is useful when you're:
- Maintaining work and personal GitHub accounts on the same machine
- Making sure commits carry the correct email for each organization
- Debugging "permission denied" or "wrong account" errors during push/pull
Wrapping Up
Once you have this set up, you don't have to think about it again. SSH routes to the right key based on the host alias, and git picks up the right email based on your project directory. The whole thing runs quietly in the background.
If you have any questions feel free to reach out to me on LinkedIn.