Sub topic 4
What rebase does: replaying commits with new hashes
This chapter assumes you can read git log output, know that a commit stores a pointer to its parent, and have used branches and origin/main. It explains what Git does internally during a rebase. The later chapters build on that.
The working model
A rebase does not move commits. It copies them. Given git rebase main on a branch called feature, Git does the following:
- It finds the merge base, the latest commit that
mainandfeatureshare. - It lists the commits on
featurethat come after that base. - It saves each commit's changes (its diff) as a temporary patch.
- It checks out the tip of
mainin a detached HEAD state. - It applies the patches one at a time, creating a new commit for each. This works like
git cherry-pick. - It moves the
featurebranch pointer to the last new commit and reattaches HEAD to it.
The result is a straight line of history: main, then your commits on top.
Why the hashes change
A commit's hash is computed from its content, which includes its parent hash, its tree, its author, its message, and its timestamps. The replayed commit has a different parent, so it gets a different hash. This happens even when the diff and the message are identical.
You can see it directly:
git switch feature
git log --oneline main..feature
git rebase main
git log --oneline main..featureThe two listings show the same messages with different hashes:
d4e5f6a Add session tests
a1b2c3d Add login form
9f8e7d6 Add session tests
5c4b3a2 Add login formThe old commits are not deleted. Nothing points to them any more, but they stay in the object database until garbage collection removes them, usually after weeks. Git also records the pre-rebase tip in ORIG_HEAD, and the reflog records it as well. That is why a rebase can be undone, a topic covered in a later chapter.
Common errors
- Thinking the branch was "moved". Anyone who still holds the old commits, such as a teammate or a remote branch, holds different commits from yours. If both versions end up merged together, the same change appears twice.
- Expecting one conflict. Each commit is replayed separately, so you can hit a conflict on commit 1 and again on commit 3. Resolving the conflict in the final state does not clear the earlier stops.
- Replaying a change that already exists. If
mainalready contains an identical change, Git drops the duplicate automatically. This is normal and not a sign of data loss. - Reading the "ours" and "theirs" labels backwards. During a rebase, "ours" is the branch you are replaying onto, and "theirs" is your own commit being applied. This is the reverse of what a merge shows.
Checking your work
After a rebase, git range-diff compares the old and new series commit by commit. It shows which commits changed content and which only changed parents:
git range-diff main ORIG_HEAD HEADA commit that shows as unchanged was replayed cleanly. One marked as modified was altered, usually by a conflict resolution.
Exercise
You run git rebase main on feature, and every commit on feature now has a new hash even though git diff ORIG_HEAD HEAD shows no differences. Why did the hashes change?
Show answer
Each commit's hash includes its parent's hash. Rebase gives every replayed commit a new parent, starting with the tip of main. The new parent changes the first commit's hash, and each later commit's parent is then also new. The file contents match, so the diff is empty, but every commit is a new object.