Skip to main content
This lesson defines a concise, multi-step incident-response runbook to revoke a leaked credential, scrub Git history, and purge cached copies so the secret is fully removed. When a developer accidentally commits a password or API key, every second matters. If you discover a private key or a production database password committed to a repository, follow this runbook precisely.
A presentation slide titled "One Accidental Commit." It shows a developer icon and a key symbol labeled "Private key / Production DB password," illustrating accidentally committing secrets.
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.
Slide titled "Revoke the Credential First" showing a flow diagram with a central green checkmark pointing to three source provider cards (AWS, Azure, Stripe) each labeled "Revoked." A small cleanup/delete icon with a red X is shown on the left, indicating revocation before cleanup.

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:
This value remains visible to anyone who can read the repository’s history. To truly remove it, you must rewrite the repository history so the secret is purged from every commit, branch, and tag. Recommended tools:
A slide titled "Rewrite the Story" showing the options "git filter-repo" or "BFG Repo-Cleaner" alongside a simplified Git branch diagram (main, feature/checkout, release/v2.0) with commit nodes and orange key icons being removed at a vertical cut.
Example workflow using git-filter-repo (recommended):
Example using BFG Repo-Cleaner:
Notes:
  • 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.
Be prepared for downstream impact: force-pushing rewritten history will break clones and CI runs that reference old commits. Communicate with your team before and after the rewrite so they know to re-clone or reset.
A slide titled "Force GitHub to Accept It" showing two side-by-side diagrams: the left (green) box labeled "Local – Rewritten" depicts rewritten, linear branch commit histories, and the right (orange) box labeled "GitHub – Old History" shows the original remote branch commits with mismatch/force-push markers.

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.
A clean UI mockup titled "Ask GitHub Support" with two pill buttons labeled "GitHub support" and "Run garbage collection" above a rounded content card. The card header reads "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.
A diagram titled "Prevent It Entirely" showing a code push containing a password being caught by secret-scanning and push-protection, with an orange banner reading "Upload blocked – password detected." The result shown is a clean repository after the upload is blocked.

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 --hard according 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.
For exams and real incident response, memorize and follow this order exactly:
  1. Revoke the credential at the source.
  2. Rewrite the repository history (git-filter-repo or BFG).
  3. Force-push rewritten history to GitHub.
  4. Contact GitHub Support for server-side garbage collection.

Watch Video