GitHub tokens are created under Settings → Developer settings → Personal access tokens, as either fine-grained tokens (recommended by GitHub) or classic tokens. GitHub Apps mint short-lived installation tokens (ghs_) that expire after one hour. Treat every one like a password and prefer the narrowest permissions and shortest expiry.
GitHub key types
| Type | Prefix | Secret? | Scope | Expiry |
|---|---|---|---|---|
| Fine-grained personal access token Limited to 50 per account. Organizations can require approval before a token can read private resources. | github_pat_ | Yes | One resource owner, chosen repositories, and per-permission read/write/admin levels | Can be set to no expiry unless an organization or enterprise lifetime policy forbids it; tokens unused for a year are removed |
| Personal access token (classic) Organizations can restrict classic tokens, and requests then fail with 403. SSO organizations require the token to be authorized. | ghp_ | Yes | Broad OAuth-style scopes such as `repo`; reaches every repository the user can access | Chosen at creation (default options or a custom date); tokens unused for a year are removed |
| GitHub App installation access token GitHub is rolling out a new stateless format ( ghs_APPID_JWT) from April 27, 2026, so do not assume a fixed 40-character length. | ghs_ | Yes | Permissions and repositories granted to the app installation, optionally narrowed per request | Expires after 1 hour |
| GitHub App user access token Refresh tokens use the prefix ghr_. OAuth app access tokens use gho_. | ghu_ | Yes | What the app is permitted to do on behalf of the user | — |
How to create a GitHub key
- Sign in to GitHub, click your profile picture, then Settings.
- Open Developer settings → Personal access tokens.
- Choose Fine-grained tokens → Generate new token (or Tokens (classic) → Generate new token (classic)).
- Set a name, an Expiration, the Resource owner and Repository access (fine-grained) or scopes (classic).
- Click Generate token and copy it; GitHub does not show it again.
Limiting what the key can do
Fine-grained tokens are the narrow option: pick one resource owner, only the repositories needed, and the minimum permission levels. Classic tokens use broad scopes and grant access to every repository you can reach. A token never exceeds its owner's access. For automation inside GitHub Actions, GitHub recommends the built-in GITHUB_TOKEN with an explicit permissions key.
How to rotate a GitHub key
- Generate a new token with the same owner, repositories and permissions.
- Update every system that uses the old token.
- Delete the old token under Settings → Developer settings → Personal access tokens.
GitHub does not document an in-place regenerate flow on the token management page, so rotation means create-then-delete. Installation tokens expire on their own after one hour.
How to revoke a GitHub key
- Go to Settings → Developer settings → Personal access tokens.
- Open Fine-grained tokens or Tokens (classic) and click Delete next to the token.
- Deleting a token that created a deploy key also deletes that deploy key.
If your GitHub key leaked
- Delete the token immediately in Developer settings; GitHub advises rotating the credential first.
- Create a replacement with narrower permissions and update the systems that used it.
- Review the account's security log and the repositories the token could reach for unexpected activity.
- Check the secret scanning alert under the repository's security tab if the token was pushed to a repository.
- Rewrite Git history only if needed; it is slow and often unnecessary once the token is revoked.
Environment variables and CLI
GitHub's SDKs and docs read the key from GH_TOKEN, GITHUB_TOKEN or GITHUB_PERSONAL_ACCESS_TOKEN.
GitHub CLI (gh): gh auth login runs a browser flow by default and stores the token in the system credential store; gh auth login --with-token reads a classic token from stdin; GH_TOKEN suits headless use and fine-grained tokens
Things that catch people out
- Classic tokens reach every repository you can access, so a leaked
ghp_token is far more damaging than a repository-scoped fine-grained one. - Fine-grained tokens can behave confusingly with
gh auth login --with-token; theghmanual suggests settingGH_TOKENinstead. - Several failed authentication attempts in a short period can make the API temporarily reject all attempts, including valid ones, with 403.
- Organizations can block classic tokens or require approval for fine-grained ones, so a token that works on personal repositories may fail on organization repositories.
Common GitHub authentication errors
X-GitHub-SSO
A classic token used with an organization that enforces SAML single sign-on has not been authorized for it, so the API returns 404 or 403 with an X-GitHub-SSO header.
Recent incidents that put GitHub keys at risk
If you were affected by one of these, rotate your GitHub keys using the rotation steps above.
- LiteLLM 1.107.0 on PyPI and Docker carried a credential stealer after a leaked master key 2026-10-21
- tensorlake 0.5.144 on npm shipped a Shai-Hulud variant with a wipe-on-revoke dead-man's switch 2026-10-08
- GhostAction returns: fake security workflows pushed to 30,000+ repos to steal Actions secrets 2026-10-07
- OpenCode on Windows and WSL ran commands from a malicious MCP server during OAuth sign-in 2026-10-04
- OpenCode server's upgrade endpoint let any webpage install an attacker's npm package 2026-09-24
- keyv, flat-cache and cache-manager npm releases carried a CI-secret stealer with a revocation trap 2026-08-04
Using your GitHub key with AI agents
A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.
BotPassword works this way. For a GitHub key, these intents fit:
| Intent | What BotPassword does |
|---|---|
list_credentials | Lists the credentials this agent is granted and what it may do with each. Never values. |
request_access | Asks you for a grant, which you approve with your master password or decline in BotPassword on your desktop, never in the chat. The agent gets a receipt, never a value. Approval on your phone is planned. |
run_with | Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output. |
http_request | Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused. |
cli_login | Lets git, Docker, the AWS CLI and kubectl log in through bpw's credential helper, so the token is fetched when the tool needs it and not written into its config. The helper hands the value to the tool, so the grant is always disclosing. Other tools use run_with; OAuth device logins are planned. |
rotate | Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet. |
capture_secret | Planned, not built yet: will read a newly created key straight from the page or command output into the vault. |
store_secret | Saves a value the agent already holds as a new vault item. |
GitHub also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.
Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents
Frequently asked questions
Where do I find my GitHub personal access token?
You cannot view an existing token. Create one at Settings → Developer settings → Personal access tokens and copy it when it is generated; if you lost it, delete it and create a new one.
What is the difference between a classic and a fine-grained GitHub token?
Classic tokens (ghp_) use broad scopes across everything you can access. Fine-grained tokens (github_pat_) are limited to one owner, chosen repositories and specific permissions, and GitHub recommends them.
Do GitHub personal access tokens expire?
Both kinds let you pick an expiration at creation. Fine-grained tokens can be set to never expire unless an organization policy forbids it, and GitHub removes tokens unused for a year.
How do I log in to GitHub from the command line with a token?
Run gh auth login, or set GH_TOKEN for headless use. For plain Git over HTTPS, use the token as the password.
Can an AI agent use my GitHub key without seeing it?
Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a .env file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with list_credentials, run_with, http_request, so the agent gets the result and never the value.