Skip to content

Authenticate GitHub with gh tokens per repo, so two accounts can share one box - #15

Merged
ldayton merged 1 commit into
mainfrom
git-credential-per-account
Aug 22, 2026
Merged

ldayton merged 1 commit into
mainfrom
git-credential-per-account

Conversation

@ldayton

@ldayton ldayton commented Aug 22, 2026

Copy link
Copy Markdown
Owner

An ssh key can be registered on exactly one GitHub account, so a box with a personal and a work account could never authenticate as both — the second registration is refused as a key already in use. Tokens have no such limit, and gh already stores one per account.

gh auth setup-git does not solve it: its helper answers only for whichever account is active. With two accounts that is wrong half the time, and wrong in the worst way, because a read as the other account returns 404 rather than 403 — indistinguishable from the repository not existing.

So git now resolves the account the way gh/gh already does: gh.account on the repo, or a username carried in the remote URL, then gh auth token --user. Read-only, never gh auth switch, which mutates shared state that concurrent sessions flip under each other. Identity becomes one setting per repo, covering fetch, push and every gh subcommand alike.

The empty helper ahead of it resets the chain — helpers accumulate across system, global and local config and are tried in order, so without it a system helper (osxkeychain, or a leftover setup-git line) answers first with the wrong account.

Note that ~/.gitconfig is copied rather than symlinked (install.sh:98) and left alone when it already exists, so this reaches freshly provisioned accounts automatically and existing ones not at all.

https://claude.ai/code/session_01AVTNMRmHsTJMauEwo2ryLv

…e one box

An ssh key can be registered on exactly one GitHub account, so a box with a personal and a work account could never authenticate as both -- the second registration is refused as a key already in use. Tokens have no such limit, and gh already stores one per account.

The obvious move, `gh auth setup-git`, does not work here: its helper answers only for whichever account is active. With two accounts that is wrong half the time, and wrong in the worst way, because a read as the other account returns 404 rather than 403 -- indistinguishable from the repository not existing.

So git resolves the account the way gh/gh already does: `gh.account` on the repo, or a username carried in the remote URL, then `gh auth token --user`. Read-only, never `gh auth switch`, which mutates shared state that concurrent sessions flip under each other. Identity is now one setting per repo, covering push, fetch, and every gh subcommand alike.

The empty helper ahead of it resets the chain. Helpers accumulate across system, global and local config and are tried in order, so without it a system helper -- osxkeychain, or a leftover setup-git line -- answers first with the wrong account.

Claude-Session: https://claude.ai/code/session_01AVTNMRmHsTJMauEwo2ryLv
@ldayton
ldayton merged commit 487b39b into main Aug 22, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant