Sub topic 8
Commits: snapshots, parents and metadata
This chapter assumes you can read git cat-file -p output and know that a tree object lists names, modes and hashes. A commit is the object that ties a tree to a point in history.
A commit is a snapshot, not a diff
A commit object stores no changes. It points to one tree, which is the full state of the project at that moment. Git computes diffs on demand by comparing two trees. Unchanged files keep the same blob hash, so they are shared between commits and cost no extra space.
What a commit contains
A commit is a small text object with these parts:
tree: the hash of the root tree.parent: zero or more lines, each the hash of a previous commit.author: who wrote the change, with an email address, a Unix timestamp and a time zone offset.committer: who created this commit object, in the same format.- A blank line, then the commit message.
The author and committer differ after a rebase, a cherry-pick or an applied patch. Git keeps the original author and records you as the committer.
Inspecting commits
This example creates two commits with ordinary commands and then reads them with plumbing.
git init commit-demo
cd commit-demo
git config user.name "Ada Example"
git config user.email "[email protected]"
echo 'hello' > hello.txt
git add hello.txt
git commit -m 'first commit'
echo 'more' >> hello.txt
git commit -am 'second commit'
git cat-file -t HEAD
git cat-file -p HEAD
git cat-file -p HEAD~1
git rev-parse 'HEAD^' 'HEAD~1' 'HEAD^{tree}'The output of git cat-file -p HEAD looks like this. Your hashes and timestamps will differ.
tree 8f1c0e2b5d7a9c3e4f6a1b2c3d4e5f60718293a4
parent 3b18e512dba79e4c8300cebb2bbd1a4b8c0d5e6f
author Ada Example <[email protected]> 1764863426 +0000
committer Ada Example <[email protected]> 1764863426 +0000
second commitThe first commit has no parent line, which makes it a root commit. A merge commit has two or more.
Parents form the history
Each commit names its parents, and parents never name their children. Following parent links backward gives you the history that git log prints. Because a commit's hash covers its parent hashes, changing any old commit changes the hash of every commit after it.
Revision syntax for commits
These forms resolve to objects, and you can pass them to any plumbing command:
| Syntax | Meaning |
|---|---|
| `HEAD^` or `HEAD~1` | First parent of `HEAD` |
| `HEAD^2` | Second parent, for merge commits |
| `HEAD~3` | Three first-parent steps back |
| `HEAD^{tree}` | The root tree of the commit |
Common errors
fatal: unable to auto-detect email addressmeansuser.nameanduser.emailare not set. Set them before committing.HEAD^fails on a root commit because it has no parent.- On Windows
cmd,^is an escape character. Quote it or use~1. - A commit's tree holds the whole snapshot. Do not look for the changes in the commit object itself.
Exercise
A repository has a merge commit M with two parents. Which command prints the tree of M's second parent, and why does that tree not depend on M's own tree?
Show answer
git cat-file -p 'M^2^{tree}'. M^2 selects the second parent commit, and ^{tree} resolves that commit to its root tree. Each commit stores its own tree hash, so the parent's tree is read from the parent commit, not from M.