Sub topic 2
Why rewriting history replaces commits
This chapter assumes you can use git add, git commit, git branch, git log and git status, and that you know what a remote branch is. It explains the one idea that makes every later rebase command predictable: Git never edits a commit. It makes new ones.
Commits are immutable
A commit stores a snapshot of your files, a message, an author, a timestamp and a pointer to its parent commit. Git identifies the commit by a hash computed from all of that content. If any part changes, the hash changes, and the result is a different commit.
The parent pointer is part of that content. So when you change one commit, every commit after it also gets a new parent and therefore a new hash.
A branch is only a movable label that points at one commit. The history you see with git log is what you get by following parent pointers back from that label.
What a rewrite does
Suppose a branch has three commits on top of main:
main: A --- B
\
feature: C --- D --- EYou want to change the message of C. Interactive rebase does this:
- It finds the commits after the base, here
C,DandE. - It creates
C'with the new message andBas its parent. - It re-applies the changes of
Don top ofC', creatingD'. - It does the same for
E, creatingE'. - It moves the
featurelabel toE'.
main: A --- B
\
feature: C' --- D' --- E'
old, still stored: C --- D --- EThe old commits C, D and E are not deleted. Nothing points at them any more, but they remain in the repository for a while. That is why a rebase can be undone, as a later chapter shows.
Seeing it yourself
You can confirm that hashes change. Record the hashes, rewrite the oldest commit, and list them again.
git log --oneline -n 3
git rebase -i HEAD~3
git log --oneline -n 3In the todo list, change pick to reword for the oldest commit and save a new message. The second git log shows different hashes for all three commits, even though you only touched one message. The content of D and E is the same. Their parents differ, so their hashes differ.
Consequences
- Unchanged commits before the edit keep their hashes. Commits at or before the base, such as
AandB, are not touched. - Everything after the first change is replaced. Choose the base commit so it sits just before the oldest commit you want to change.
- Replaced commits can cause conflicts. Re-applying
Don a new parent is a fresh merge of its changes, which may fail. - Shared history is a problem. If a teammate already built work on
C,DorE, their branch still points at the old commits. Your rewritten branch and theirs now disagree about history.
Rewrite or merge?
| Goal | Better choice |
|---|---|
| Tidy your own unpublished commits before review | Interactive rebase |
| Combine work that others have already pulled | Merge, which adds commits and changes nothing existing |
| Undo a change on a shared branch | `git revert`, which adds a new commit that reverses the change |
Exercise
You rewrite the message of commit C in the series C, D, E. Why do D and E also get new hashes, and what happens to the original C, D and E?
Show answer
A commit's hash depends on its content, including its parent pointer. C' is a new commit, so D must be re-created with C' as its parent, which gives D' a new hash. The same applies to E. The original commits are not deleted. They remain in the repository, unreferenced by the branch, until Git cleans them up, so the rebase can still be undone.