Most developers end up with two GitHub accounts: personal and work. Git itself doesn't care, but SSH does. GitHub logs you in as whichever account owns the first key that works. If your personal key is offered first, you push to work repos as the wrong person, or get ERROR: Permission to company/repo.git denied to personal-you.
The fix is one key per account and a host alias that pins it.
#1. Create a key for the second account
You probably already have ~/.ssh/id_ed25519 for your personal account. Create a separate one for work:
ssh-keygen -t ed25519 -C "you@company.com" -f ~/.ssh/id_ed25519_workUse a passphrase, then let macOS remember it (see saving SSH passphrases in Keychain).
#2. Add the public key to the second account
Sign in to the work GitHub account, open Settings → SSH and GPG keys → New SSH key, and paste:
pbcopy < ~/.ssh/id_ed25519_work.pub#3. Give each account its own Host entry
Edit ~/.ssh/config:
# Personal: plain github.com
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
# Work: an alias that connects to the same github.com
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yesgithub.com-work isn't a real domain. It's a name SSH matches against your config, and HostName sends the connection to the real github.com. IdentitiesOnly yes is the important line: without it SSH may still offer your personal key from the agent first.
Check that each alias reaches the right account:
ssh -T git@github.com
# Hi personal-you! You've successfully authenticated…
ssh -T git@github.com-work
# Hi work-you! You've successfully authenticated…#4. Clone and push with the alias
Use the alias in place of github.com for work repositories:
git clone git@github.com-work:company/project.gitFor a repo you already cloned, update its remote:
git remote set-url origin git@github.com-work:company/project.git
git remote -v#5. Use the right name and email per folder (optional)
SSH decides which account pushes. Your commit author comes from Git's user.email. Keep work folders under one directory and let Git switch automatically:
# ~/.gitconfig
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work# ~/.gitconfig-work
[user]
email = you@company.com
[core]
sshCommand = ssh -i ~/.ssh/id_ed25519_work -o IdentitiesOnly=yesWith core.sshCommand set, repos under ~/work/ use the work key even with plain git@github.com: remotes. Choose either aliases or sshCommand; both work.
#Common mistakes
- Reusing a key: GitHub won't let one public key belong to two accounts.
- Forgetting
IdentitiesOnly yes: the agent's personal key wins and you push as the wrong user. - A
Host *with anIdentityFile: it adds that key to every host, including your aliases. Read how wildcards and precedence work. - Old remotes: existing clones keep using
github.comuntil youset-url.
#The drag-and-drop version
In EasySSHConfig you drag github.com onto your personal key. When you drag it onto your work key too, the app asks whether to move it or create an alias. It then writes the github.com-work block above, with HostName and IdentitiesOnly, and shows the git clone git@github.com-work:… line to copy.
Frequently asked questions
Can two GitHub accounts share one SSH key?
No. GitHub rejects a public key that's already attached to another account, because the key is how it identifies you. Each account needs its own key pair.
Why does Git push as the wrong GitHub user?
SSH offered the other account's key first and GitHub accepted it. Pin one key per host alias with IdentityFile and IdentitiesOnly yes so only the intended key is offered.
Do I need a host alias, or is there another way?
You can instead set core.sshCommand per folder with includeIf in ~/.gitconfig. Aliases work everywhere (any tool that uses SSH); core.sshCommand only affects Git.
Does this work for GitLab and Bitbucket?
Yes. The same approach (one key per account, an alias Host with HostName set to the real host) works for any Git host that authenticates by SSH key.