Sub topic 7
Commits: snapshots, parents, and metadata
This chapter assumes you have read the chapters on blobs and trees. You should know that a tree object lists names, modes, and object IDs. It also assumes you can run commands in a Unix-style shell.
What a commit stores
A commit is a small text object. It does not contain files or changes. It holds a pointer to one tree, pointers to its parent commits, and some metadata. The tree is the complete snapshot of the project at that moment.
Git shows you diffs, but it does not store them. When you run git show, Git compares the commit's tree with its parent's tree and computes the difference at that time.
Reading a commit
Run this script in a scratch directory. It builds two commits by hand and prints the second one.
git init commits-demo && cd commits-demo
git config user.name "Ada Example"
git config user.email "[email protected]"
blob=$(echo 'hello' | git hash-object -w --stdin)
tree=$(printf '100644 blob %s\tgreeting.txt\n' "$blob" | git mktree)
first=$(echo 'first commit' | git commit-tree "$tree")
second=$(echo 'second commit, same tree' | git commit-tree "$tree" -p "$first")
git cat-file -p "$second"The output has this shape. Your IDs and timestamps will differ.
tree 3f1c...
parent 9a7e...
author Ada Example <[email protected]> 1764862210 +0000
committer Ada Example <[email protected]> 1764862210 +0000
second commit, same treeThe fields are:
| Field | Meaning |
|---|---|
| `tree` | The root tree of the snapshot. Exactly one per commit. |
| `parent` | A previous commit. Zero, one, or more lines. |
| `author` | Who wrote the change, with a Unix timestamp and a timezone offset. |
| `committer` | Who created this commit object, in the same format. |
| message | Follows a blank line and runs to the end of the object. |
Both commits point at the same tree. Git allows this, and it is why a commit is a snapshot and not a diff. The second commit records no changes, but it still has its own ID because its parent line differs.
Parents shape history
The number of parent lines sets the kind of commit:
- No parent: a root commit, the first one in a line of history.
- One parent: an ordinary commit.
- Two or more parents: a merge commit. The order matters, and the first parent is the branch you merged into.
Author and committer
The author and committer are often the same person, but they can differ. Commands such as git rebase, git cherry-pick, and git am create new commits from existing changes. They keep the original author and the original author date. They set the committer to you and the committer date to now.
Why metadata changes the ID
A commit's ID is the hash of its header and its full text, as for every object. The tree ID, parent IDs, author, committer, timestamps, and message are all part of that text. Changing any one of them gives a different commit ID.
This is why git commit --amend and git rebase produce new commits. The old commits still exist in the object database until Git removes unreachable objects.
Common errors
commit-treefails with "Author identity unknown". Setuser.nameanduser.email, as the script does.commit-treerejects an argument that is not a tree. Pass a tree ID, not a commit ID or a blob ID.- Forgetting
-pcreates a root commit. Your history looks disconnected. - Giving
-pa tree ID fails, because a parent must be a commit.
Exercise
You cherry-pick a colleague's commit onto your branch. Which fields of the new commit match the original, and which differ? Is the commit ID the same?
Show answer
The author, author date, and message match, and the tree matches only if the surrounding files are the same. The parent and the committer and committer date differ. The commit ID differs, because the parent and committer lines are part of the hashed content.