Multiple SSH Keys for GitHub: Fix the Wrong Key (IdentitiesOnly)
Use one Host alias per GitHub account in ~/.ssh/config, give each its own IdentityFile, and add IdentitiesOnly yes. Without that last line, SSH offers every key in your agent first, and GitHub authenticates you as whichever account owns the first key it recognizes.
Why does SSH use the wrong key for GitHub?
GitHub identifies you by your public key, not by a username. All accounts connect as git@github.com, so SSH has nothing to tell them apart. By default it offers the keys loaded in ssh-agent first, then the IdentityFile you configured. The first key GitHub accepts wins, and that key belongs to one account only.
That is how you get Permission denied to <other-account>, or worse, a push that succeeds as the wrong person. If you hit the error, our guide to git push permission denied on the wrong GitHub account walks through the diagnosis.
How do I set up multiple SSH keys for GitHub?
1. Create one key per account
ssh-keygen -t ed25519 -C "you@personal.dev" -f ~/.ssh/id_ed25519_personal
ssh-keygen -t ed25519 -C "you@acme.com" -f ~/.ssh/id_ed25519_acme
Add each public key (.pub) to the matching GitHub account under Settings, SSH and GPG keys.
2. Write one Host alias per account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host github-acme
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_acme
IdentitiesOnly yes
Host is the alias you type. HostName is the real server. Mixing them up is the most common mistake in this setup.
3. Point your remotes at the alias
git clone git@github-acme:acme-inc/billing.git
git remote set-url origin git@github-acme:acme-inc/billing.git
What does IdentitiesOnly do?
IdentitiesOnly yes tells SSH to use only the key named in IdentityFile for that host, and to ignore everything else the agent holds. It is the line that turns “usually the right key” into “always the right key”. Leave it out and your setup works until the day your agent holds a key GitHub also knows.
How do I check which account a key belongs to?
ssh -T git@github-personal
ssh -T git@github-acme
GitHub answers Hi <username>! You've successfully authenticated. If the username is wrong, the alias is using the wrong key. Add -v to see which keys SSH offered and in what order.
Does this fix everything?
No. SSH is one of four layers. The alias fixes push authentication, but your commit email still comes from git config, your gh CLI session is separate, and commit signing has its own key. A perfect SSH config can still produce commits under your personal email, which then show as unverified commits. Folder-based email rules help with that part, see git includeIf for a different email per folder, and the CLI layer is covered in gh auth switch and what it misses.
If you would rather not maintain four layers by hand, Hats binds a folder to a profile and switches the commit identity, SSH key, gh account and signing together, and blocks the push if you forgot. The full manual route is in multiple GitHub accounts on one Mac.
FAQ
Do I need ssh-agent with multiple keys?
No. With IdentitiesOnly yes and an IdentityFile per alias, SSH reads the right key directly. The agent is only useful to avoid retyping a passphrase.
Can I keep using github.com in my remote URLs?
Only for one account. For the others, the remote must use the alias, or SSH falls back to the default key selection.
Why does my push still go out as the wrong account?
Check the remote URL first with git remote -v. A remote that still says git@github.com bypasses your alias entirely.
Does the alias work with submodules and Homebrew taps?
Submodules use the URL in .gitmodules, so change it to the alias. Tools that hardcode github.com ignore your aliases.