Sub topic 7
Removing a whole file from all history
This chapter assumes you have rotated the exposed credential, as covered earlier. It also assumes you have a fresh clone with git-filter-repo installed, and permission to force-push to the repository. Rewriting history does not make a leaked secret safe, because anyone who already fetched it still has it. Rewriting reduces further exposure.
Use the approach in this chapter when the secret lives in a file that should never have been committed. Examples are config/secrets.env, a private key, or a credentials JSON export. If the file also holds content you want to keep, use text replacement instead, which the next chapter covers.
How path removal works
git filter-repo replays the history of the repository and writes a new copy of every commit. For each commit it filters the file tree. With --invert-paths, every path you name with --path is dropped and everything else is kept.
The core command is:
git filter-repo --invert-paths --path config/secrets.envWithout --invert-paths, --path does the opposite. It keeps only the named paths and discards everything else. This is a common and destructive mistake. Always include --invert-paths when you want to remove a file.
Three consequences follow from how the rewrite works:
- Hashes change. Changing a commit's tree changes its hash. Every descendant commit gets a new hash too, even if its own contents did not change. Tags that point at rewritten commits are moved to the new commits.
- Commits that become empty are pruned. If a commit only added or removed the secret file, it has no changes left after filtering and is dropped.
- All refs are rewritten. Branches and tags are all processed, not only the checked-out branch.
A runnable example
The script below builds a small repository that leaks a key and then "fixes" the leak with a normal commit. It then clones the repository fresh and rewrites the clone.
set -e
git init demo
cd demo
git config user.name "Demo User"
git config user.email "[email protected]"
mkdir config
echo 'API_KEY=AKIAEXAMPLEKEY12345' > config/secrets.env
echo 'print("hello")' > app.py
git add .
git commit -m "Add app and config"
git rm -q config/secrets.env
git commit -m "Remove secrets file"
git log --all --oneline -S'AKIAEXAMPLEKEY12345'
cd ..
git clone file://"$PWD"/demo demo-clean
cd demo-clean
git filter-repo --invert-paths --path config/secrets.env
git log --oneline
git log --all --oneline -S'AKIAEXAMPLEKEY12345'Before the rewrite, the -S search prints two commits: the one that added the key and the one that removed the file. After the rewrite, git log --oneline shows a single commit with a new hash, and the second search prints nothing. The "Remove secrets file" commit became empty and was pruned.
The file:// prefix matters. A plain local path makes Git hardlink the object store. The prefix forces a normal clone, which is what git filter-repo expects.
Fresh-clone safety check
git filter-repo refuses to run in a repository that does not look like a fresh clone. It checks for signs such as stashes, extra reflog entries, and unpacked loose objects. If it refuses, do not reach for --force first. Make a new clone and run it there. A fresh clone also means your original repository stays untouched until you are sure the result is correct.
Finding every path to remove
Removing the path you can see today is often not enough. Before you rewrite, list every path where the secret has existed.
- Check renames. If
secrets.envwas once called.env.production, removing only the new name leaves the old one in history. Search by content withgit log --all --oneline -S'<secret>'and rungit show --stat <commit>on each hit to see the path at that commit. - Check other branches and tags.
--allongit logincludes them. The rewrite touches them as well. - Check copies. The same file may exist in several directories, such as
deploy/secrets.envandconfig/secrets.env.
The analysis report from git filter-repo --analyze also lists every path that has ever existed, including deleted ones. Look in .git/filter-repo/analysis/path-all-sizes.txt and path-deleted-sizes.txt.
Selecting several paths
You can repeat --path, use patterns, or read paths from a file.
| Option | Matches | Example |
|---|---|---|
| `--path` | An exact file, or a whole directory if you give a directory name | `--path config/secrets.env` |
| `--path-glob` | A shell-style pattern | `--path-glob '*.pem'` |
| `--path-regex` | A Python regular expression against the full path | `--path-regex '^deploy/.*\.key$'` |
| `--paths-from-file` | One rule per line from a file | `--paths-from-file ../paths.txt` |
In --path-glob, * also matches the / character. The pattern '*.pem' therefore matches certs/prod/server.pem as well as server.pem. This is usually what you want for secrets, but check that it does not match files you need.
A combined command looks like this:
git filter-repo --invert-paths \
--path config/secrets.env \
--path .env.production \
--path-glob '*.pem'For longer lists, put the rules in a file kept outside the repository. Plain lines are exact paths. Prefix a line with glob: or regex: for patterns:
config/secrets.env
.env.production
glob:*.pem
regex:^deploy/.*\.key$git filter-repo --invert-paths --paths-from-file ../paths.txtKeeping the file in your working tree
Sometimes the application still needs the file, for example a local .env that is now going to be ignored. git filter-repo removes the file from the working tree as well as from history. After the rewrite, recreate it from your original clone or from your secret manager, and add it to .gitignore so it cannot be committed again:
echo 'config/secrets.env' >> .gitignore
git add .gitignore
git commit -m "Ignore local secrets file"Copy the file back from the original clone, which the rewrite did not change. Use the new, rotated credential in it.
Previewing and checking the result
To see what the tool will do before it changes the repository, add --dry-run. It writes the stream of commits it would produce into .git/filter-repo/ and leaves your refs alone. You can read fast-export.filtered there, and compare it with fast-export.original.
After a real run, check these things:
git log --all --oneline -S'AKIAEXAMPLEKEY12345'returns nothing.git log --all --oneline -- config/secrets.envreturns nothing.git ls-filesshows the files you expected to keep.- The commit count is lower only by the commits that became empty.
The full verification process, including searching every blob, is the subject of a later chapter.
Common problems
- The secret still appears. It was probably in a second path, such as a renamed file, or it was copied into another file's content. Search by content with
-S, then add the missing path or use text replacement. - Too much was removed. You probably left out
--invert-paths, or a glob matched more than intended. Discard the clone, make a new one, and run again with a narrower pattern. - The
originremote is gone. This is intentional.git filter-reporemoves it so you cannot push the rewrite by accident. You add the remote back in the force-push chapter. - Signed commits and tags lose their signatures. The commits are new objects, so the old signatures no longer apply.
Exercise
A teammate runs git filter-repo --path config/secrets.env in a fresh clone, expecting to delete that file from history. What happens instead, and what is the correct command?
Show answer
Without --invert-paths, --path keeps only the named path and removes everything else, so the rewritten repository would contain only config/secrets.env. The correct command is git filter-repo --invert-paths --path config/secrets.env. The teammate should discard the damaged clone and start from a new one.
Exercise
You removed config/secrets.env from history, but git log --all --oneline -S'AKIAEXAMPLEKEY12345' still prints one commit. Name two likely causes and one way to find out which applies.
Show answer
The key may have lived in a file under another name, such as a renamed copy like .env.production, or it may appear in the content of a file you did not remove, such as a script or a README. Run git show --stat on the commit that the search prints to see which path contains the key. Then add that path to --invert-paths, or use --replace-text if the file has content you need to keep.