Sub topic 10
Revoke, rotate, then clean up history
This chapter assumes you can read Git history, know what an API key or token is, and have seen a secret scanning alert in the Security tab. It covers what to do after a secret has been committed and pushed.
Why the order matters
Once a secret reaches a remote, treat it as public. Forks, clones, CI caches and anyone with read access may already hold a copy. Rewriting history removes the secret from the branch. It does not remove it from copies that already exist. Only revoking the credential makes the leaked value useless.
The order is therefore fixed:
- Revoke the credential at its provider.
- Rotate it: issue a new one and deploy it.
- Check logs for misuse.
- Clean up history if you still want to.
- Close the alert with an accurate resolution.
Step 1: Revoke
Go to the provider that issued the credential. Disable or delete the key, token or password. Do this first, even if you are unsure whether anyone saw it. A revoked secret cannot be used, so the exposure window closes.
If the alert shows a validity status of active, treat it as urgent. Validity checks are covered in a separate chapter.
Step 2: Rotate
Create a replacement and store it outside the repository, for example in an environment variable or a secrets manager. Update the services that use it. Read it at runtime:
import os
api_key = os.environ["PAYMENTS_API_KEY"]Confirm the application works with the new value before you move on. Otherwise you may be tempted to restore the old one.
Step 3: Check for misuse
Look at the provider's audit or access logs for the period between the commit and the revocation. Note any unfamiliar IP addresses, unexpected API calls or new resources. If you find misuse, follow your organization's incident process.
Step 4: Clean up history
Cleaning history is optional hygiene, not the fix. It reduces the chance that the dead credential confuses future scanners and readers. Tools such as git filter-repo can remove the value from every commit. Rewriting history changes commit hashes, so you must force-push and every collaborator must re-clone or reset their copies.
If the secret was only in your last local commit and never pushed, you can amend or reset it. Rotate it anyway if it was ever written to a shared system, such as a CI log.
Step 5: Close the alert
Close the alert only after the earlier steps are done. The resolution should describe what actually happened:
| Resolution | Use when |
|---|---|
| `revoked` | The credential was revoked at its provider |
| `false_positive` | The match is not a real secret |
| `used_in_tests` | The value is a dummy used in tests |
| `wont_fix` | You accept the risk and will not act |
This command closes alert 42 as revoked:
gh api --method PATCH repos/OWNER/REPO/secret-scanning/alerts/42 \
-f state=resolved \
-f resolution=revokedCommon mistakes
- Deleting the file in a new commit. The secret remains in earlier commits.
- Rewriting history but never revoking the credential.
- Closing the alert as
revokedwhile the key still works. - Rotating the secret but leaving the old one active at the provider.
Exercise
A developer pushed a cloud access key by bypassing push protection. They propose running git filter-repo, force-pushing, and closing the alert as revoked. What is wrong with this plan, and what should the order be?
Show answer
The key is never revoked, so anyone who copied it can still use it, and the alert would be closed with an inaccurate resolution. The order should be: revoke the key at the provider, rotate and deploy a new one, check provider logs for misuse, optionally rewrite history, then close the alert as revoked.