Multiple GitHub Accounts on One Mac: A Setup That Holds
If you use more than one GitHub account on a Mac, the working setup has four moving parts: a per-folder git identity (includeIf), a per-account SSH key, the right gh session, and matching commit signing. Set all four per folder and you stop pushing as the wrong person. Here is how to do each, and where the DIY version breaks.
Why one account on a Mac is easy and two is not
Git has one global identity by default. That is fine until you have a personal account and a work one, or a client who wants commits under their own email. Then “identity” turns out to be four separate things, each configured in a different place:
| Layer | Where it lives | What goes wrong |
|---|---|---|
| Commit identity | user.name, user.email | Your personal email ends up in a client’s history |
| Push authentication | SSH key or credential helper | Permission denied, or a push as the wrong account |
| GitHub CLI | gh auth | A pull request opens from the wrong account |
| Signing | commit.gpgsign, user.signingkey | “Unverified” badges on commits |
Most guides fix one layer and leave the other three to chance.
Step 1: One SSH key per account
Generate a key for each account:
ssh-keygen -t ed25519 -C "you@personal.com" -f ~/.ssh/id_personal
ssh-keygen -t ed25519 -C "dev@acme.com" -f ~/.ssh/id_acme
Add each public key to the matching GitHub account (Settings, SSH and GPG keys). Then give each key its own host alias in ~/.ssh/config:
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_personal
IdentitiesOnly yes
Host github-acme
HostName github.com
User git
IdentityFile ~/.ssh/id_acme
IdentitiesOnly yes
IdentitiesOnly yes matters. Without it, ssh-agent may offer every loaded key, and GitHub accepts the first one that matches, which may not be the account you meant.
Step 2: Switch identity by folder with includeIf
Keep repositories grouped by account: ~/dev for personal, ~/clients/acme for the client. Git can load extra config depending on where a repo lives. In ~/.gitconfig:
[user]
name = Your Name
email = you@personal.com
[includeIf "gitdir:~/clients/acme/"]
path = ~/.gitconfig-acme
And in ~/.gitconfig-acme:
[user]
email = dev@acme.com
signingkey = ~/.ssh/id_acme.pub
[core]
sshCommand = ssh -i ~/.ssh/id_acme -o IdentitiesOnly=yes
[gpg]
format = ssh
[commit]
gpgsign = true
Every repository under ~/clients/acme/ now uses the Acme email, the Acme key for pushes and SSH-based signing. The trailing slash in gitdir: is required, since it makes git match everything beneath the folder. Using core.sshCommand means you can keep normal git@github.com: remote URLs instead of rewriting them to github-acme:.
Check it from inside a repo:
git config user.email
git config core.sshCommand
Step 3: Don’t forget the GitHub CLI
Everything above is invisible to gh. It keeps its own sessions, and the active one is global. Log in to both accounts:
gh auth login
gh auth status
gh auth switch --user acme-dev
If you open pull requests from the terminal, this is the layer that bites: the commits are right, the push is right, and the PR is created by the wrong account anyway. gh auth switch is manual, so you have to remember it every time you change projects.
Where the manual setup breaks
The folder approach is solid, and it is plain git, so it is easy to inspect. The weak points are the edges:
- Repos outside your folders. A quick clone into
/tmpmatches no rule, so it falls back to your global identity. - The
ghsession is global, so it does not followcdat all. - Silent overrides. An old
url.<base>.insteadOfrewrite, a repo-levelcore.hooksPath, or a key that never made it to GitHub can quietly defeat the setup. - Nothing warns you. Git will happily commit with the wrong email. You find out in review, or from the client.
Automating it with Hats
Hats is a free macOS menu bar app built around exactly this problem. You create a profile per account, bind folders (or a whole GitHub org) to a profile, and it switches commit email, SSH key, gh session and signing together. Under the hood it writes plain includeIf config like the one above, so it stays readable and removable.
It also adds a guard: if you try to commit or push from a repo whose identity does not match the rule, it blocks the action and tells you which hat to wear. The CLI covers the same ground:
brew install --cask xorbeta/hats/hats
hats rule add --folder ~/clients/acme acme
hats status
hats use acme
hats doctor
hats doctor looks for the traps listed above, such as URL rewrites and hook overrides, and prints a fix for each. The guard chains your repo’s own hooks, so husky and commitlint keep working. Hats writes configuration only: tokens stay in gh, keys in ~/.ssh.
FAQ
Can I use one SSH key for two GitHub accounts? No. GitHub requires each SSH key to belong to a single account, so you need one key per account.
Does includeIf work with remote URLs instead of folders?
Git also supports hasconfig:remote.*.url: conditions in recent versions, which can match on the remote. Folder-based matching is the most widely supported and the easiest to reason about.
Why does my commit show the right email but still the wrong author on GitHub? GitHub links a commit to an account by the email in the commit. Make sure that email is added and verified on the account you expect.
Do I need separate signing keys? It is cleanest if you have one per account, because GitHub shows “Verified” only when the signing key is registered on the account that owns the commit email.