Sub topic 8
Commits: snapshots, parents and metadata
This chapter assumes you can create commits with git add and git commit. It also assumes you have met blobs and trees: a blob holds file contents, and a tree maps names and modes to blobs and other trees. Here you will look inside a commit, the object that turns a tree into a point in history.
What a commit contains
A commit is a small text object with four kinds of information:
- One
treeline naming the root tree of the project at that moment. - Zero or more
parentlines naming the commits that came directly before it. - An
authorline and acommitterline, each with a name, an email, a Unix timestamp and a timezone offset. - A blank line, then the commit message.
A commit does not store a diff. It points at a complete tree, so it is a snapshot. Tools such as git show compute differences by comparing two trees.
Reading a commit
Create a repository with two commits through the usual commands, then read the objects with plumbing.
git init -b main 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 'Add hello.txt'
echo 'second' > second.txt
git add second.txt
git commit -m 'Add second.txt' -m 'This commit has a body paragraph.'
git cat-file -t HEAD
git cat-file -s HEAD
git cat-file -p HEADThe last command prints something like this. Your hashes and timestamps will differ.
tree 3f2a...e1
parent 9c41...07
author Ada Example <[email protected]> 1764862971 +0000
committer Ada Example <[email protected]> 1764862971 +0000
Add second.txt
This commit has a body paragraph.git cat-file -t prints commit. git cat-file -s prints the size in bytes of the content shown above, without the object header.
The header fields
| Line | Meaning | Count |
|---|---|---|
| `tree` | Hash of the root tree | Exactly one |
| `parent` | Hash of a previous commit | Zero for a root commit, one for most, two or more for merges |
| `author` | Who wrote the change, and when | One |
| `committer` | Who created this commit object, and when | One |
| `encoding` | Message encoding, only if not UTF-8 | Optional |
| `gpgsig` | Signature over the commit | Optional |
| `mergetag` | Embedded copy of a signed tag that was merged | Optional |
Timestamps are seconds since 1970-01-01 UTC. The offset such as +0000 or -0500 records the timezone the person was in. It does not change the moment in time.
Following parents and trees
Revision syntax lets you move through these links without copying hashes.
git cat-file -p 'HEAD^{tree}'
git cat-file -p 'HEAD^'
git cat-file -p 'HEAD~1'
git rev-list --parents -n 1 HEAD
git rev-parse 'HEAD^{tree}' 'HEAD~1^{tree}'HEAD^{tree}resolves to the tree of the commit.HEAD^andHEAD~1both mean the first parent here.git rev-list --parents -n 1 HEADprints the commit hash followed by its parent hashes.- The last command prints two different tree hashes, one per commit.
To see what changed between the two snapshots, ask Git to compare the trees:
git diff-tree --no-commit-id --name-status -r HEADThis prints A second.txt. The blob for hello.txt has the same hash in both trees, so Git does not report it. Unchanged content is stored once and shared between snapshots.
How a commit gets its hash
A commit's hash covers every byte of the object: the tree hash, the parent hashes, both identity lines with timestamps, and the message. You can reproduce the hash yourself. Git hashes the text commit <size>, a NUL byte, and then the content. The hash-object command does that framing for you:
git cat-file commit HEAD | git hash-object -t commit --stdin
git rev-parse HEADBoth commands print the same hash.
Two consequences follow:
- Because the
parentline is part of the content, a commit's hash depends on its whole ancestry. Changing an old commit changes its hash, and so changes the hash of every descendant. This is whatgit rebasedoes when it rewrites history: it creates new commits and leaves the old ones in place. - Because the hash depends on timestamps, two commits with the same tree and message usually differ.
You can see the second point by fixing the inputs. git commit-tree reads the identity and dates from environment variables, so you can control them exactly:
tree=$(git rev-parse 'HEAD^{tree}')
export GIT_AUTHOR_NAME="Ada Example" GIT_AUTHOR_EMAIL="[email protected]"
export GIT_COMMITTER_NAME="Ada Example" GIT_COMMITTER_EMAIL="[email protected]"
export GIT_AUTHOR_DATE="1767225600 +0000" GIT_COMMITTER_DATE="1767225600 +0000"
a=$(echo 'same message' | git commit-tree "$tree")
b=$(echo 'same message' | git commit-tree "$tree")
c=$(echo 'other message' | git commit-tree "$tree")
echo "$a"
echo "$b"
echo "$c"$a and $b are identical, because every input is identical. Git simply writes the same object twice. $c differs because only the message changed. None of these three commits is reachable from a branch yet. A later chapter explains what that means for them. Unset the variables before moving on, or open a new shell.
unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
unset GIT_AUTHOR_DATE GIT_COMMITTER_DATEAuthor versus committer
The two identities answer different questions. The author wrote the change. The committer made this particular commit object. They match for a normal commit. They differ after git cherry-pick, git rebase, git commit --amend by another person, or git am applying a emailed patch. In those cases the author line and its date are carried over from the original, while the committer line gets the current user and time.
Use this when you read history. Sorting by author date gives when the work was written. The committer date gives when the commit was last rewritten.
Merge commits and root commits
A merge commit has more than one parent line. The order matters: the first parent is the branch you were on when you ran git merge.
git switch -c feature
echo 'feature' > feature.txt
git add feature.txt
git commit -m 'Add feature.txt'
git switch main
echo 'main change' > main.txt
git add main.txt
git commit -m 'Add main.txt'
git merge --no-ff feature -m 'Merge feature'
git cat-file -p HEAD
git rev-list --parents -n 1 HEADThe merge commit shows two parent lines. The first is the new main commit and the second is the tip of feature. Its tree contains hello.txt, second.txt, feature.txt and main.txt. Git does not store the merge as a combination of two diffs. It stores the final merged snapshot.
A root commit has no parent line. A repository can have several root commits, for example after git switch --orphan or after merging an unrelated history.
Edge cases
- Empty trees. A commit may point to the empty tree, for example
git commit --allow-empty. The tree object for no entries has a well-known hash, and Git treats it as always present. - Message formatting. The message is arbitrary bytes after the first blank line. Git's tools treat the first line as the subject, but the object format does not enforce this.
- Missing identity.
git commit-treefails if it cannot work out a name and email. Setuser.nameanduser.email, or use the environment variables above. - Signatures. A
gpgsigheader is a signature over the commit content with the signature itself removed. Verify it withgit verify-commit HEAD. - Wrong parent order. Reversing the parents of a merge changes the hash, and also changes what
git log --first-parentshows.
Exercise
You run git cat-file -p HEAD and see no parent line. What does that tell you, and how would a merge commit look different?
Show answer
The commit is a root commit, the first one in its line of history. A merge commit has two or more parent lines, and the first one is the branch you were on when you merged.
Exercise
You create two commits with git commit-tree from the same tree and the same message. The hashes differ. What is the most likely cause, and how can you make them equal?
Show answer
The author and committer timestamps differ, because the commit hash covers the whole content including dates. Set GITAUTHORDATE and GITCOMMITTERDATE to fixed values, and use the same name and email, and the hashes will match.
Exercise
A teammate says "a commit stores the diff from its parent". Using the commands in this chapter, how would you show this is wrong?
Show answer
Run git cat-file -p HEAD and note that the commit holds a tree line and no patch. Run git cat-file -p 'HEAD^{tree}' to see the full listing of the project at that commit. Then run git diff-tree --no-commit-id --name-status -r HEAD to show that the differences are computed by comparing the two trees.