Sub topic 12
What a Commit Actually Stores
This chapter assumes you can run git init, git add, and git commit, and that you know the three areas: working tree, staging area, and repository. Here you will look at what Git writes into .git when you commit.
A commit is a snapshot, not a diff
Git does not store a list of changes. Each commit points to a complete snapshot of the project as it was staged. Unchanged files are not copied again. The new snapshot reuses the stored content they already have.
A commit holds four things:
- A pointer to a tree, which is the snapshot of the directory structure.
- Zero or more pointers to parent commits. The first commit has none.
- The author name, email, and timestamp, taken from your
git configidentity. - The commit message.
The object types
Git keeps its data as objects in .git/objects. Each object is named by a hash of its content.
| Object | What it holds |
|---|---|
| blob | The contents of one file, with no file name |
| tree | A list of names, each pointing to a blob or another tree |
| commit | A tree, parent commit(s), author, and message |
Because a name is the hash of the content, two files with identical content share one blob. A file that did not change between commits keeps the same blob hash.
Inspecting a commit
You can read these objects with git cat-file. The -p option prints an object in readable form.
mkdir snapshot-demo
cd snapshot-demo
git init
echo "# Demo" > README.md
echo "Some notes." > notes.txt
git add README.md notes.txt
git commit -m "Add README and notes"
git cat-file -p HEAD
git cat-file -p HEAD^{tree}The first cat-file shows the commit. The output looks like this, though the hashes and identity will differ:
tree 8f3c...
author Ada Lovelace <[email protected]> 1764859470 +0000
committer Ada Lovelace <[email protected]> 1764859470 +0000
Add README and notesThe first commit has no parent line. The second cat-file lists the tree, with one blob line for each file. Change only README.md and commit again. Then compare the trees of both commits. The notes.txt entry has the same hash in each.
echo "More details." >> README.md
git commit -am "Expand README"
git cat-file -p HEAD
git cat-file -p HEAD^{tree}
git cat-file -p HEAD~1^{tree}The second commit now shows a parent line holding the hash of the first. That chain of parents is the history that git log walks.
Common misunderstandings
- "A commit stores only what I changed." It stores a pointer to the full tree. Git can compute a diff by comparing two trees, and
git showdoes this for you. - "Commits include my working tree." A commit holds what was in the staging area when you committed. Unstaged edits are not in it.
- "Changing a commit edits it." Objects are never modified, because their name is their content hash.
git commit --amendcreates a new commit with a new hash. The old one is left behind.
Exercise
You change one line in README.md in a project with 200 files and commit. How many new blob objects does Git need for the files, and why does the commit still describe all 200 files?
Show answer
Git needs one new blob, for the new contents of README.md. The other 199 files have identical content, so their blobs already exist. The new commit points to a new tree that lists all 200 files, reusing the existing blob hashes for the unchanged ones.