Sub topic 4
git filter-repo versus filter-branch
This chapter assumes you know basic Git (clone, commit, branch, push) and have read the previous chapters. Those chapters explain why a committed secret stays exposed and why you should rotate the credential first. They also explain that rewriting history gives every rewritten commit a new hash. Here you will see why this tutorial uses git filter-repo and not the older git filter-branch, and how the two differ in behavior.
Two tools for the same job
Git has no built-in command for "rewrite every commit in this repository". Two tools fill that gap.
git filter-branchships with Git. It has existed for many years.git filter-repois a separate Python script that you install yourself. The Git project's own documentation recommends it overfilter-branch.
Both can remove a file or change file contents in every commit. The difference is in how they do it, how fast they are, and how easy it is to get a wrong result without noticing.
How filter-branch works
filter-branch walks the commits you select. For each commit it checks out the tree (or uses a temporary index), runs shell commands you supply, and then creates a new commit from the result. The commands are strings that you pass through the shell.
To remove config/secrets.env from all history, you would write this:
git filter-branch --force \
--index-filter 'git rm --cached --ignore-unmatch config/secrets.env' \
--prune-empty \
--tag-name-filter cat \
-- --allEach part has a job:
--index-filterruns the quoted command against each commit's index without checking out files, which is faster than--tree-filter.--ignore-unmatchstopsgit rmfrom failing on commits that never contained the file.--prune-emptydrops commits that become empty after the removal.--tag-name-filter catrewrites tags to point at the new commits, keeping their names.-- --allapplies the rewrite to every ref. Without it,filter-branchrewrites only the current branch.
The Git manual for filter-branch carries a long warning that lists its problems. The main ones are these:
- Safety. Unusual setups and filter mistakes can silently corrupt history. For example, a filter that fails on one commit may not stop the whole run.
- Performance. Starting a shell for every commit makes it very slow on large repositories. A rewrite that takes hours here can take minutes with
filter-repo. - Usability. Real-world tasks need complicated quoting and many options, and the tool provides little help in verifying the outcome.
There is a second problem that matters for secrets. filter-branch saves the old branch tips under refs/original/. The old commits, which still contain the secret, stay reachable in your local repository until you remove those refs.
How filter-repo works
git filter-repo reads the repository once with git fast-export, lets a Python program change the stream of commits, and writes the result with git fast-import. Nothing is checked out per commit, and no shell is started per commit.
The same task as above is one command:
git filter-repo --invert-paths --path config/secrets.envYou do not need to ask for --all. filter-repo rewrites all refs by default, and it handles tags for you.
Behavior differences that matter for secrets
The design difference produces several practical differences. Some of them directly affect whether a secret is really gone.
Cleanup is automatic
After a run, filter-repo expires the reflog and runs garbage collection with pruning. The old commits that held the secret are no longer in your local .git directory. With filter-branch you must do this yourself:
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=nowIf you forget this, a later git log --all -S'secret' or a copy of the folder can still expose the secret.
It insists on a fresh clone
Before it rewrites anything, filter-repo checks that the repository looks like a fresh clone. It refuses to run if it sees signs of an older working copy, such as extra remotes, stashes, or unpacked loose objects beyond what a new clone has. This check is a safety measure. A repository with local-only work and stray refs is the place where old copies of the secret hide. You can bypass the check with --force, but for secret removal you almost never should. The next chapter covers the fresh-clone setup.
The origin remote is removed
filter-repo deletes the origin remote after a run. This stops you from pushing rewritten history by accident. You must add the remote back deliberately, which a later chapter covers.
Commit references are updated
Rewriting changes hashes, so a commit message such as "reverts a1b2c3d" would point at a commit that no longer exists. filter-repo rewrites such abbreviated hashes in messages to the new ones. It also writes .git/filter-repo/commit-map and ref-map. These files map old hashes to new hashes, which is useful when you need to update links in issues or documents.
Empty commits are pruned
If a commit changed only the removed file, it becomes empty. filter-repo prunes such commits and rewires their children to the nearest remaining ancestor. With filter-branch you must remember --prune-empty.
Signatures do not survive
Both tools produce new commits, so a GPG signature on an old commit no longer matches. filter-repo drops signatures from rewritten commits instead of keeping invalid ones. Expect signed commits and signed tags to lose their signatures in any rewritten range.
Side-by-side comparison
| Aspect | `git filter-branch` | `git filter-repo` |
|---|---|---|
| Installation | Bundled with Git | Separate install (`pip install git-filter-repo`) |
| Status | Discouraged by the Git project | Recommended by the Git project |
| Speed | One shell process per commit, slow | Streams through fast-export and fast-import, fast |
| Default scope | Only refs you name | All refs |
| Old commits afterward | Saved in `refs/original`, reflog kept | Reflog expired, `gc` run, old objects pruned |
| Fresh clone check | None | Refuses to run on non-fresh clones unless `--force` |
| Remote after run | Unchanged | `origin` removed |
| Hash references in messages | Not updated | Rewritten to the new hashes |
| Replace text in files | Custom `sed` in a tree filter | `--replace-text` with an expressions file |
| Path selection | `git rm` inside a filter string | `--path`, `--path-glob`, `--invert-paths` |
Where each tool can still be the wrong choice
filter-repo is not the answer to everything.
- It is a separate dependency. It needs Python 3 and a recent enough Git. The project README lists the current minimum versions, so check them before you plan a rewrite on a locked-down build server.
- It is meant for whole-repository rewrites. If you want to change one recent commit that nobody has pulled,
git commit --amendor an interactive rebase is simpler. This does not apply to a secret that has been pushed and shared, because other copies already exist. - It cannot fix copies you do not control. No history rewrite reaches forks, clones on other machines, or caches on the hosting platform. The chapters on hosting residue and team coordination cover those.
BFG Repo-Cleaner is a third tool that you will often see in older guides. It is fast and simple for common cases, but it is less flexible than filter-repo. This tutorial uses filter-repo because it handles files, text, and commit messages with one consistent interface.
Checking what you have
Before you rely on a tutorial or script, check which tool it calls. These commands tell you whether filter-repo is available:
git filter-repo --version
git --versionIf the first command prints a version or abbreviated hash, the script is installed and on your path. If Git says filter-repo is not a git command, the install step in the next chapter has not been done yet.
To see the leftover refs that filter-branch creates, list them:
git for-each-ref refs/originalEmpty output means no filter-branch backup refs exist in this repository. After a filter-repo run this list is always empty, because the tool does not create them.
Exercise
A teammate removed a secret file with git filter-branch --index-filter 'git rm --cached --ignore-unmatch .env' --prune-empty -- --all, then ran git push --force --all. They say the secret is gone from their machine. Name two things that may still contain the secret locally, and one thing that makes filter-repo safer on these points.
Show answer
Old commits stay reachable through the refs/original/ backup refs and through the reflog, so git log --all -S on the repository or any copy of it can still find the secret until those refs are deleted, the reflog is expired, and git gc --prune=now runs. Tags are also not rewritten unless --tag-name-filter cat was used, so old tags can keep the original commits alive. filter-repo expires the reflog and prunes old objects automatically, and it rewrites tags by default.
Exercise
Why does filter-repo remove the origin remote after it finishes, and what must you do before you can push?
Show answer
It removes origin so you cannot push rewritten history by accident. Before pushing, you must add the remote back with git remote add origin <url>, then force-push the branches and tags on purpose.