Repository navigation
Description
Problem
gh account selection is global which makes it difficult to work concurrently with agents. Ideally this should be made automatic & context-scoped (like git's includeIf). For users with multiple accounts logged in with different access this is problematic when those accounts have different access restrictions that are not host-based.
git already solves the "multiple identities on one machine" problem automatically via path-based config (or pseudo-host based config) includes and does not require manual switching or wrapper scripts:
# ~/.gitconfig
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig-personal
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-workalternatively pseudo-host based looks something like
[url "[email protected]:acme/"]
insteadOf = [email protected]:acme/
insteadOf = https://github.com/acme/
insteadOf = ssh://[email protected]/acme/
# with the following in ssh_config
# your default
Host github.com
IdentitiesOnly yes
IdentifyFile ...
# acme org
Host acme.github.com
HostName github.com
IdentitiesOnly yes
IdentifyFile ...Both of those methods use the right identity transparently and per-repo without mutating global state.
The gh commands silently use whatever account happens to be globally active. While gh commands are host aware this is completely unsolved when both accounts are on the same host (an EMU + a public github.com identity), where hostname can't disambiguate the user.
Ideal end state
gh should resolve the acting account automatically from context the same way git does instead of requiring a global switch. Ideally it could even reuse/mirror the resolution git already performs (e.g. infer based on account authorization and the git organization/repo access, with some user-defined default in ambiguous cases).
An example (fake accounts)
Identities (both on github.com):
| Account | Type | Owns repos under |
|---|---|---|
octocat |
personal / public identity | octocat/* |
octocat_acme |
Enterprise Managed User (EMU) | acme-corp/*, acme-labs/* |
Directory layout:
~/personal/ → work as octocat
~/work/ → work as octocat_acme (EMU)
git today (works automatically):
# ~/.gitconfig
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig-personal # user.email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work # user.email = [email protected]gh today (broken):
$ cd ~/work/acme-corp/billing-api
$ gh pr create ...
# ✗ runs as whatever `gh auth switch` last set globally.
# If that was `octocat`, the PR/comment is authored by the WRONG identity —
# and an EMU repo may reject or misattribute it.gh desired — automatic, context-scoped resolution.** Illustrative shape (any of these, not prescriptive):
# ~/.config/gh/config.yml (illustrative)
account_rules:
- if_gitdir: "~/personal/"
account: [email protected]
- if_gitdir: "~/work/"
account: [email protected]
# or resolve by repo owner, independent of directory:
- if_owner: ["acme-corp", "acme-labs"]
account: [email protected]
- if_owner: ["octocat"]
account: [email protected]$ cd ~/work/acme-corp/billing-api
$ gh pr create ...
# ✓ automatically acts as octocat_acme — no global switch, no wrapper script,
# no mutation of state that affects other shells or agents.
$ cd ~/personal/dotfiles
$ gh pr comment 12 --body "lgtm"
# ✓ automatically acts as octocat.Automated agents managing PRs/comments across both identities can't safely gh auth switch as it's a global racy mutation and there's no per-invocation --acount ... primitive for same-host accounts. Context-based resolution means an agent (or human) just cds into the repo and every gh call is already the right identity, matching the git behavior they already trust. As an escape hatch, an explicit override (gh --account <user>@<host> / GH_ACCOUNT) covers cases outside any repo.
Prior art / references
- Original request: cli/cli#326 (open since Feb 2020); path-based (
includeIf-style) switching requested repeatedly in-thread - Root-cause analysis: cli/cli#12885
- Owner→user mapping PR (auto-closed): cli/cli#13130
- Git Config Setting: #12459, #13529
- Related: cli/cli#12145, #9111, #11938
Activity
github-actions commentedon Jul 2, 2026
Thank you for your issue! We have categorized it as an enhancement (feature or improvement) request, and it has been added to our backlog. In doing so, we are not committing to implementing this feature at this time, but, we will consider it for future releases based on community feedback and our own product roadmap.
Unless you see the help wantedContributions welcome
label, we are not currently looking for external contributions for this feature.
If you come across this issue and would like to see it implemented, please add a thumbs up! This will help us prioritize the feature. Please only comment if you have additional information or viewpoints to contribute.
jeff-younker commentedon Jul 31, 2026
From what I can see the issues with this stem from checking that the host is
github.com. If gh checked that the host was*.github.comthen I think the existing recommended method (using insteadOf) would work as long as the remapped host corresponded to the right pattern.platinummonkey commentedon Jul 31, 2026
here is an example FIX option #13781
barnuri commentedon Oct 15, 2026
Until this lands natively, I built a small wrapper that does this: https://github.com/barnuri/ghx
It maps folders to gh accounts (
~/.config/ghx/config.yaml, deepest match wins) or a per-repo.ghxfile, then runs the realghwithGH_TOKENfromgh auth token --user <login>. It never callsgh auth switch, so terminals and agents in different folders don't change each other's account. All gh commands, flags and extensions pass through unchanged. It installs as aghshim, andgh auth git-credentialgets the folder's token, sogit pushuses the right account too.mdiloreto commentedon Nov 5, 2026
Sharing an external workaround for the concurrent-terminal/agent case in this issue. I maintain gh-autoaccount, a GitHub CLI extension currently at v0.1.0-beta.9.
It selects an already-authenticated account per command using user-configured directory or repository-owner pins, without calling
gh auth switch. A managedghshim and Git HTTPS credential helper handle routing; noninteractive agents do not depend on direnv or an interactive prompt hook. This is not native support ingh, and it uses its own pins rather than readinggit config github.account.After installation/setup from the README, an example with a logged-in account named
boband an existing~/workdirectory is:The agent-facing pieces include a bundled skill, JSON context diagnostics, and guards that require a matching pin and verify the selected login before starting a command. Authentication and Git commit authorship remain separate; passing a token to a child is not a sandbox.
I would still welcome context-scoped selection upstream. This beta may be useful to people who want to try that workflow with the released CLI in the meantime; feedback and limitations are tracked in the repository.
jamesng-minhkhoi commentedon Nov 7, 2026
Hitting a very concrete version of this daily with coding agents (opencode / Claude Code style harnesses) working across two accounts on the same host,
github.com:Because account selection is global, my repo's agent instructions have to tell the agent to run
gh auth switch --user <login>before push/PR commands for specific repos. That works right up until:switchin session A silently re-identifies session B mid-flight;repository not found'' — it's reallythis account can't see the repo'';What has worked as a stopgap:
GH_TOKEN="$(gh auth token --user <login>)" gh pr create ...No global mutation, agent-safe. But it's undiscoverable, and it doesn't cover
gh auth git-credential, sogit pushidentity still depends on ambient state.+1 on scoping resolution per repo/directory (git
includeIf-like) or per-invocation, and on surfacing the chosen identity for mutating commands. Theinternal/accountrulesdirection in #13781 looks like the right shape for this — happy to test it against the concurrent-agents setup described above.