Sub topic 3
What rewriting history actually does
This chapter assumes you know basic Git: you can clone, commit, branch, and push, and you know that commits have hashes. The rest of the tutorial rewrites history with git filter-repo. Before you run it, you need a clear picture of what "rewrite" means. Git never edits an old commit. The tool builds a new set of commits, then moves your branch and tag names onto them.
Commits are fixed by their content
A commit is an object whose ID is a hash of its contents. You can inspect those contents:
git cat-file -p HEADThe output has these fields:
tree: the ID of the snapshot of every file in the commit.parent: the ID of each parent commit. A merge commit has more than one.authorandcommitter: name, email, and timestamp.- The commit message.
Git computes the commit's ID from all of these fields. If any field changes, the result is a different ID. That makes a commit tamper-evident, and it is why Git has no "edit this old commit in place" operation.
The snapshot works the same way. The tree ID is a hash of the file names and the IDs of the file contents (blobs). Change one character in one file and the blob ID changes. The tree ID changes with it, and so does the ID of the commit that points at the tree.
Why one change touches every later commit
A commit's ID includes its parent's ID. Suppose you remove a secret from commit C2 in this history:
C1 <- C2 <- C3 <- C4 (main)The new C2 has a different tree, so it gets a new ID, which we call C2'. C3 names C2 as its parent. Its parent field must now name C2', so C3 also gets a new ID, C3'. The same happens to C4. Every descendant of the edited commit is replaced, even if its own files did not change.
Commits before the edit are not affected. C1 has the same content and the same parent (none), so it keeps its ID.
The old chain does not vanish when the new one is created. The old commits still exist as objects in the repository, but no branch or tag points at them. Git deletes unreachable objects only when garbage collection runs. Until then, the secret is still on disk, and anyone who has the old commit's ID can read it.
What git filter-repo does step by step
git filter-repo does not walk the history one commit at a time and amend it. It works as a pipeline:
- It exports all history as a stream with
git fast-export. - It applies your filters to the stream. A filter might drop a path, replace text in file contents, or change a commit message.
- It imports the filtered stream with
git fast-import, which creates the new commits. - It points every branch and tag at the new commits.
- It expires the reflog and runs garbage collection, which removes the old objects from this clone.
Two side effects are worth knowing before you run it:
- Empty commits are pruned. If a commit's only change was the file you removed, nothing is left of it after filtering, and it is dropped. Its children are attached to its parent.
- The
originremote is removed. The tool does this on purpose, so you cannot push the rewritten history to the wrong place by accident. A later chapter shows how to add the remote back.
Seeing it happen
The script below builds a small repository, adds a secret in one commit, removes it in the next, and then rewrites a clone with --invert-paths. It needs git-filter-repo installed, which a later chapter covers. The --no-local flag makes the clone a real copy, so git filter-repo accepts it as a fresh clone.
set -e
rm -rf demo demo-clean before.txt after.txt
git init -q demo
cd demo
git config user.name "Demo"
git config user.email "[email protected]"
echo "print('hello')" > app.py
git add app.py
git commit -q -m "Add app"
echo "KEY=AKIAEXAMPLEKEY12345" > secrets.env
git add secrets.env
git commit -q -m "Add config"
git rm -q secrets.env
git commit -q -m "Remove config"
echo "print('bye')" >> app.py
git commit -q -am "Update app"
cd ..
git clone -q --no-local demo demo-clean
cd demo-clean
git log --format='%h %s' > ../before.txt
git filter-repo --invert-paths --path secrets.env
git log --format='%h %s' > ../after.txt
diff ../before.txt ../after.txt || true
cat .git/filter-repo/commit-mapYour hashes will differ from anyone else's, because they depend on timestamps. The shape of the result is the same every time:
- "Add app" keeps its hash. Its content and parents did not change.
- "Add config" and "Remove config" are gone. Each changed only
secrets.env, so both became empty and were pruned. - "Update app" appears with a new hash. Its content is the same, but its parent is now "Add app" instead of "Remove config".
The file .git/filter-repo/commit-map lists each old commit ID next to its replacement. Pruned commits map to an all-zero ID. You can use this file later to translate an old hash, for example one quoted in a ticket, into the new one.
Which things change and which do not
| Item | After a rewrite |
|---|---|
| Commits before the first changed commit | Same IDs |
| The changed commit and all its descendants | New IDs |
| Commits that became empty | Removed |
| Branch and tag names | Same names, pointing at new commits |
| Commit and tag signatures (GPG, SSH) | Gone, because the signed content changed |
| Hashes written inside commit messages | Rewritten to the new IDs where the tool can match them |
| Old objects in this clone | Removed by the tool's cleanup |
| Old objects in other clones, on the host, and in forks | Unchanged |
The last row is the main limit of the whole procedure. A rewrite changes your repository. It does not reach into anyone else's copy.
Common mistakes at this stage
- Expecting a normal
git pullto fix collaborators. Their branches contain the old commits. A pull tries to merge old history with new history, and it can bring the secret back into your clean repository. Later chapters cover the correct instructions for collaborators. - Rewriting a working repository you still need. The tool deletes the old objects in its clone. Run it on a fresh clone, so your original is the backup.
- Assuming a new commit that deletes the file is the same thing. A deleting commit adds one more commit on top of the chain. The old commits stay reachable, and so does the secret. Only a rewrite removes it from reachable history.
- Forgetting that tags are history too. A tag that points at an old commit keeps that commit reachable.
git filter-reporewrites tags along with branches, but a tag created later by a collaborator on the old history could reintroduce the old commits.
Exercise
A branch has commits A <- B <- C <- D <- E. Only commit C contains a leaked API key, and you remove the key from C during a rewrite. Which commits keep their original IDs, and which get new IDs? Explain why.
Show answer
A and B keep their IDs. Their content and parents are unchanged. C gets a new ID because its tree changed. D and E also get new IDs even though their own files are unchanged, because each commit's ID includes its parent's ID. The new parent ID for D changes D's hash, and that changes E's hash in turn.
Exercise
After running git filter-repo in a fresh clone, a teammate says, "My clone still has the secret, but yours doesn't. Isn't that the same repository?" What do you tell them?
Show answer
They have the same repository name, but not the same history. The rewrite created new commits in your clone and moved your branches onto them. Your teammate's clone still has the old commits, and so does any fork or host cache made before the rewrite. The secret is gone only from the clone that was rewritten, until you force-push, and even then old copies elsewhere survive. This is why the secret must be revoked and why collaborators must re-clone.