gh auth switch for Multiple GitHub Accounts: What It Misses
gh auth switch changes the active GitHub CLI account, but it does it for the whole machine, not for the repo you are standing in. That is why it works for one terminal and quietly breaks the next. Here is how it behaves, where it bites, and how to pin the right account per folder.
What does gh auth switch actually do?
The GitHub CLI stores several logged-in accounts per host, but only one is active at a time. gh auth switch changes which one.
gh auth login # run once per account
gh auth status # lists every account, marks the active one
gh auth switch --user acme-dev
The active account is global state. Every terminal tab, every editor task and every script that calls gh sees the change immediately. There is no “active account for this repo” in gh itself.
Why does the wrong account still open my pull request?
Because four separate things decide who you are, and gh auth switch only moves one of them:
- Commit identity:
user.nameanduser.emailin git config. - Push authentication: your SSH key or credential helper.
- GitHub CLI session: the account
gh pr createuses. - Commit signing: the key behind
commit.gpgsign.
Switch gh to your client account and your next commit still carries your personal email, and the push may still go out with your personal SSH key. Each layer fails differently: an unverified badge, a Permission denied, or a pull request opened by an account your client has never heard of. For the push side, see Git push permission denied to the wrong GitHub account.
How do I make git use the gh account for HTTPS pushes?
If you push over HTTPS, let gh act as the credential helper:
gh auth setup-git
Git then asks gh for a token, and gh answers with the active account. That fixes the “token belongs to someone else” case, but it ties pushes to whatever is active globally, which is the very thing you want to avoid when two repos need two accounts at once.
How do I use a different account per folder without switching?
Two moves cover most of it.
1. Set the commit identity by folder with includeIf. The full setup, and its traps, are in Git includeIf: different email per folder.
# ~/.gitconfig
[includeIf "gitdir:~/clients/acme/"]
path = ~/.gitconfig-acme
2. Give gh a token for that folder only. The GH_TOKEN environment variable overrides the stored active account for that process. With a tool such as direnv, you can set it in a folder’s .envrc:
# ~/clients/acme/.envrc
export GH_TOKEN="$(gh auth token --user acme-dev)"
Now gh inside ~/clients/acme uses the acme account, whatever gh auth switch last said. Pair it with an SSH key per folder through core.sshCommand and the three layers agree. The broader picture is in Multiple GitHub accounts on one Mac.
What about commit signing?
Signing is a fourth layer. If the folder’s profile sets your email but not its signing key, GitHub shows “Unverified”. Set user.signingkey and commit.gpgsign in the same per-folder config file. See GitHub unverified commits with multiple accounts.
Can one tool do all four layers?
You can wire this by hand, and it holds as long as you remember every folder and every layer. Hats is a macOS menu bar app that binds a folder to a profile and moves commit identity, SSH key, gh session and signing together, and blocks a push when the hat does not match the folder. The manual setup above is plain git and shell, so you can keep it or replace it.
FAQ
Does gh auth switch change my git user.email?
No. It only changes which account the GitHub CLI uses. Commit email comes from git config.
Can two terminals use different gh accounts at the same time?
Not through gh auth switch, since the active account is shared. Set GH_TOKEN per shell or per folder instead.
Does gh auth switch affect SSH pushes?
No. SSH pushes use the key your SSH config or core.sshCommand selects, not the gh account.
How do I see which account gh is using?
Run gh auth status. The account marked active is the one gh commands will use.