What to do when you commit a secret
One line: revoke and rotate the key first, then clean the history. Deleting the file does not bring back a key that was already copied.
1. Rotate first
The moment a value is exposed it may already be in someone else's hands. Revoke it in the service console and issue a new one before rewriting any history.
Read-only keys are no exception: read access is still a data breach.
| Service | Action |
|---|---|
| AWS | Deactivate then delete the IAM access key; check CloudTrail |
| GitHub | Settings → Developer settings → revoke the token |
| Slack | Reinstall the app, regenerate tokens and webhooks |
| Stripe | Roll the key in the dashboard; regenerate the webhook secret |
| Database | Change the password and terminate active sessions |
2. Assess the exposure
Work out when the key became visible and who could reach it. Check cloud audit logs, API usage logs and billing alerts.
On a public repository, minutes matter: automated scanners fetch new commits within seconds.
3. Remove it from history
A new commit that deletes the file leaves the value in earlier commits. Rewrite history with a tool such as git filter-repo, force-push, then ask the team to re-clone.
GitHub can purge cached views on request, but forks and clones are separate. That is why rotation comes first.
# Remove the file from history (example)
git filter-repo --path .env --invert-paths
# Or replace the value
git filter-repo --replace-text replacements.txt
4. Prevent a repeat
Add .env to .gitignore and commit only .env.example. Run a scanner in a pre-commit hook or CI so the commit never lands.
Agree as a team that configs pasted into issues, pull requests and chat are always masked first.
- .gitignore: .env, .env.local, *.pem
- pre-commit with gitleaks or detect-secrets
- Enable GitHub secret scanning and push protection
- Keep a masked snippet template for sharing
5. Close the loop
Check whether the exposed key caused charges or access, and record the incident. If the same mistake keeps happening, change the procedure rather than the rule.
Questions
Is a private repository safe?
No. Collaborators, forks, backups and CI logs can leak it. Rotate even in a private repository.
Can I just amend the commit?
If you have not pushed, yes. Once pushed, assume it was cloned and rotate first.
Sources
Every example here can be pasted into the workbench as it is.
Open the workbench