Sub topic 3
What rewriting history actually does
This chapter explains the model behind git filter-repo. It assumes you can clone, commit, branch, and push, and that you know commits are identified by hashes. Once you see why a rewrite changes every hash after the edit, the later steps (verifying, force-pushing, telling collaborators to re-clone) make sense.
Commits are immutable
A commit is an object stored under a hash. Git computes that hash from the commit's content: the snapshot of files (a tree), the author and message, and the hash of the parent commit. If any of those inputs change, the result is a different hash, and therefore a different commit.
You can't edit a commit in place. "Rewriting history" means building new commits that look like the old ones, minus the changes you wanted, and then moving your branch names to point at the new commits.
Why one edit changes many hashes
Suppose a secret was added in commit B. Commit C has B as its parent, and D has C as its parent. If you remove the secret from B, you get a new commit B'. The parent hash recorded in C is now wrong, so C must be rebuilt as C' pointing at B'. The same applies to D.
Commits before the edit (A) keep their hashes. Everything from the edit onward gets new hashes, even if those later commits never touched the secret. Tags that point at old commits also have to be moved, or they keep pointing at the old chain.
What the tool does
git filter-repo automates this. It reads the whole repository, passes each commit, file, and message through your filter, and writes a new set of objects. It then points every branch and tag at the new commits. Your filter might drop a path (--invert-paths --path ...), replace text (--replace-text), or rewrite messages (--replace-message). The following chapters cover each of these.
The old objects are not deleted from your local repository straight away. They become unreachable, and Git removes them later during garbage collection. git filter-repo also expires the reflog and runs cleanup so that a freshly rewritten clone does not carry the old data.
What a rewrite does not do
A rewrite changes only the repository you run it in. It has no effect on copies that already exist.
| Copy of the history | Changed by the rewrite? |
|---|---|
| Your local clone, after the rewrite | Yes |
| The remote, after a force-push | Yes, the branches and tags you push |
| Teammates' existing clones | No, they still hold the old commits |
| Forks | No, each is a separate repository |
| Anyone who already read or copied the secret | No |
Old and new histories are unrelated to Git
After a rewrite, the old chain A, B, C, D and the new chain A, B', C', D' share only the unchanged commits before the edit. Git treats the new commits as different objects. If a teammate with the old history pulls or merges your new branch, Git sees two diverging histories. The old commits, including the secret, can be merged back in. This is the reason for the coordination step covered later: collaborators should re-clone instead of merging.
# In a clone you have already rewritten, compare the old and new tips.
# The hashes differ even though the files look the same.
git rev-parse HEADIf you ran git rev-parse HEAD before and after a rewrite, you would see two different values. A changed hash is the quickest sign that the rewrite took effect.
Exercise
A secret was added in commit B, in a chain A, B, C, D. You rewrite history to remove it from B. Which commits get new hashes, and why does D change even though it never touched the secret?
Show answer
B, C, and D all get new hashes. B changes because its content changed. C changes because it records B's hash as its parent. D changes because it records C's hash as its parent. A keeps its hash because nothing before the edit changes.