Skip to content

Guide

How to switch GitHub accounts with gh auth switch

The GitHub CLI can stay logged in to more than one account on the same host, with one active at a time. Here's how to add a second account, switch between them, and avoid opening a pull request from the wrong one.

By the iKnowMyMac team · Updated · 5 min read

The short answer

Since gh 2.40, the GitHub CLI keeps several accounts per host. Add the second one with gh auth login, list both with gh auth status, and switch with gh auth switch --hostname github.com --user sam-acme. With exactly two accounts, plain gh auth switch swaps to the other one. The active account is global: every terminal and every repo uses it until you switch again, so check gh auth status before gh pr create.

On this page

Check your gh version

Multiple accounts need 2.40 or later
gh --version

Older versions keep one account per host, and logging in again replaces it. Update with brew upgrade gh if you installed it with Homebrew, or download the latest release from cli.github.com.

Log in to a second account

Run once for each account
gh auth login --hostname github.com

Choose your preferred protocol and sign in through the browser as the second account. gh adds it next to the first instead of replacing it, and makes it the active one.

See which account is active

Every account on every host
gh auth status
Example output (shortened)
github.com
  ✓ Logged in to github.com account sam-acme (keyring)
  - Active account: true
  - Git operations protocol: ssh

  ✓ Logged in to github.com account samdev (keyring)
  - Active account: false
  - Git operations protocol: ssh

For scripts, gh api user --jq .login prints just the active account's name.

Switch accounts

Switch to a named account
gh auth switch --hostname github.com --user samdev

--hostname matters when you also use a GitHub Enterprise Server host; without it gh asks which host you mean if there's more than one. With exactly two accounts on one host, gh auth switch on its own swaps to the other.

What switching changes, and what it doesn't

  • gh pr create, gh issue, gh repo clone, gh api

    Uses the active gh account?
    Yes
  • git push over HTTPS, after gh auth setup-git

    Uses the active gh account?
    Yes: Git asks gh for the login
  • git push over SSH

    Uses the active gh account?
    No: the SSH key decides the account
  • The email in your commits

    Uses the active gh account?
    No: that's user.email in Git's config
What follows the active gh account

Use a different account per folder

gh has no setting per folder, but it reads GH_TOKEN from the environment before its stored accounts. With a tool such as direnv, a work folder can set it from the stored work login:

.envrc in ~/work (direnv)
export GH_TOKEN=$(gh auth token --hostname github.com --user sam-acme)

Every program started from that shell can read the token, so keep this to folders you trust, and don't commit the .envrc with a token written into it.

Log out of one account

Remove one account and keep the other
gh auth logout --hostname github.com --user samdev

The faster way: GitHub CLI account switcher in iKnowMyMac

iKnowMyMac shows the active gh account for each host in the menu bar popover and on its Git screen, and switches it by running gh auth switch for you. It never reads or stores a token.

FAQ

Questions

Something else? support@iknowmymac.com

Is the active gh account per repo or global?

Global, per host. gh keeps one active account for github.com and one for each Enterprise host, and every terminal uses it until you switch. For a different account in one folder, set GH_TOKEN in that folder's environment.

Why does gh auth switch say there's only one account?

Either gh is older than 2.40, which keeps one account per host, or only one account is logged in. Run gh --version, update if needed, then gh auth login for the second account.

Does gh auth switch change who I push as?

Only over HTTPS with gh as Git's credential helper, which gh auth setup-git sets up. Over SSH, the key decides the account, and your commits' email comes from Git's config either way.

Where does gh keep the tokens?

In the macOS Keychain by default, which gh auth status shows as (keyring). If the Keychain isn't available, gh stores them in ~/.config/gh/hosts.yml.

Know why your Mac is slow.

macOS 15 Sequoia or later · Apple silicon. A 11.5 MB native app, notarized by Apple. No account, no analytics, no telemetry.

One-time purchase · every feature · 14-day refund