Automatic Account Selection for Multi-Account Workflows #12697
Replies: 5 comments 1 reply
-
|
Thanks! Will have a look at this tomorrow since I don't work Mondays. |
Beta Was this translation helpful? Give feedback.
-
|
I had a similar implementation in #12628 using the combined resolution chain in this proposal makes sense to me. |
Beta Was this translation helpful? Give feedback.
-
|
@williammartin checking back on this, since the discussion opened there have been 3 more related issues filed (#12885, #12888, #12190) so the demand keeps growing. would love to know if there is a direction you would like to go here so we can help with the implementation. |
Beta Was this translation helpful? Give feedback.
-
|
I have each local repo dir configured (via and a With It would be great if |
Beta Was this translation helpful? Give feedback.
-
|
@duanyutong yeah that is basically the same pattern I implemented in PR #12628. it reads git config github.account which you can set per-repo or via includeIf for directory-based switching. worked great locally for splitting personal/work without any manual gh auth switch. the PR got closed because it was not linked to a help wanted issue, not because of the approach. @williammartin would be great to get a help wanted label on this or one of the related issues (#12885, #12888, #12190) so we can move forward with the implementation. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Developers who manage multiple GitHub accounts (personal, work, clients) face constant friction with
ghCLI. While git handles multi-identity workflows well through SSH config, URL rewriting, and conditional includes,ghrequires manual global switching viagh auth switch— a stateful operation that affects all repositories at once.The moment you have to think about switching, you're going to repeatedly forget to do it.
But even if you never forget, there's a deeper issue:
gh auth switchis a global, stateful mutation. If you're working across multiple repositories concurrently — multiple terminal tabs, background scripts, IDE integrations — switching in one context silently affects all others. A global switch is fundamentally incompatible with concurrent multi-account usage.References
git config github.accountProposed Solution: Repository Auth Mapping (
gh auth map)The core idea is to add a built-in mapping mechanism within
gh. Users define rules that map repository owners (or specific repositories) to authenticated accounts.ghresolves the current repository from git remotes and selects the correct account automatically — no manual switching required.This is self-contained within
gh, with no dependency on external git or SSH configuration, and works regardless of whether the repo was cloned over SSH or HTTPS.Commands
Supported scopes
OWNER/*Devolutions/*OWNER/REPOawakecoding/MsRdpExHOST/OWNER/*github.com/Devolutions/*HOST/OWNER/REPOgithub.com/awakecoding/MsRdpExMatching priority: exact repo → owner wildcard → host active account. Environment token precedence (
GH_TOKEN,GITHUB_TOKEN) is preserved.Example
From this point on:
No
gh auth switch. No forgetting. No wrong-account PRs.Enhanced
gh auth statusgh auth status --activeshows the effective account for the current repository, including the source of the selection (repository mapping, active host account, or environment token) — eliminating guesswork.Why this approach
gh, no external configuration neededghcommands work without modificationgh auth map listandgh auth status --activeshow what's configured and resolvedPossible Extension: Git Config Integration
PR: #12628 by @yuvrajangadsingh | Issue: #12459 by @jayproulx
A complementary approach uses
git config github.account(typically set viaincludeIfconditional includes) to letghread the desired account from git's own configuration:This could be an additional resolution step:
ghchecksgit config github.accountfirst, and if the account is authenticated, uses it — otherwise falls through to mapping rules.Strengths: familiar to
includeIfusers, directory-scoped (works even outside a repo).Considerations: relies on external config, no built-in auditability, directory-based scoping may not cover all cases (e.g., repos from different orgs in the same parent directory).
Combined Resolution Chain
Both approaches can coexist:
Questions for Discussion
ghown this natively? The mapping mechanism is self-contained and backward compatible.git config) be supported too?gh auth statusalways show the effective account for the current repo context?gh auth mapthe right name, or wouldgh auth rule/gh auth accountbe clearer?gh auth mapalone, withgit configsupport as a follow-up?I created PR #12681 as a working implementation — I've been using it locally and it eliminates the multi-account friction entirely. I'm happy to iterate on the design based on feedback.
Beta Was this translation helpful? Give feedback.
All reactions