Blog

Git Push Wrong GitHub Account Over HTTPS: Fix osxkeychain

Git pushes with the wrong GitHub account over HTTPS because the macOS keychain stores one login per host, github.com, and git reuses it for every repo. To fix it, erase the cached entry, then either scope credentials by repo path with credential.useHttpPath, put the username in the remote URL, or switch to SSH.

Why does git push with the wrong GitHub account over HTTPS?

When you clone with an https:// URL, git asks a credential helper for a username and token. On macOS that helper is usually osxkeychain, and it keys entries by protocol and host. Your personal and work accounts both live at github.com, so the first login that got saved wins everywhere.

The symptom is familiar if you read our guide on git push permission denied with the wrong GitHub account: a 403 saying Permission to acme/billing.git denied to your-personal-user.

How do I see which credential git is using?

git config --show-origin --get-all credential.helper
printf "protocol=https\nhost=github.com\n\n" | git credential-osxkeychain get

The second command prints the username and token the keychain would hand to git. If the username is the wrong one, you have found the culprit.

How do I erase the cached GitHub credential?

printf "protocol=https\nhost=github.com\n\n" | git credential-osxkeychain erase

The next push prompts again. Enter the right account and token, and the keychain saves it. This works until you push from a repo that needs the other account.

How do I keep two GitHub accounts apart over HTTPS?

Scope credentials by repo path

git config --global credential.useHttpPath true

With this on, git matches credentials by the full path (github.com/acme/billing.git), so each org or user gets its own keychain entry.

Put the username in the remote URL

git remote set-url origin https://acme-dev@github.com/acme/billing.git

Git now asks the keychain for acme-dev specifically, instead of whichever login came first.

Let the GitHub CLI answer

gh auth setup-git
gh auth switch --user acme-dev

gh becomes the credential helper and serves the active account’s token. The catch is that it follows the account you switched to last, not the folder you are in. We cover that gap in gh auth switch for multiple GitHub accounts.

Or leave HTTPS behind

SSH with one key per account avoids the shared-host problem. See multiple SSH keys for GitHub with IdentitiesOnly.

How do I stop this from happening again?

The fixes above are per repo or per remote, so you must remember them. Hats binds a folder to a profile: commit identity, SSH key, gh session and signing change together, and the guard blocks a push made under the wrong hat. Install it with brew install --cask xorbeta/hats/hats. Details are on the Hats homepage.

FAQ

Does git config user.email change which account pushes?

No. It only sets the author of commits. The account that pushes comes from the credential (HTTPS) or the key (SSH).

Can the keychain hold two GitHub tokens?

Yes, as separate entries, but git only picks the right one if you give it a username or enable credential.useHttpPath.

Why does it still fail after I erase the credential?

Check for another helper in git config --list --show-origin, such as gh or a system-level osxkeychain, and for a remote URL that already embeds the wrong username.

Is a personal access token safer than a password?

GitHub no longer accepts account passwords for git over HTTPS, so you need a token or a browser login anyway. Prefer fine-grained tokens limited to the repos you need, as described in the GitHub docs on credential caching.