Sub topic 8
The object graph and why objects never change
This chapter assumes you can read git cat-file -p output and know that blobs, trees, and commits are named by a hash of their content.
Objects point to each other by ID
A commit stores the ID of its tree and the IDs of its parent commits. A tree stores the IDs of its blobs and subtrees. Following these IDs gives you a directed graph: each object points to the objects it depends on, and nothing points back.
The graph has no cycles. An object's ID is a hash of its content, and that content includes the IDs it points to. An object would have to contain its own hash to point to itself, which is impossible.
Watching the graph grow
Create two commits where only one file changes between them.
git init graph-demo && cd graph-demo
git config user.name "Demo"
git config user.email "[email protected]"
echo 'one' > a.txt
echo 'two' > b.txt
git add a.txt b.txt
git commit -m 'first'
echo 'three' > b.txt
git commit -am 'second'
git ls-tree HEAD~1
git ls-tree HEAD
git cat-file -p HEADCompare the two ls-tree listings. The blob ID for a.txt is identical in both, so Git stored that content once and both trees reference it. The ID for b.txt differs, because its content differs. The root tree IDs also differ, since each tree's content lists different blob IDs.
The last command shows the second commit. It has a tree line and a parent line holding the ID of the first commit.
Why objects never change
An object cannot be edited in place, because its name is derived from its content. If you change one byte, you get a different hash, and that is a different object. The old object stays where it was.
This has a chain effect on history. A commit's content includes its parent's ID. Suppose you alter an old commit. It gets a new ID, so every commit after it must also change, because each one embeds its parent's ID. The result is a new chain of commits with new IDs.
This is what git commit --amend and git rebase do. They do not modify commits. They create new commits and move a branch to them.
Reachability
The graph is walked from refs such as branches, tags, and HEAD. An object is reachable if some ref leads to it by following pointers. You can list every reachable object with:
git rev-list --objects --allIn the demo repository this prints two commits, two trees, and three blobs, one each for a.txt, b.txt version 1, and b.txt version 2.
Common errors
- Expecting a commit to hold a diff. A commit points to a full tree. Tools compute diffs by comparing two trees.
- Thinking an amended commit is the same commit. It has a different ID, and anyone who has the old one now has diverging history.
- Assuming unreachable means gone. The objects usually remain on disk until garbage collection prunes them.
Exercise
You change only the contents of b.txt and commit. Which objects in the new commit are new, and which are reused from the previous commit?
Show answer
The new blob for b.txt, the new root tree (its listing contains a new blob ID), and the new commit are all new. The blob for a.txt is reused, because its content and therefore its ID are unchanged. The old commit's tree is also still referenced as the parent's tree.
Exercise
Why does rewriting an old commit change the IDs of all the commits after it?
Show answer
Each commit's content includes its parent's ID. A new parent ID changes the child's content, so the child gets a new hash, and the change passes on to every later commit.