skip to content

With the GitHub CLI on one laptop, how do you use a personal github.com account and a work Enterprise Server host without mixing identities?

level: seniorimportance: should knowfreq 15%

answer

  1. one login per host
  2. the remote names the host
  3. outside a clone, a default
  4. one active account per host
  5. switching is manual

basics

~20 s

Log in per host with gh auth login --hostname; gh keeps an active account per host and infers the host from the repository's remote. Outside a clone, use --hostname, -R or GH_HOST, and gh auth switch between accounts on one host.

solid answer

~50 s

Run `gh auth login` for github.com and `gh auth login --hostname ghe.example.com` for the work host; each host gets its own entry in `hosts.yml`, with its own accounts, active account and `git_protocol`, and its own tokens in the credential store. Inside a clone gh reads the remote, so work repositories reach `ghe.example.com` with the work token automatically. Commands with no repository context, such as `gh repo list`, default to github.com unless you pass `--hostname` (on `gh api`), `-R ghe.example.com/acme/api`, or set `GH_HOST`. When two accounts share a host, logins are additive since gh 2.40.0 and `gh auth switch --hostname github.com --user <login>` changes the active one; that also changes the token Git gets through gh's helper. gh never switches by directory and never sets Git's commit email. For one-off calls, `GH_TOKEN=$(gh auth token --user <login>)` borrows an inactive account's token.

go deeper

for a junior

Remember that gh logs in per host with gh auth login --hostname and that inside a repository it uses the host the remote points at.

for a middle

Explain how gh picks the host outside a clone (default github.com, GH_HOST, --hostname, -R HOST/OWNER/REPO) and what gh auth switch changes.

for a senior

Show how you keep identities apart in practice: one active account per host, gh auth token for one-off calls, explicit hosts in scripts, and Git's own commit identity set separately.

for a principal

Discuss when separate hosts, separate machines or separate OS users are a better boundary than switching accounts in one gh configuration.

## The model: hosts, accounts and one active account per host The GitHub CLI stores credentials **per host and per account**. Each host it has logged in to — github.com, a `ghe.com` subdomain, or an Enterprise Server host such as `ghe.example.com` — is an entry in `hosts.yml`, and each entry lists one or more accounts with exactly one **active** account. The active account's token is what gh sends to that host, and, when gh is Git's credential helper, what Git sends too. So a personal github.com account and a work Enterprise Server account never compete: they live under different hosts. Two accounts on the *same* host — a personal and a work account both on github.com — are the case that needs switching. ## Setting it up 1. `gh auth login` for the personal github.com account. 2. `gh auth login --hostname ghe.example.com` for the work host, with `--git-protocol` set to whatever the work repositories use. 3. Optionally `gh auth setup-git`, so Git over HTTPS uses gh's token for both hosts. 4. `gh auth status` to see both hosts, their accounts, and which one is active on each. ## How each command picks its host | Situation | Host gh targets | How to steer it | |---|---|---| | Inside a clone of a work repository | the host in the Git remote, `ghe.example.com` | nothing to do | | Outside any clone (`gh repo list`, `gh api`) | github.com by default | `--hostname` on `gh api`, or `GH_HOST=ghe.example.com` | | Naming a repository explicitly | the host in `HOST/OWNER/REPO` | `-R ghe.example.com/acme/api` or `GH_REPO` | | A script using environment tokens | per host family | `GH_TOKEN` for github.com, `GH_ENTERPRISE_TOKEN` for the Enterprise Server host | If you are logged in only to the work host, most commands aimed at github.com still fail: gh expects a login on each platform you target, even for read-only work on public repositories. ## Two accounts on one host Since gh 2.40.0, `gh auth login` is **additive**: logging in as a second account on a host adds it and makes it active instead of replacing the first. From then on: - `gh auth switch` changes the active account; with exactly two accounts on the host it simply toggles, with more it needs `--user` or a prompt. `gh auth switch --hostname github.com --user alice-work` is the scriptable form. - `gh auth token --user <login>` prints a specific account's token without switching, so `GH_TOKEN=$(gh auth token --user alice) gh api user` runs one command as that account. - `gh auth logout` on the active account switches to a remaining one where possible. - `gh auth refresh` acts only on the active account, so refreshing another account means switching to it first. ## An example session ```bash gh auth login --hostname ghe.example.com --git-protocol https gh auth status --active GH_HOST=ghe.example.com gh repo list acme gh pr list -R ghe.example.com/acme/api gh auth switch --hostname github.com --user alice-work ``` The first line adds the work host beside the existing github.com login, and `--active` shows just the account each host will use. The third and fourth lines run outside any clone, so they name the host explicitly; the last one changes which of two github.com accounts is active, which affects github.com only. ## What gh deliberately does not do - **No automatic switching.** gh does not change accounts based on the current directory or remote; the active account on a host stays active until you switch it. - **No commit identity.** Switching does not touch Git's `user.name` or `user.email`. Who commits are attributed to stays a Git configuration matter, so a switched gh account can still push commits carrying your personal email. - **No per-account settings.** `git_protocol` is per host, so setting it during a second login changes it for every account on that host; editor and pager settings are not per account either. ## Habits that keep identities apart - Keep personal and work repositories on different hosts where the organisation allows it; then gh's host inference does the work. - Before acting outside a clone, check `gh auth status --active` for the host you are about to touch. - In scripts, prefer `-R HOST/OWNER/REPO` or `--hostname` to relying on whatever account happens to be active.

  • Why can a switched gh account still push commits that show your personal email?
    gh authenticates the push with the active account's token, but the commit's author was written by Git from `user.name` and `user.email` when you committed. gh's switching never changes those settings, so you set the work identity in Git yourself.
  • How do you run a single command as the inactive account without switching?
    Borrow its token for that command only: `GH_TOKEN=$(gh auth token --user alice) gh api user`. The variable takes precedence over the stored active account for that one process, and the stored state is left as it was.

  • gh switches accounts automatically based on the current directory
  • gh auth switch also updates Git's user.email
  • A second gh auth login on a host replaces the first account
  • Outside a clone, gh queries every host it is logged in to
  • git_protocol can differ between two accounts on the same host

Assumes gh 2.102; several accounts per host, the additive gh auth login and gh auth switch arrived in gh 2.40.0, before which each login replaced the host's only account.