How to rotate GitHub tokens: PATs, fine-grained tokens and App keys
GitHub has several kinds of credential, and they rotate differently. Personal access tokens are replaced by hand. GitHub App private keys can overlap. Installation tokens expire every hour on their own, which is why they're the one to move to.
- Token formats
ghp_classic PAT,github_pat_fine-grained PAT,gho_OAuth,ghu_user-to-server,ghs_server-to-server (installation),ghr_refresh token.- Where
- Settings → Developer settings → Personal access tokens → Fine-grained tokens or Tokens (classic).
- Revoke
- Click Delete next to the token. Deleting a token that created a deploy key deletes that key too.
- Expiry
- Set at creation; defaults to 30 days. Organizations can enforce a maximum lifetime. Tokens unused for a year are removed.
- Permissions
- Fine-grained tokens: one owner, chosen repositories, read/write/admin per permission. Classic tokens: scopes, across every repo you can reach.
- App private keys
- Up to 25 per GitHub App, no expiry: generate a new one, deploy it, then delete the old one.
- Installation tokens
- Expire after 1 hour, and can be narrowed to specific repositories (up to 500) and permissions.
- Keysquid
- Automatic for GitHub App installation tokens. Personal access tokens are stored with rotation reminders.
Which GitHub credential do you have?
| Credential | Lifetime | How it rotates | Use it for |
|---|---|---|---|
| Classic PAT (ghp_) | Until it expires or is deleted | Create a new one, swap, delete the old | Avoid: it reaches every repo you can |
| Fine-grained PAT (github_pat_) | You choose; default 30 days | Create a new one, swap, delete the old | Personal scripts against a few repos |
| GitHub App private key (PEM) | No expiry | Generate a second key, deploy, delete the first | The app's identity; signs JWTs |
| Installation token (ghs_) | 1 hour | Request a new one from the app | Services, CI, bots |
GitHub’s own advice: use fine-grained tokens instead of classic tokens whenever possible, and for access on behalf of an organization or for long-lived integrations, use a GitHub App. A rotation is a good time to make that move.
Rotate a personal access token
- Create the replacement under Settings → Developer settings → Personal access tokens. For a fine-grained token, pick the resource owner, only the repositories it needs, the minimum permissions, and an expiration.
- Swap it in wherever the old one is used. In GitHub Actions that’s usually a repository or organization secret:
gh secret set DEPLOY_TOKEN --repo acme/api # paste the new value when prompted gh secret set DEPLOY_TOKEN --org acme --visibility selected --repos api,web - Run the workflow or script once with the new token.
- Delete the old token from the same settings page. There is no overlap setting to manage: the old token works until you delete it or it expires.
Leaked a token? Delete it first, then create the replacement. Check the repositories it could reach for pushes, new deploy keys, new webhooks and Actions runs you don’t recognise. GitHub’s guide to remediating a leaked secret covers cleaning up history.
Rotate a GitHub App private key
Because an app can hold up to 25 private keys at once, this rotation can be seamless:
- Open the app’s settings: Developer settings → GitHub Apps → Edit → Private keys, and click Generate a private key. GitHub downloads the PEM file and keeps only the public half.
- Deploy the new PEM to whatever signs the app’s JWTs.
- Confirm the app still authenticates (for example, by requesting an installation token).
- Delete the old key. An app must always keep at least one.
Better: stop rotating, use installation tokens
An installation token is requested from your GitHub App with a short-lived JWT signed by the app’s private key, and it expires after one hour. You can narrow each token to specific repositories and permissions when you request it:
curl -s -X POST "https://api.github.com/app/installations/$INSTALLATION_ID/access_tokens" \
-H "Authorization: Bearer $APP_JWT" -H "Accept: application/vnd.github+json" \
-d '{"repositories": ["api"], "permissions": {"contents": "read"}}'The private key becomes the only long-lived secret, and it never needs to leave the service that mints tokens.
How Keysquid handles GitHub credentials
Automatic Keysquid creates, verifies, distributes and revokes GitHub App token keys on a schedule, with no one in the loop. Short-lived installation tokens instead of a long-lived personal access token.
Setup, as the app shows it:
- Create a GitHub App with only the permissions your service needs and install it on the repositories.
- Generate a private key for the app and paste it here.
- Writes to: Installation token (default variable GITHUB_TOKEN), in every platform and secret store you map it to.
- Interval: from every 30 minutes to every 50 minutes, subject to your plan’s minimum.
- Note: Installation tokens expire after one hour, so this key rotates every 30–50 minutes.
Every rotation runs in the same order: create the new key, verify it, write it everywhere, redeploy where a platform needs it, and only then revoke the previous key after a grace period. If any write fails, everything written so far is restored and the new key is revoked. The value is encrypted to the agent you run, so Keysquid’s servers never see it.
Personal access tokens can’t be created through GitHub’s API, so Keysquid doesn’t rotate them. It recognises ghp_ and github_pat_ values when you import an environment, stores them like any other secret, keeps every platform in sync when you paste a new one, and reminds you on schedule. The better path is the one above: a GitHub App, which Keysquid rotates on its own.
Frequently asked questions
Can I rotate a GitHub personal access token automatically?
Not through GitHub's API: personal access tokens are created in your account settings. You can automate distributing the new token, but the cleaner fix is a GitHub App, whose installation tokens expire after an hour and are minted on demand.
What's the maximum lifetime of a fine-grained token?
You choose the expiration when creating it, and it defaults to 30 days. Organizations and enterprises can set a maximum lifetime policy that blocks longer ones, including non-expiring tokens.
How do I revoke a GitHub token?
Go to Settings → Developer settings → Personal access tokens, open Fine-grained tokens or Tokens (classic), and click Delete next to the token.
What do ghp_, github_pat_ and ghs_ mean?
ghp_ is a classic personal access token, github_pat_ a fine-grained personal access token, and ghs_ a server-to-server token such as a GitHub App installation token. gho_, ghu_ and ghr_ are OAuth, user-to-server and refresh tokens.
How many private keys can a GitHub App have?
Up to 25. That's what allows a zero-downtime rotation: generate a new key, deploy it, then delete the old one.