Skip to main content
If a developer accidentally commits a secret to a GitHub repository, treat that secret as fully compromised and rotate/revoke it immediately at the issuing system. After revocation, you can remove the secret from the repository history using git-filter-repo to replace or remove sensitive strings across all commits. This guide demonstrates a safe workflow to:
  • Revoke the exposed secret at the source.
  • Rewrite repository history to remove or replace the secret.
  • Force-push the cleaned history and notify collaborators.
Quick overview of steps: Step 1 — create a demo repository on GitHub (UI) I created a new repository named “test-secret” in my personal account with a README.
A dark-mode screenshot of GitHub’s "Create a new repository" page. The Owner dropdown is open and the repository name is set to "test-secret", with visibility, README, .gitignore, and license options visible.
Step 2 — clone locally and add a secret file Clone the repository and add a file secret.txt containing a mock secret (for demo purposes). Replace <your-user> with your GitHub username in the examples below.
Create secret.txt with:
Add, commit, and push the file:
After this push the secret exists in the repository history and is visible in the commit that added it. Step 3 — remove the file from the tip (but history still contains the secret) If you remove the file and commit that removal, the file disappears from the current tip, but previous commits still contain the secret:
If you inspect commit history on GitHub you will still find the earlier commit that contains password=1234567. Removing the file in a later commit does not purge the secret from the repository history.
Always rotate/revoke the exposed credential immediately at the issuing system before attempting any repository history cleanup.
Step 4 — prepare to rewrite history with git-filter-repo git-filter-repo rewrites repository history. Because history rewriting affects all collaborators and remote refs, follow these recommended precautions:
  • Work on a fresh clone in a separate directory (do not reuse your working copy).
  • Prepare the mapping file for replacements ahead of time.
  • Run git-filter-repo locally and verify the results before pushing.
  • Force-push rewritten refs to the remote.
  • Notify collaborators to re-clone or rebase because history will change.
Install git-filter-repo if you don’t already have it. For many systems:
Create a fresh clone into a different folder (do not use the repository you used earlier):
Step 5 — create the replace-text mapping file git-filter-repo’s --replace-text option accepts a file where each line uses the format OLD==>NEW. You can replace a whole key/value or just the sensitive substring. Ensure the separator is exactly ==>. Example mapping file (e.g., /tmp/mock-expression.txt):
Notes:
  • Do not add extra ==> separators. One ==> per replacement line is required.
  • Lines starting with # are treated as comments.
Step 6 — run git-filter-repo to rewrite history Run git-filter-repo with the --replace-text option pointing to your mapping file:
Expected output (example):
This command scans all commits and replaces matching text according to your mapping. Verify locally that the sensitive string was replaced in all commits. Step 7 — add the remote (if needed) and force-push the rewritten history If your fresh clone already has the remote origin set, you can skip adding it. Otherwise add the remote and force-push all refs and tags:
The force push replaces the remote history with your rewritten history. After pushing, inspect the repository on GitHub — prior commits that contained the secret should now contain the replacement text. Summary of common commands Final notes and cautions
  • Always revoke or rotate secrets immediately after accidental exposure.
  • Test the replacement on a local copy before force-pushing to the canonical remote.
  • After rewriting history and force-pushing, inform all collaborators; they must re-clone or carefully rebase to avoid divergent histories.
  • Use git-filter-repo with care—rewriting history is destructive and affects all users of the repository.
Links and references

Watch Video

Practice Lab