Skip to content

Better multi-account gh account usage #13768

Open

Description

@platinummonkey

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-work

alternatively 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

Activity

  1. added
    needs-triage
    on Jul 1, 2026
  2. added
    enhancement
    gh-auth
    and removed
    needs-triage
    on Jul 2, 2026
  3. github-actions commented on Jul 2, 2026

    @github-actions
    Contributor

    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 wanted Contributions 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.

  4. jeff-younker commented on Jul 31, 2026

    @jeff-younker

    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.com then I think the existing recommended method (using insteadOf) would work as long as the remapped host corresponded to the right pattern.

  5. platinummonkey commented on Jul 31, 2026

    @platinummonkey
    Author

    here is an example FIX option #13781

  6. barnuri commented on Oct 15, 2026

    @barnuri

    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 .ghx file, then runs the real gh with GH_TOKEN from gh auth token --user <login>. It never calls gh 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 a gh shim, and gh auth git-credential gets the folder's token, so git push uses the right account too.

  7. mdiloreto commented on Nov 5, 2026

    @mdiloreto

    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 managed gh shim and Git HTTPS credential helper handle routing; noninteractive agents do not depend on direnv or an interactive prompt hook. This is not native support in gh, and it uses its own pins rather than reading git config github.account.

    After installation/setup from the README, an example with a logged-in account named bob and an existing ~/work directory is:

    gh autoaccount pin bob --dir ~/work
    gh autoaccount exec --dir ~/work --require-pin --expect-user bob --verify-identity -- gh pr status

    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.

  8. jamesng-minhkhoi commented on Nov 7, 2026

    @jamesng-minhkhoi

    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:

    • two agent sessions run in parallel: a switch in session A silently re-identifies session B mid-flight;
    • forgetting to switch back means a push fails with a misleading repository not found'' — it's really this account can't see the repo'';
    • nothing surfaces ``you are about to act as X against repo R'' before a mutating command, so the mistake is only visible after the fact.

    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, so git push identity 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. The internal/accountrules direction in #13781 looks like the right shape for this — happy to test it against the concurrent-agents setup described above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgh-auth

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions