How gh-autoaccount works: account routing, agents and a demo
The paths taken by gh calls and Git HTTPS, rule precedence and a two-account trial. Includes the agent skill, identity guards and a demo using simulated data.
AI-assisted translation, approved by Mateo.

A personal account, a work account and several agents running: that is the scenario gh-autoaccount aims to address. I built it as a GitHub CLI extension to select credentials from context without having to switch the global active account.
This post explains the mechanism in v0.1.0-beta.9, its setup and what the guards check. The launch News summarizes the idea; here we look at how it works.
The problem is not just the commit email
Git can select user.name and user.email through includeIf. That sets commit authorship, not necessarily the account authenticating a GitHub query or an HTTPS push.
Meanwhile, gh auth switch changes the shared active account. If one terminal switches while another prepares a PR, both depend on that global state. The alternative is to select an account for each invocation.
Two paths: gh and Git HTTPS
Setup installs two pieces with separate responsibilities:
- gh shim: when it precedes the real executable on PATH, it receives the call, resolves context and launches the real GitHub CLI with the selected token for that process.
- Git credential helper: Git consults it when it needs HTTPS credentials. It resolves the account using the rules and credential request for the corresponding host. It is registered in Git configuration and does not depend on a shell hook.
The gh path is: command → shim → account selection → credential resolution → real GitHub CLI. For Git HTTPS: Git operation → helper request → host account and credential selection → Git continues the operation.
There is no need for direnv or the next prompt. An agent can follow the same path if its process has the shim on PATH. Already-running editors need restarting after setup; cron and systemd may need an explicit PATH. The Git helper has its own configuration.
Which rule wins
The documented precedence is:
- An explicit
exec --useraccount. - The
GH_AUTOACCOUNT_USERsession override. - A repository-owner pin, for an organization or user.
- The deepest directory pin.
- The default account.
An owner pin wins over a directory pin. For gh, context can come from the current repository, -R or other documented forms. Command-line interpretation is best effort: do not assume it understands every arbitrary argument.
The user configures these rules; they are not inferred from a commit email or git config github.account. Credentials are looked up on each invocation, with no token cache of its own. Headless keyring access may require a D-Bus session.
Minimal installation and setup
You need Git, gh 2.40 or later and both accounts already authenticated through gh auth login. Replace alice, bob and work-org with your accounts and organization; the example directories must exist.
- Install:
gh extension install mdiloreto/gh-autoaccount. - Read what setup changes before running it:
gh autoaccount setupinstalls the shim and helper and may modify shell and Git configuration. - Restart your terminal, editor and any agents that were already running.
- Set the default:
gh autoaccount default alice. - Associate the work directory:
gh autoaccount pin bob --dir ~/work. - Optionally associate an organization:
gh autoaccount pin bob --owner work-org.
A personal repository with no other pin then uses alice; one inside ~/work uses bob. The work-org pin allows its repositories to authenticate as bob even when stored elsewhere. Inspect the rules with gh autoaccount list.
A two-terminal trial
In the personal terminal, run cd ~/personal, then gh api /user --jq .login. In this example, without another pin or override, the expected result is alice.
In another terminal, run cd ~/work, then the same gh api /user --jq .login: the expected result is bob. Query again from the first terminal: it should still use alice, without running gh auth switch between them.
The demo shows project-based selection and an agent guard. It uses the real binary with simulated accounts and API responses, recorded offline. It does not prove live GitHub authentication or keyring access; check the preceding example's results in your own installation.
An explicit agent workflow
The bundled skill teaches inspection, verification and execution without changing the global account. Preview it with gh autoaccount skill. On Linux and macOS, install it in the project with gh autoaccount skill install --dir .agents/skills; use the directory your agent loads. On Windows, copy it manually.
gh autoaccount status --json shows local context and gh autoaccount list --json exposes the rules. To execute from a specific directory without relying on the agent's initial working directory:
gh autoaccount exec --dir ~/work --require-pin --expect-user bob --verify-identity -- gh pr status
--dirchanges the directory used for lookup and child execution.--require-pinrequires an actual pin; a default is not enough.--expect-user bobchecks the selected profile.--verify-identityqueries the remote login before starting the child process.
Local JSON does not prove remote identity, and checking the login does not prove permissions on every repository. If a guard fails, stop and diagnose rather than removing it to force the operation.
Limits and diagnostics
The token is passed to the child process, not exported globally into the shell. That process and its descendants can read it: this is not a sandbox. SSH and commit authorship have separate configuration; Git identity is an additional option.
For troubleshooting, start with gh autoaccount status and gh autoaccount doctor. After upgrading the extension, gh autoaccount setup --migrate refreshes its managed files. A 404 may also mean missing permissions or SSO, not just the wrong account. Do not publish tokens or keys when asking for help.
This is still a beta: behavior may change before 1.0. The README, selection and JSON reference and security model document the boundaries. Functional reports belong in Issues, and security reports should use the private channel.
Thanks for reading. The learning continues in the next article.


