- Revoke the exposed secret at the source.
- Rewrite repository history to remove or replace the secret.
- Force-push the cleaned history and notify collaborators.
Step 1 — create a demo repository on GitHub (UI)
I created a new repository named “test-secret” in my personal account with a README.

secret.txt containing a mock secret (for demo purposes). Replace <your-user> with your GitHub username in the examples below.
secret.txt with:
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.
- 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.
--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):
- Do not add extra
==>separators. One==>per replacement line is required. - Lines starting with
#are treated as comments.
--replace-text option pointing to your mapping file:
origin set, you can skip adding it. Otherwise add the remote and force-push all refs and tags:
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.
- git-filter-repo: https://github.com/newren/git-filter-repo
- GitHub: https://github.com/
- Best practices for leaked credentials: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/recovering-from-compromised-credentials