Most read operations work without authentication, but anything that writes data (creating issues, PRs, pushing code) requires you to log in first.
Logging In
gh auth loginThis starts an interactive flow:
- Choose the host —
github.comor GitHub Enterprise Server - Choose the authentication method:
- Web browser (recommended) — generates a one-time code, opens your browser, and authenticates via OAuth
- Paste a token — useful for CI/CD, scripts, or custom permission scopes
- Choose the default Git protocol — HTTPS or SSH
Non-Interactive Login
For scripts and CI environments, skip the prompts:
# Login with a token from stdin
echo $MY_TOKEN | gh auth login --with-token
# Login with specific options
gh auth login --hostname github.com --git-protocol ssh --webChecking Auth Status
gh auth statusOutput shows:
- Which host you are authenticated to
- Your GitHub username
- The token type and scopes
- The Git protocol in use
Switching Accounts
If you have multiple GitHub accounts (e.g., personal and work):
# Add another account
gh auth login
# Switch between accounts
gh auth switch
# Switch to a specific account on a specific host
gh auth switch --hostname github.com --user myworkRefreshing Tokens
If your token expires or you need additional permission scopes:
# Refresh with an interactive browser flow
gh auth refresh
# Add a specific scope
gh auth refresh --scopes repo,read:org
# Add the project scope (required for gh project commands)
gh auth refresh -s projectViewing Your Token
gh auth tokenThis prints the raw token to stdout — useful for piping into other tools:
# Use the token with curl
curl -H "Authorization: token $(gh auth token)" https://api.github.com/userConfiguring Git Credentials
gh auth setup-gitThis configures git to use gh as a credential helper, so git push, git pull, etc. automatically use your gh token.
Logging Out
gh auth logout
# Log out of a specific host
gh auth logout --hostname github.comWhere Tokens Are Stored
Configuration files live in ~/.config/gh/ (or $GH_CONFIG_DIR if set):
| File | Contents |
|---|---|
hosts.yml | Authenticated hosts, usernames, OAuth tokens, git protocol |
config.yml | Global preferences (editor, pager, aliases, etc.) |
Security warning: Never share or commit your token. If you accidentally expose it, run
gh auth refreshimmediately to rotate it.
Token Scopes
When creating a personal access token manually (Settings → Developer settings → Tokens), enable at least:
| Scope | Purpose |
|---|---|
repo | Full access to repositories |
read:org | Read organization membership |
project | Manage GitHub Projects |
admin:public_key | Manage SSH keys |
gist | Create and manage gists |
The browser-based login flow automatically requests the scopes gh needs.
Exercises
- Run
gh auth loginand authenticate via the browser flow - Run
gh auth statusand confirm your username appears - Run
gh auth setup-gitto configure Git credential integration - Run
gh auth tokenand verify a token is printed (do not share it!)