Home / Alt manpages / gh-auth-switch(1)

  • gh-auth-switch(1)
  • User command
  • linux

Switch GitHub Accounts Safely with gh auth switch

Push to the wrong repository under the wrong identity and gh auth switch is the command that got you there, or the one that fixes it. You will finish with GitHub CLI using the account you actually meant for a chosen host, plus a quick way to prove which one is active. The examples use GitHub CLI 2.87.3, installed on this machine. Allow about five minutes.

  • You need: an existing gh login with at least two accounts available for the same host.
  • It will not touch: logging an account in, creating a token, changing repository permissions, or altering Git configuration. It only changes which stored authentication entry gh uses.

Warning

It is still security-sensitive. Selecting the wrong account can make every later command read or write through the wrong identity.

1. Check the installed command

Confirm the version and read the local command help first. These are ordinary, read-only commands and need no elevated privileges:

$ gh --version
gh version 2.87.3 (2026-02-23)
$ gh auth switch --help
Switch the active account for a GitHub host.
  • Two options only. --hostname (or -h) chooses the GitHub host, and --user (or -u) chooses the account.
  • No sudo. A root shell sees a different home directory, and therefore a different gh authentication configuration.

Checkpoint

If gh --version reports an older release, treat gh auth switch --help on that installation as the final authority for its actual behaviour.

2. See the accounts and current selection

Before switching anything, ask gh to show the active account for each known host:

$ gh auth status --active
github.com
  - Active account: true

Real output also includes the account name and authentication details. Hosts and accounts are machine-specific, so do not copy an identity from this example. Narrow the check to one host with:

$ gh auth status --active --hostname github.com

If that reports an authentication problem, fix or refresh the login before you switch anything. A switch selects an account gh already knows; it is not a replacement for gh auth login or gh auth refresh.

3. Switch interactively when the host is obvious

Run the command with no options when you want gh to guide the choice:

$ gh auth switch
? Choose an account:  [use arrows to move, type to filter]
  account-one
  account-two
  • Pick and confirm. Select the intended account and accept the prompt; exact formatting is release-dependent.
  • Two accounts on one host? The command can switch between them automatically. More than two means you need --user or the interactive prompt to disambiguate.
  • Read before you click through. Do not mash a prompt just to make it disappear, especially on a machine used for both personal and organisation work.

4. Use explicit host and user values in repeatable work

For a script, runbook or shell history entry, name both values so the target is unambiguous:

$ gh auth switch --hostname github.com --user ACCOUNT_NAME

Replace ACCOUNT_NAME with the exact account name shown by gh auth status. For a GitHub Enterprise Server host, use its own hostname rather than github.com:

$ gh auth switch --hostname github.example.com --user ORG_ACCOUNT

Do not include a protocol, path or trailing repository name in --hostname; the value is just a host such as github.com or github.example.com. Keep account values quoted if your shell input comes from another command, and inspect generated values before running a state-changing command.

The switch itself normally prints nothing useful on success. Treat the exit status as the first check:

$ gh auth switch --hostname github.com --user ACCOUNT_NAME
$ printf 'switch status: %s\n' "$?"
switch status: 0

5. Verify before using a repository command

Run the status check again, restricted to the host you just changed:

$ gh auth status --active --hostname github.com
github.com
  ✓ Logged in to github.com account ACCOUNT_NAME
  - Active account: true

The check may also report the Git operations protocol, token scopes and the local configuration path. Never use --show-token for routine verification: it prints a credential in plain text. Do not paste authentication output into an issue, terminal recording or support ticket.

For an extra identity check against GitHub, run a harmless API request once the host and account are confirmed:

$ gh api user --hostname github.com --jq .login
ACCOUNT_NAME

Checkpoint

This makes a network request but does not modify GitHub data. If the result is not the account you expected, stop before running anything that creates, edits or deletes remote resources.

6. Recover from a wrong selection

Switching accounts is reversible. If verification shows the wrong account, run the same command with the account you actually meant:

$ gh auth switch --hostname github.com --user CORRECT_ACCOUNT
$ gh auth status --active --hostname github.com
  • Missing account? Do not guess another host or edit authentication files by hand. Stop and inspect gh auth status --hostname github.com; you may need gh auth login or gh auth refresh instead, and both have their own prompts and credential consequences.
  • A failed switch proves nothing. Rerun the host-specific status check after any error rather than assuming the previous account is untouched. Common causes: a misspelled user, a mismatch between github.com and an Enterprise host, or an account whose token has expired or lacks the required access.

Done means

  • Correct account confirmed. gh auth status --active --hostname HOST names the account you intended.
  • Right host used. The host value matches exactly what the repository or API request uses.
  • No token exposed. You have not used --show-token.
  • Reversible. You can switch back by repeating gh auth switch with the other known account.