Sub topic 2
Logging in and checking your account
This chapter assumes you have a GitHub account, a shell such as bash or zsh, and basic Git usage. You also need gh installed. Every later chapter depends on gh knowing who you are, so we start with authentication.
How gh authenticates
gh talks to GitHub's API on your behalf, so it needs a credential. It stores an OAuth token or a personal access token (PAT) for each host and account you sign in with. A host is a GitHub server, such as github.com or a GitHub Enterprise Server address.
You can supply the credential in three ways:
| Method | When to use it |
|---|---|
| Interactive `gh auth login` | Your own machine, first-time setup |
| Pasting a token during login | Environments where a browser is not available |
| `GH_TOKEN` environment variable | Scripts and CI, covered in a later chapter |
Logging in
Run the login command and answer the prompts:
gh auth login
gh auth statusThe prompts ask for four things:
- The host:
GitHub.comor a GitHub Enterprise Server address. - The Git protocol for operations such as clone and push: HTTPS or SSH.
- Whether to authenticate Git with your credentials. Answering yes lets
git pushover HTTPS reuse theghtoken. - The login method: through the web browser or with a pasted token.
With the browser method, gh prints a one-time code. You open the URL it shows, enter the code, and approve access. With the token method, you paste a PAT you created beforehand.
Checking your account
gh auth status reports every host and account gh knows about. For each one it shows:
- The host name.
- The account name that is logged in.
- Whether the token is valid.
- The Git protocol in use.
- The token's scopes.
Read this output whenever a command behaves unexpectedly. Most surprises come from using the wrong account or a token that has expired.
Several accounts and hosts
You can run gh auth login more than once, for example for a personal account and a work account, or for github.com and an Enterprise Server. gh keeps all of them. One account per host is active at a time. Use these commands to manage that:
gh auth switch
gh auth login --hostname github.example.com
gh auth status --hostname github.example.comgh auth switch changes the active account. The --hostname flag points a command at a specific host instead of the default.
Common errors
HTTP 401: Bad credentials: the token is expired or revoked. Rungh auth loginagain.HTTP 403or a missing-scope message: the token lacks a scope. Re-run login and approve the extra scopes, or create a new PAT.- Commands act on the wrong account: run
gh auth statusand thengh auth switch. GH_TOKENoverrides your login: if theGH_TOKENenvironment variable is set,ghuses it instead of the stored credential.gh auth statusshows this, and unsetting the variable restores the stored login.
Logging out
To remove stored credentials for a host, run gh auth logout. This deletes the local token. It does not revoke the token on GitHub, so revoke a PAT in your GitHub settings if you no longer trust it.
Exercise
You run gh pr list and get HTTP 401: Bad credentials, but you logged in yesterday. Which command shows the state of your login, and what is the most likely fix?
Show answer
Run gh auth status to see the host, account, and whether the token is valid. The token has most likely expired or been revoked, so run gh auth login again. Also check whether a stale GH_TOKEN environment variable is overriding the stored login.
Exercise
You have a personal account and a work account both logged in on github.com. How do you change which one gh uses?
Show answer
Run gh auth switch and pick the account. Confirm the result with gh auth status.