
Assume the credential is already compromised before you touch the repository. The first action is to neutralize the credential at its source — do not attempt repository cleanup first.
Step 1 — Revoke the Credential Immediately
Before manipulating Git history, go to the credential issuer (for example, AWS, Azure, Stripe) and revoke the leaked token or rotate the secret. Neutralizing the secret immediately prevents further unauthorized access and buys you the time to safely scrub history. Common provider actions:- AWS: delete or rotate IAM keys, revoke session tokens, and rotate affected secrets in Secrets Manager.
- Azure: rotate keys or regenerate credentials for the affected resource.
- Stripe: revoke API keys and recreate new keys.

Step 2 — Remove the Secret from Git History
Simply deleting the file and committing the deletion does not remove the secret from previous commits — Git preserves history. For example:- git-filter-repo — modern, maintained, and flexible: https://github.com/newren/git-filter-repo
- BFG Repo-Cleaner — faster for simple deletions: https://rtyley.github.io/bfg-repo-cleaner/

- Always operate on a mirror clone (
--mirror) so you rewrite all refs, including remote branches and tags. - Preserve a backup of the original repository for forensic or compliance needs before rewriting history.
Step 3 — Force-Push the Rewritten History
After rewriting history locally you must overwrite the remote repository. This requires force pushing every branch and tag to GitHub.
Step 4 — Request Server-Side Garbage Collection from GitHub
Even after overwriting branches and tags, GitHub may retain cached fragments or references in places such as pull request diffs, checks, or activity logs. These artifacts can persist after a force push. To ensure a complete erasure, contact GitHub Support and request a server-side garbage collection to permanently purge lingering cached fragments.
When contacting GitHub Support, state that you have rewritten repository history and request server-side garbage collection to remove cached fragments outside branch history (for example, pull request views or other internal references).
Prevention: Stop Secrets from Entering Git
Prevention reduces the likelihood of this emergency. Use platform protections and local controls to stop secrets from being committed in the first place:- Enable GitHub secret scanning with push protection: https://docs.github.com/en/code-security/secret-scanning
- Add pre-commit hooks (e.g., git-secrets, pre-commit framework) to scan for patterns before commits are made.
- Store secrets in encrypted secrets management systems (AWS Secrets Manager, HashiCorp Vault, Azure Key Vault) instead of code.
- Educate developers on secure credential handling and periodic key rotation.

Quick Runbook (Checklist)
Additional remediation tasks:
- Rotate any other credentials that may have been exposed.
- Scan other repositories and forks for the secret.
- Update CI/CD configurations and environment variables.
- Require developers to re-clone repositories or run
git fetch+git reset --hardaccording to your guidance.
Why deletion alone is insufficient
- Git is immutable: commits remain reachable unless rewritten and garbage-collected.
- Pull request diffs and cached artifacts might still display secret values even after a force push.
- Only server-side GC by the hosting provider can purge some cached fragments.
Useful Links and References
- git-filter-repo: https://github.com/newren/git-filter-repo
- BFG Repo-Cleaner: https://rtyley.github.io/bfg-repo-cleaner/
- GitHub Support: https://support.github.com/contact
- GitHub Secret Scanning: https://docs.github.com/en/code-security/secret-scanning
- Kubernetes & Git best practices: https://kubernetes.io/docs/
- Revoke the credential at the source.
- Rewrite the repository history (git-filter-repo or BFG).
- Force-push rewritten history to GitHub.
- Contact GitHub Support for server-side garbage collection.