You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
gh should auto-select account based on repo remote / git config #13529
gh supports multiple authenticated accounts but requires manual gh auth switch every time you move between repos owned by different accounts. This is a constant source of friction — 404s, PRs attributed to the wrong account, CI triggered under the wrong identity.
Anyone managing separate personal and work GitHub accounts (which is extremely common) hits this daily.
Expected Behavior
gh should automatically select the correct authenticated account based on the current repo context. Two natural resolution strategies:
git config github.account — works with git's existing includeIf / conditional includes, zero new config surface
Remote origin org matching — map orgs to accounts in gh config, zero per-repo setup
Either way: cd into a repo, run gh pr create, correct account is used. No manual switching.
Current Workaround
A shell wrapper that calls gh auth switch before every invocation. It works but:
Mutates global state (not safe for concurrent terminal sessions)
Adds latency to every gh command
Shouldn't be necessary for something this fundamental
The implementation is trivial — a single check in ActiveUser() before falling back to the stored active user. A working PR already exists. This should be a priority for the multi-account experience.
For anyone who needs this today, here's what I'm using. Same concept as @jayproulx's wrapper in #12459 but uses the JSON output instead of grepping stderr:
# ~/.zsh_aliases (or .bashrc, .zshrc, etc.)# gh CLI — auto-switch account based on git config github.account# Uses includeIf in ~/.gitconfig to set github.account per directory.# Workaround until this is implemented upstream.gh() {
local target active
target=$(git config github.account 2>/dev/null)if [[ -n"$target" ]];then
active=$(command gh auth status --active --json hosts -q '.hosts["github.com"][0].login'2>/dev/null)if [[ "$target"!="$active" ]];thencommand gh auth switch --user "$target"&>/dev/null
fificommand gh "$@"
}
Git config setup:
# ~/.gitconfig[github]account = personal-username # default[includeIf "gitdir:~/projects/work/"]path = ~/.gitconfig-work
# ~/.gitconfig-work[github]account = work-username # overrides default for repos under ~/projects/work/
The default [github] section must come before the includeIf so conditional includes can override it (git config is last-wins).
Works, but mutates global auth state on every invocation — not safe across concurrent terminal sessions. Native support would fix that.
Activity
davidlovas commentedon May 27, 2026
Shell wrapper workaround
For anyone who needs this today, here's what I'm using. Same concept as @jayproulx's wrapper in #12459 but uses the JSON output instead of grepping stderr:
Git config setup:
The default
[github]section must come before theincludeIfso conditional includes can override it (git config is last-wins).Works, but mutates global auth state on every invocation — not safe across concurrent terminal sessions. Native support would fix that.
Upstream sync 2026-05-29 (#16)
Upstream sync 2026-05-29 (#19)
feat: auto-select authenticated account based on repo context
feat: auto-select authenticated account based on repo context
feat: auto-select authenticated account based on repo context
feat: auto-select authenticated account based on repo context