Sub topic 10
Revoke, rotate, then clean up history
This chapter covers what to do after a secret has been committed and pushed. It assumes you know basic Git (commits, branches, history rewriting at a high level), can read a secret scanning alert, and know what an API key or token is. Earlier chapters covered how alerts are raised and triaged. Here the focus is the order of the response and why that order matters.
Why the order matters
Once a secret reaches a remote repository, treat it as exposed. Anyone with read access could have copied it. Clones, forks, CI caches, and pull request refs may hold it. Automated scrapers watch public repositories and can pick up a credential within minutes.
Rewriting history removes the secret from where you can reach it. It does not remove copies you cannot reach. Only revoking the credential at its provider makes the leaked value useless. So the order is:
- Revoke the exposed credential so it stops working.
- Rotate: issue a replacement and deploy it to everything that needs it.
- Investigate whether the credential was used.
- Clean up history, if that is still worthwhile.
- Close the alert with an accurate resolution.
Step 1: Revoke
Revoking means invalidating the credential at the service that issued it. Where you do this depends on the secret type:
- Cloud access keys: deactivate or delete the key in the provider's identity console.
- Personal access tokens: delete the token in the account that owns it.
- Database passwords: change the password or drop the user.
- Webhook or signing secrets: regenerate them in the sending service.
Revocation is the step that ends the exposure. If the secret is a type that GitHub's partner program covers, the provider may already have been notified and may have revoked it automatically. Check rather than assume. Look at the provider's console or email.
If you cannot revoke immediately, for example because production depends on the key, rotate in a way that lets you cut over quickly. A short delay with a plan is acceptable. Leaving a key live indefinitely is not.
Step 2: Rotate
Rotation means creating a new credential and replacing the old one everywhere it is used. Work through each consumer:
- Application configuration and environment variables.
- CI/CD secrets, such as GitHub Actions repository, environment, and organization secrets.
- Other repositories, scripts, and teammates' local setups that reuse the same value.
- Any service that stored a copy, such as a deployment platform or a secrets manager.
Store the new value in a secrets manager or in the platform's encrypted secret store, never in the repository. The code should read it at runtime:
import os
api_key = os.environ["PAYMENTS_API_KEY"]This reads the credential from the environment, so the value never appears in the repository.
A common error is to rotate in one place and forget another. The old key then fails in production at the moment you revoke it. List consumers before you revoke, and deploy the new credential promptly. Where possible, use credentials that support two active values at once, so you can add the new one, migrate, and then remove the old one.
Step 3: Investigate
Revoking stops future use. It does not tell you whether someone used the credential before that. Review the provider's audit logs for the exposure window, which runs from the time of the commit to the time of revocation.
Look for:
- Requests from unfamiliar IP addresses or regions.
- Actions the credential's owner would not normally perform.
- New resources, users, or keys created with the leaked credential.
- Large data reads or unusual spending.
The alert helps you define the window. The commit date shows when the secret entered the repository. If the push was bypassed, the alert also records that. If you find signs of misuse, follow your organization's incident process. Revoking and rotating alone is not enough in that case, because an attacker may have created their own access.
Step 4: Clean up history
After the credential is dead, deleting it from history is housekeeping. It reduces the chance that scanners and people keep flagging it, and it keeps the repository tidy. It is not what makes you safe.
Decide whether to rewrite
Consider these factors:
| Factor | Leans toward rewriting | Leans toward leaving it |
|---|---|---|
| Credential state | Revoked, so rewriting is optional cleanup | Revoked, and the repository has many forks and clones |
| Content | Also contains personal or sensitive data, not only a key | Only a dead key |
| Repository | Small team, few forks | Large, widely forked project |
| Compliance | Policy requires removal | No such requirement |
A rewrite changes commit hashes. Every collaborator must re-clone or carefully reset their local copy. Open pull requests may break. Forks and cached copies keep the old history anyway.
If the secret was only in the most recent unpushed commit, you do not need any of this. Amend the commit before pushing.
Removing the secret with git filter-repo
The git filter-repo tool is the approach recommended over the older git filter-branch. It is a separate install. Work on a fresh clone, and keep a backup.
Create a file listing the text to replace. Each line is a literal string, optionally followed by ==> and its replacement:
AKIAEXAMPLEKEY123456==>***REMOVED***Then run it on a fresh mirror clone:
git clone --mirror [email protected]:OWNER/REPO.git
cd REPO.git
git filter-repo --replace-text ../replacements.txtReview the result before you push. Then force-push the rewritten refs:
git push --force --mirrorThe filter-repo tool removes the origin remote on purpose, as a safety measure. You may need to add it back with git remote add origin before pushing.
If the file itself should never have existed, remove the whole path instead:
git filter-repo --invert-paths --path config/secrets.ymlWhat a rewrite does not clean
- Forks and clones that already exist.
- Pull request refs such as
refs/pull/*, which GitHub keeps. Removing those, and purging cached views of commits, requires contacting GitHub Support. - Local copies and CI caches on other machines.
- Build artifacts, logs, and container images that captured the value.
This is the reason the chapter puts revocation first. Cleanup is best effort. Revocation is complete.
Step 5: Close the alert accurately
Secret scanning alerts have a state and, once closed, a resolution. Pick the one that matches what actually happened:
| Resolution | Use when |
|---|---|
| `revoked` | The credential was revoked at its provider |
| `false_positive` | The detected string is not a real secret |
| `used_in_tests` | It is a dummy value in test code |
| `wont_fix` | You accept the risk and will not act |
Closing as revoked is a statement that the credential is dead. Do it only after revoking. Closing it earlier makes your alert list misleading, because reviewers will assume the risk is gone.
You can close an alert from the web interface or through the API. This command resolves alert 42 as revoked:
gh api --method PATCH repos/OWNER/REPO/secret-scanning/alerts/42 \
-f state=resolved \
-f resolution=revokedBefore closing, you can check the alert's current details. Pair this with the validity field, if it is available for the secret type. A validity of active after you believe you revoked the key is a signal that revocation did not take effect, or that you revoked a different key.
gh api repos/OWNER/REPO/secret-scanning/alerts/42 \
--jq '{number, state, secret_type_display_name, validity, push_protection_bypassed}'Common errors
- Cleaning history first. The credential stays live while you rewrite, and copies elsewhere remain valid.
- Revoking without checking consumers. This causes an outage because a forgotten service still uses the old key.
- Skipping the investigation. A key that was never used maliciously and one that was need different responses.
- Closing as
revokedwithout revoking. The alert goes quiet and the risk stays. - Re-introducing the secret. A teammate merges an old branch after the rewrite. Ask everyone to discard old clones.
- Replacing the secret with another secret. Rotating into a new hard-coded value repeats the problem. Use the runtime lookup shown earlier.
Summary
Treat every pushed secret as compromised. Revoke it, rotate to a new credential, check the logs for misuse, and only then decide whether a history rewrite is worth the disruption. Close the alert with the resolution that matches what you did.
Exercise
A teammate pushed an AWS access key to a private repository by bypassing push protection. They proposed running git filter-repo today and revoking the key next week, once the release ships. What would you change, and which resolution would you use when closing the alert?
Show answer
Revoke the key first, or as soon as a replacement can be deployed, because the key stays valid and usable until the provider disables it. A history rewrite does not affect clones, forks, or cached copies. Rotate the key into every consumer, check the provider's audit logs for the window between the commit and the revocation, and then decide whether a rewrite is worth the disruption. Close the alert with the revoked resolution, and only after the key has actually been revoked.
Exercise
After a rewrite and force-push, a collaborator merges a branch from an old local clone. Why is this a problem, and how do you prevent it?
Show answer
The old branch still contains the original commits, so merging it brings the secret back into the rewritten history. Prevent it by asking every collaborator to delete their old clones and re-clone after the rewrite, and by revoking the credential so that any reintroduced copy is already dead.