To remove a secret from Git history, revoke the secret first, then rewrite every commit with git filter-repo --sensitive-data-removal --invert-paths --path .env (to drop a file) or --replace-text (to blank a string), and force-push with git push --force --mirror origin. The rewrite changes commit hashes and does not reach existing clones or forks, so the leaked value must be treated as public no matter what.
If the secret is a password, token or API key, rotate it before anything else. History rewriting is cleanup; revocation is the fix. See what to do when an API key leaks for that part.
Not pushed yet? You don't need filter-repo
If the commit with the secret exists only on your machine, the fix is local and quick:
- It is the last commit: remove the secret from the file (or
git rm --cached .env), then rungit commit --amend. - It is a few commits back: use an interactive rebase to edit that commit, or
git reset --softto the commit before it and recommit.
The details are in how to undo the last Git commit. Once the commit has been pushed, anyone who fetched it has a copy, and you need the full procedure below.
Why deleting the file in a new commit doesn't work
Git stores every version of every file. A commit that deletes .env only changes the newest snapshot; the earlier commits still contain the file byte for byte. To check whether a string exists anywhere in history, search every commit:
git log --all --oneline -S "sk_test_FAKE1234567890EXAMPLE"
git grep "sk_test_FAKE1234567890EXAMPLE" $(git rev-list --all)
Example output from a throwaway demo repository with a fake key committed in "Add config" and still present in the later "Update app" commit:
77c1b80 Add config
----
4f8677c000b4e5603dbbedb2419eb05b53feb68a:.env:API_KEY=sk_test_FAKE1234567890EXAMPLE
4f8677c000b4e5603dbbedb2419eb05b53feb68a:config.py:KEY = "sk_test_FAKE1234567890EXAMPLE"
77c1b809cdb9dc3718756f7b71a6475fe5783259:.env:API_KEY=sk_test_FAKE1234567890EXAMPLE
77c1b809cdb9dc3718756f7b71a6475fe5783259:config.py:KEY = "sk_test_FAKE1234567890EXAMPLE"
-S lists commits that added or removed the string. git grep over git rev-list --all shows every commit and file where it currently sits.
Install git filter-repo
git filter-repo is a Python tool that is not bundled with Git, so you install it separately. GitHub's guide requires version 2.47 or later for the --sensitive-data-removal flag used below. Any of these work:
brew install git-filter-repo # macOS / Linux with Homebrew
python3 -m pip install git-filter-repo # any OS with Python 3
Check it with git filter-repo --version. The pip package reported version 2.47.0 when we installed it for this article (October 2026). Don't use git filter-branch: the official Git documentation says it "has a plethora of pitfalls" and recommends filter-repo instead.
Step 1: make a fresh clone
filter-repo refuses to run in a working repository you've been committing in. In our demo, running it in the original repo printed:
Aborting: Refusing to destructively overwrite repo history since
this does not look like a fresh clone.
(expected at most one entry in the reflog for HEAD)
Please operate on a fresh clone instead. If you want to proceed
anyway, use --force.
That check protects unpushed work. Push or save anything local, then clone again and work in the new copy:
git clone https://github.com/alex/myapp.git myapp-clean
cd myapp-clean
Step 2a: remove a whole file from every commit
Use the file's full path within the repository, not just its name:
git filter-repo --sensitive-data-removal --invert-paths --path .env
--path .env selects the file and --invert-paths means "keep everything except it". If the file was moved or renamed at some point, add a --path for each old location. Trimmed output from the demo:
NOTICE: Fetching all refs from origin to make sure we rewrite
all history that may reference the sensitive data, via
git fetch -q --prune --update-head-ok --refmap "" origin +refs/*:refs/*
HEAD is now at 96b1754 Update app
You rewrote 2 (of 3) commits.
NOTE: First Changed Commit(s) is/are:
77c1b809cdb9dc3718756f7b71a6475fe5783259
With --sensitive-data-removal, filter-repo first fetches every ref from the remote (not only branches and tags) so nothing that references the secret is missed, and it reports the first changed commit, which you'll need if you contact your Git host. The manual warns that this fetch can discard local-only changes, which is one more reason to work in a fresh clone.
Step 2b: replace a string everywhere it appears
When the secret is inside a file you want to keep, such as config.py, replace the text instead. Write the values to a file outside the repository, one per line:
sk_test_FAKE1234567890EXAMPLE==>REMOVED-SECRET
Then run:
git filter-repo --sensitive-data-removal --replace-text ../replacements.txt
Each line is literal text by default. regex: and glob: prefixes are supported, and without ==> the match is replaced by ***REMOVED***. The replacement applies to every version of every non-binary file. After the demo run, git show HEAD:config.py printed:
import os
KEY = "REMOVED-SECRET"
Step 3: verify, then force-push
Run the same search as before. No output (and exit status 1 from git grep) means the string is gone from every commit you have:
git grep "sk_test_FAKE1234567890EXAMPLE" $(git rev-list --all)
Then overwrite every ref on the remote:
git push --force --mirror origin
On GitHub, errors for refs/pull/ refs are expected because those refs are read-only. If an ordinary branch is rejected, branch protection is the likely cause; GitHub's guide says to disable it temporarily and retry. Treat the push as irreversible.
What the rewrite does not remove
In our demo, after the force-push the remote's branch no longer contained the key. But the old commit object was still on the "server" and still readable by its hash:
$ git cat-file -t 77c1b80
commit
$ git show 77c1b80:.env
API_KEY=sk_test_FAKE1234567890EXAMPLE
Unreferenced objects stay until the server runs garbage collection. Hosted services work the same way, and there are other copies you can't reach:
| Copy | Still has the secret? | What to do |
|---|---|---|
| Old commits on GitHub (by SHA, cached views, pull request diffs) | Yes, until cleaned | Contact GitHub Support with the repo name, number of affected PRs and the First Changed Commit(s) |
| Forks | Yes | Contact fork owners; you can't rewrite their copies |
| Teammates' clones | Yes | They should delete and re-clone, not merge; a merge brings the old history back |
| CI caches, mirrors, backups | Possibly | Check each one |
To count affected pull requests on GitHub, run grep -c '^refs/pull/.*/head$' .git/filter-repo/changed-refs after the rewrite. GitHub's guide notes that Support only helps with sensitive data when rotating credentials can't mitigate the risk, which is another way of saying: rotate.
Side effects to expect
- Every commit from the first changed one onward gets a new hash. Links to old commit SHAs break.
- Signed commits and tags lose their signatures.
- Open pull requests can end up with confusing diffs, so merge or close them first.
- Anyone who pushes during the cleanup can lose work or reintroduce the old commits.
Stop it happening again
- Keep secrets in environment variables or a secrets manager, not in tracked files.
- Add
.envand similar files to.gitignorebefore the first commit. If a file is already tracked, ignoring it does nothing; see why .gitignore isn't working. - Stage files by name instead of
git add .orgit commit -a. - Turn on push protection on GitHub, or run a pre-commit scanner such as gitleaks or git-secrets.
Frequently asked questions
- Is deleting the file in a new commit enough to remove a secret from Git?
- No. A new commit only removes the file from the latest snapshot. Every earlier commit still contains it, and anyone with a clone can read it with git show or git log -p. You have to rewrite history with a tool such as git filter-repo, or treat the secret as public and rotate it.
- Do I need to rewrite history if I already rotated the key?
- Not for security. Once the old key is revoked it no longer grants access, which is why GitHub's documentation says rotating may solve the problem without a rewrite. Rewriting is still worth doing when the data itself is sensitive (personal data, an internal hostname list) or when a scanner keeps flagging the old value.
- Why does git filter-repo say this does not look like a fresh clone?
- filter-repo refuses to rewrite a repository that has local history in its reflog, because the rewrite is irreversible and could destroy unpushed work. Make a fresh clone and run the command there. The --force flag skips the check, but the tool's own manual warns against recommending it.
- Should I use git filter-branch or BFG instead?
- The official Git documentation says git filter-branch has many pitfalls and recommends git filter-repo instead. GitHub's guide on removing sensitive data also uses git filter-repo, version 2.47 or later for the --sensitive-data-removal flag.