Sub topic 4
git filter-repo versus filter-branch
This chapter assumes you know how to clone, commit and push. It also assumes you know that a commit's hash depends on its contents and its parent. Earlier chapters covered why a committed secret must be rotated and what a history rewrite changes. This chapter explains why the rest of the tutorial uses git filter-repo and not the older git filter-branch.
Two tools for the same job
Both tools rewrite every commit you select and give each one a new hash. They differ in how they do it.
git filter-branch ships with Git. It checks out each commit, runs your shell command, and records the result. That is slow on large histories. Its behavior also has many edge cases. The Git documentation itself warns against using it and points to git filter-repo as the replacement.
git filter-repo is a separate Python script. It needs Git and Python 3 installed. It reads the whole history as a stream, edits the stream in memory, and writes a new history.
The same task in both tools
Removing config/secrets.env from every commit with filter-branch looks like this:
git filter-branch --index-filter \
'git rm --cached --ignore-unmatch config/secrets.env' \
--prune-empty -- --allWith filter-repo it is one short command:
git filter-repo --invert-paths --path config/secrets.envThe filter-branch version needs a quoted shell script. If you leave out --ignore-unmatch, the command fails on commits that never had the file. If you leave out --prune-empty, empty commits stay behind. If you leave out -- --all, only the current branch is rewritten and other branches keep the secret. filter-repo handles all refs by default.
Differences that matter for secrets
| Concern | `git filter-branch` | `git filter-repo` |
|---|---|---|
| Speed on large history | Slow, one checkout per commit | Fast, streams the history once |
| Default scope | Only the refs you name | All refs |
| Empty commits | Kept unless you add `--prune-empty` | Removed automatically |
| Old objects | Remain in backup refs and the reflog | Reflog expired and garbage collected |
| Safety check | None | Refuses to run on a repository that is not a fresh clone |
| Text replacement | Write your own script | Built in with `--replace-text` |
| Maintenance | Discouraged by the Git project | Recommended by the Git project |
The old-objects row matters most here. After filter-branch, the original commits are still reachable through refs/original/ and the reflog. The secret is still in your local repository until you delete those refs and run garbage collection yourself. filter-repo cleans up after itself.
Behaviors to expect from filter-repo
- It checks for a fresh clone. If the repository looks like it has local history, such as extra reflog entries or stashes, it stops and tells you why. The
--forceflag overrides this. Do not use it on your only copy. - It removes the
originremote. This stops you from pushing rewritten history by accident. You add the remote back before pushing. - It saves a mapping. The folder
.git/filter-repo/holdscommit-mapandref-map, which list old hashes and their new ones. - It updates hashes in commit messages. A message that mentions an old commit hash is rewritten to name the new hash.
Exercise
You run git filter-branch to remove a file and then run git log --all and see no trace of it. Why might the secret still be recoverable from the same local repository, and which tool avoids this by default?
Show answer
filter-branch keeps the original commits reachable through refs/original/ and the reflog, so the old objects stay in the repository until you remove those refs and garbage collect. git filter-repo expires the reflog and prunes the old objects as part of its run.