Git Objects: Blobs, Trees, Commits and Tags
What Git really stores: a content-addressed database of four object types. How blob IDs are computed, how trees record names and modes, what a commit object contains, annotated tag objects, why identical files are stored once, and how to inspect any object with cat-file, ls-tree and hash-object.
kvi5hnuCode Sprout team member7 min readOn this page
Every Git command so far has worked on top of one simple design: an object database where each piece of data is stored under the hash of its content. There are only four kinds of objects, and once you've seen them, many of Git's behaviours stop being surprising: why commits are snapshots, why rewriting one commit changes all later IDs, why renaming a file costs nothing, and why a deleted file is still in history. This article opens the database and looks at each object type with Git's plumbing commands, the low-level tools that the everyday commands are built on.
Content-addressed storage
Git stores data under its object ID: a SHA-1 hash, 40 hex characters (or SHA-256 in repositories that use it, article 50), of the object's type, size and content. You can compute one without even creating a repository:
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" | git hash-object --stdin
printf 'blob 16\0Lake loop: 5 km\n' | sha1sum # the same calculation by hand
echo "=== store it, then read it back"
id=$(echo "Lake loop: 5 km" | git hash-object -w --stdin)
find .git/objects -type f
git cat-file -t $id
git cat-file -s $id
git cat-file -p $id
01d9211a15c11495bb00028c22993909aab675e1
01d9211a15c11495bb00028c22993909aab675e1 -
=== store it, then read it back
.git/objects/01/d9211a15c11495bb00028c22993909aab675e1
blob
16
Lake loop: 5 km
- The ID is the SHA-1 of a header,
blob 16plus a zero byte, followed by the 16 bytes of content. The same content always gives the same ID, on any computer. git hash-objectcomputes the ID, and-walso writes the object into.git/objects. It's stored under a folder named after the first two hex characters, in a file named after the remaining 38, compressed with zlib. This is a loose object. Packed objects come in article 50.git cat-filereads objects:-tshows the type,-sthe size, and-ppretty-prints the content.
Because the ID depends on the content, an object can never change: different content would be a different object. This is what makes Git's history tamper-evident.
The four object types
| Type | Contains | Created by |
|---|---|---|
| blob | a file's content, with no name or permissions | git add |
| tree | a folder listing: names, modes and the IDs of blobs and subtrees | git commit (or write-tree) |
| commit | a tree ID, parent commit IDs, author, committer and message | git commit |
| tag | an annotated tag: object ID, type, tag name, tagger and message | git tag -a |
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt
mkdir maps; echo "Lake loop: 5 km" > maps/copy.txt # same content, different name
git add .; git commit -q -m "Add lake page"
git tag -a v1.0 -m "Version 1.0"
echo "=== all objects: $(find .git/objects -type f | grep -vc pack)"
echo "=== the commit"
git cat-file -p HEAD
echo "=== its tree"
git cat-file -p 'HEAD^{tree}'
echo "=== the maps subtree"
git cat-file -p HEAD:maps
echo "=== the tag object"
git cat-file -p v1.0
=== all objects: 5
=== the commit
tree 16e177ce076fec2b4db4ad46c0d348aed95f4dec
author Sam Lee <sam@example.com> 1772445600 +0000
committer Sam Lee <sam@example.com> 1772445600 +0000
Add lake page
=== its tree
100644 blob 01d9211a15c11495bb00028c22993909aab675e1 lake.txt
040000 tree 8caa65f3897c9a19c5a2a1d9cc4d69b357c85ebc maps
=== the maps subtree
100644 blob 01d9211a15c11495bb00028c22993909aab675e1 copy.txt
=== the tag object
object 54552412a1755676546b0764a37760d26fdbc90f
type commit
tag v1.0
tagger Sam Lee <sam@example.com> 1772445600 +0000
Version 1.0
Follow the IDs from top to bottom:
- The commit names one tree, the snapshot of the whole project, then the author and committer with a Unix timestamp and time zone, and the message. A first commit has no
parentline. Later commits have one, and merges two or more. A signed commit also has agpgsigheader (article 39). - The tree lists entries as
<mode> <type> <id> <name>.lake.txtpoints to a blob, andmapsto another tree. - Both files point to the same blob:
lake.txtandmaps/copy.txthave identical content, so Git stores it once. Names live in trees, never in blobs. - The tag object points to the commit and records the tag's name, tagger and message. A lightweight tag (article 38) is just a reference, with no object of its own.
So the commit created four objects (a commit, two trees and one blob) and the tag a fifth.
Tree entry modes
| Mode | Meaning |
|---|---|
100644 | regular file |
100755 | executable file |
120000 | symbolic link: the blob holds the link's target path |
040000 | subfolder (a tree) |
160000 | submodule: a commit ID in another repository, a gitlink (article 41) |
These are the only permissions Git tracks: a file is executable or not. Owners, groups and other permission bits aren't recorded, and empty folders can't be stored, because a tree with no entries isn't written for a subfolder.
Snapshots that share everything unchanged
Each commit records a full snapshot through its tree, yet history doesn't grow by a full copy each time, because unchanged files and folders reuse the same blobs and trees:
git init -q trail-guide && cd trail-guide
mkdir maps; echo "Lake map" > maps/lake.map; echo "Lake loop: 5 km" > lake.txt
git add .; git commit -q -m "Add lake page"
before=$(find .git/objects -type f | wc -l)
echo "Lake loop: 5.4 km" > lake.txt
git commit -q -am "Fix lake distance"
echo "new objects for the second commit: $(( $(find .git/objects -type f | wc -l) - before ))"
echo "=== maps tree in each commit"
git rev-parse HEAD~1:maps HEAD:maps
echo "=== the second commit names its parent"
git cat-file -p HEAD | head -2
new objects for the second commit: 3
=== maps tree in each commit
272e6ca7324f17780aec685287f4a15d94178275
272e6ca7324f17780aec685287f4a15d94178275
=== the second commit names its parent
tree 11f137f760bf78a117f51b9d78c60f7dc9dc38f6
parent 4445fcc1f321295ef686bd3348aa34d1212c2e56
Changing one file created three objects: the new blob, a new top-level tree pointing to it, and the commit. The maps tree has the same ID in both commits, so it's shared. The old lake.txt blob stays, because the first commit still uses it.
This structure is a Merkle tree: each ID covers the IDs below it. A commit's ID covers its tree, which covers every file, and also its parent's ID, which covers all earlier history. That's why changing anything in an old commit gives it, and every commit after it, a new ID (article 47).
Git computes diffs when you ask for them, by comparing snapshots. It doesn't store them. Packfiles do store objects as compressed deltas against similar objects, but that's a storage optimization that's invisible to the object model (article 50).
Exploring objects
| Command | Shows |
|---|---|
git cat-file -t / -s / -p <object> | type, size, content |
git ls-tree [-r] <tree-ish> [path] | tree entries, -r recursively |
git rev-parse <revision> | the ID a name resolves to: HEAD, main~2, HEAD:lake.txt, v1.0^{commit} |
git cat-file --batch-check --batch-all-objects | every object, with type and size |
git count-objects -vH | how many loose and packed objects, and their size |
git show <object> | the porcelain view of any object |
<commit>:<path> names the blob or tree at a path in a commit, and ^{tree} or ^{commit} peels a tag or commit to the type you want (article 29).
What's current
- Repositories can use SHA-256 object IDs (64 hex characters), and Git 3.0 plans to make SHA-256 the default for new repositories (article 50). The object types and structure are the same.
- Git has hardened SHA-1 ("SHA-1DC") since 2.13, detecting the known collision attacks on SHA-1.
git cat-file --batchand--batch-checksupport custom formats and are used by tools to read many objects quickly.
Common questions
Does Git store diffs or whole files? Logically, whole snapshots: each commit's tree points to the full content of every file. Physically, packfiles compress similar objects as deltas. You never see the deltas: every object reads back complete.
Where is the file name stored? In the tree that contains the file. Blobs are content only, which is why identical files share a blob, and why a rename doesn't create a new blob.
Why can't Git track an empty folder?
Trees are only created for folders with tracked content. The usual workaround is an empty placeholder file such as .gitkeep.
Can two different files have the same ID? Only through a hash collision. Git's hardened SHA-1 detects the known attack technique, and SHA-256 repositories avoid the issue entirely.
Summary
- Git is a content-addressed object database: an object's ID is the hash of its type, size and content, so identical content is stored once and objects never change.
- Blobs hold content, trees hold names, modes and IDs, commits hold a tree, parents, people and a message, and annotated tags hold a target and a message.
- Each commit is a full snapshot, but unchanged blobs and trees are shared between commits.
- IDs chain upwards, so changing any old object changes every ID that depends on it.
git cat-file,ls-tree,hash-objectandrev-parseinspect the database directly.
Practice questions
How many new objects does git commit create if you change one file in the top-level folder? And if the file is in docs/guides/?
Answer
At the top level: three, namely the new blob, a new top-level tree and the commit. In docs/guides/: five, namely the blob, new trees for docs/guides, docs and the top level, and the commit. Every tree on the path to the changed file changes, because each holds the ID of the one below. Unchanged folders are reused.
Two files in different folders have identical content. How many blobs are stored, and why?
Answer
One. A blob's ID depends only on its content, so both tree entries point to the same blob. The names are stored in the trees.
What information does a commit object contain?
Answer
The ID of its top-level tree, the IDs of its parent commits (none for a root commit, two or more for a merge), the author and committer with timestamps and time zones, optionally a signature, and the message. File contents aren't in it directly: they're reached through the tree.
Previous: Rewriting History: Authors, Dates, Secrets and Big Files With filter-repo
Next: Refs, HEAD and Plumbing Commands: Building a Commit by Hand

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.