What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

GitHub CLI (gh) does not document a built-in way to choose among multiple accounts on the same host based on a repository’s owner. You can automate that behavior with a custom wrapper: read the repository’s remote, map its owner to a token, then run gh with that token in GH_TOKEN. GitHub documents that environment tokens take precedence over credentials stored by gh; the owner-to-token mapping is your own logic.

What gh selects automatically—and what it does not

GitHub CLI can detect the intended GitHub platform from repository context. That does not mean it will choose an account based on the repository owner when multiple accounts belong to the same host. GitHub’s guide says the CLI cannot automatically detect the intended account when it lacks repository context, and points users with multiple accounts on one platform to gh auth switch. See Using the GitHub CLI across GitHub platforms.

Host selection, repository targeting, and account selection are distinct. GH_HOST supplies a default host when one cannot be inferred, while GH_REPO can specify a repository as [HOST/]OWNER/REPO. Neither setting provides an owner-based account-to-token mapping. The GitHub CLI environment variables manual documents these settings and token precedence.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose between manual switching and an owner-based wrapper

Approach What it does When it fits
gh auth switch Switches the active account for a GitHub host. If the choice is ambiguous, specify --user or use the prompt. You want to change the host’s active account yourself.
Per-invocation GH_TOKEN Uses the token supplied in the command environment instead of stored credentials. You want a launcher to select a token for each repository without changing the stored active account.

The gh auth switch manual documents manual switching. The CLI’s environment-variable documentation establishes that GH_TOKEN takes precedence over GITHUB_TOKEN on GitHub.com and ghe.com, and that environment tokens take precedence over stored credentials. For GitHub Enterprise Server, use the corresponding enterprise variables described in the manual. Check the relevant host’s documentation before adapting a wrapper.

Build a wrapper that maps repository owners to tokens

The following is a custom pattern, not a feature provided by gh. It assumes Bash, a Git repository, a remote named origin, GitHub.com, and token values supplied through environment variables. The example expects HTTPS or SSH GitHub remotes and deliberately stops if it cannot identify the owner.

  1. Save the script as gh-owner somewhere on your PATH, then make it executable with chmod +x gh-owner.

    #!/usr/bin/env bash
    set -euo pipefail
    
    remote="${GH_OWNER_REMOTE:-origin}"
    url="$(git remote get-url "$remote")"
    
    case "$url" in
      https://github.com/*|http://github.com/*)
        path="${url#*github.com/}"
        ;;
      git@github.com:*)
        path="${url#git@github.com:}"
        ;;
      ssh://git@github.com/*)
        path="${url#*github.com/}"
        ;;
      *)
        printf 'Unsupported or non-GitHub remote: %sn' "$url" >&2
        exit 2
        ;;
    esac
    
    owner="${path%%/*}"
    owner="${owner,,}"
    
    case "$owner" in
      personal-owner)
        token="${GH_TOKEN_PERSONAL:-}"
        ;;
      work-owner)
        token="${GH_TOKEN_WORK:-}"
        ;;
      *)
        printf 'No token mapping for owner: %sn' "$owner" >&2
        exit 2
        ;;
    esac
    
    if [[ -z "$token" ]]; then
      printf 'Token is not set for owner: %sn' "$owner" >&2
      exit 2
    fi
    
    GH_TOKEN="$token" command gh "$@"
  2. Set the tokens in your shell or secret manager, rather than embedding them in the script. For example, export GH_TOKEN_PERSONAL and GH_TOKEN_WORK in the environment used to launch the wrapper.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  3. From a repository, call the wrapper with the usual gh arguments, such as gh-owner auth status or gh-owner repo view. The wrapper passes those arguments to gh after selecting a token from the origin owner.

The owner strings in the case statement are examples; replace them with the actual owners and environment-variable names you control. The script lowercases the owner for matching. It does not validate token permissions or establish which permissions every GitHub operation requires. Check the requirements for the command you intend to run.

Account for remotes, forks, and worktrees

A wrapper’s accuracy depends on how it determines the repository owner. Git repositories can have several remotes, and a fork may have one remote for the fork and another for the upstream project. The example uses origin; changing the remote to inspect changes which owner controls token selection. You can set GH_OWNER_REMOTE for a repository or adapt the script to require an explicit remote.

Worktrees share repository configuration, including remotes, so the same remote-based mapping may apply across worktrees even if you expected different account behavior. Repositories using a GitHub Enterprise host need their own host parsing and token handling; the example only recognizes GitHub.com remotes. Do not assume that a repository owner alone is enough to distinguish credentials if the same owner or repository is accessed through different hosts.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep tokens out of logs and command history

Tokens are credentials. Avoid putting literal token values in a command typed into a shared terminal, committing them to a script, or printing them for debugging. GitHub CLI’s gh auth token manual explains that the command outputs an authentication token for the active account by default and can select a named user. Treat that output as secret: do not pipe it to logs or expose it in shared terminal recordings.

The wrapper uses shell variables and passes the chosen token in the environment of the gh process. This avoids adding the token itself to the command line in the example, but it does not make tokens safe to disclose through shell tracing, debug output, process environments, or poorly protected secret storage. For automation, favor short-lived or narrowly scoped credentials where available, and restrict who can access the environment that supplies them.

When gh auth login is enough

If you do not need automatic owner mapping, use the documented login and account-switching workflow instead. The gh auth login manual describes logging in and notes that gh can use an authentication token found in environment variables. Use gh auth switch when you want to change the active account for a host; use a wrapper only when you want your own repository-owner mapping to select a token for each invocation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.