The object model
Overview
Everything Git stores lives in .git/objects, and there are only a handful of object types. Three of them hold your project’s history: blobs, trees and commits. Understanding how they link together explains what a commit really is.
Blobs: file contents
A blob (binary large object) holds the contents of one file. Just the bytes — not the file’s name, location or permissions. That’s why two identical files anywhere in your project, or the same file across many commits, share a single blob.
git cat-file -p 8ab686e
# <h1>Home</h1>Trees: directories
A tree is a directory listing. Each entry has a mode (file permissions), a type, an ID and a name:
git cat-file -p HEAD^{tree}040000 tree 3f1c2d9e5b6a7c8d9e0f1a2b3c4d5e6f7a8b9c0d css
100644 blob 8ab686eafeb1f44702738c8b0f24f2567c36da6d index.html100644is a normal file,100755an executable file,040000a subdirectory (another tree),120000a symbolic link.- Names live in trees, not blobs. Renaming a file creates a new tree with a different name pointing to the same blob.
- Git tracks files, not folders. An empty folder has no tree entry, so it can’t be committed. (The usual workaround is to add a placeholder file such as
.gitkeep.)
Commits: snapshots with context
A commit ties it together:
git cat-file -p HEADtree 9c1f4e2a7b3d5f6e8a0c1b2d3e4f5a6b7c8d9e0f
parent 7d02b4e1c3f0a9e8d7c6b5a4f3e2d1c0b9a8f7e6
author Asha Rao <asha@example.com> 1764835200 +0530
committer Asha Rao <asha@example.com> 1764835200 +0530
Add stylesheet- tree: the root tree — the complete snapshot.
- parent: the previous commit. The very first commit has none; a merge commit has two (or more).
- author and committer: who wrote the change and who made the commit, each with a timestamp (seconds since 1970, plus a time zone). They differ when, for example, someone applies another person’s patch, or rebases their commits.
- The message, after a blank line.
Notice what a commit doesn’t contain: a list of changes. Diffs are always computed by comparing the commit’s tree with its parent’s.
Following the chain
Git has a convenient syntax for walking these links:
| Expression | Means |
|---|---|
HEAD^{tree} |
the tree of the commit HEAD |
HEAD:css |
the css tree inside that commit |
HEAD:css/site.css |
the blob for that file |
HEAD~1 |
the parent commit |
git rev-parse HEAD:css/site.css # print the blob's ID
git cat-file -p HEAD:css/site.css # print its content(You’ve already used one of these: git show HEAD~2:prices.json in Viewing history.)
Annotated tags
There’s a fourth object type, the tag object, created by git tag -a. It stores a tag name, a message, the tagger and the ID of the commit it marks — and can be cryptographically signed. You’ll meet tags properly in Tags and releases.
Where the objects live
New objects are stored as individual compressed files, named by their ID: the first two characters are a folder, the rest is the file name.
.git/objects/ce/013625030ba8dba906f756967f9e9ca394464aOver time Git bundles them into packfiles in .git/objects/pack/ for efficiency (git gc does this, and Git runs it automatically now and then). That’s why you won’t always find loose files — use git cat-file, never the file system, to read objects.
git count-objects -vH # how many objects, and how much spaceThe big picture
Put together, history is a chain of commits, each pointing to a full snapshot made of trees and blobs, and each pointing back to its parent:
commit c9 ──tree──▶ root ──▶ index.html, css/ ──▶ site.css
│
parent
▼
commit 7d ──tree──▶ root ──▶ index.htmlEverything you do in Git either adds objects to this store (commit, merge, rebase) or moves names that point into it (branches, tags, HEAD). Objects are never changed. The next lesson covers the names.
Remember: blobs are contents, trees are folders, commits are snapshots with a parent. Everything else is a pointer.
Walk from a commit down to a file
In the repository the script creates, use only git cat-file and git rev-parse to follow the chain by hand: start at HEAD, find the commit’s tree, find the css subtree inside it, then the blob for site.css, and print its content. Then find the parent commit and show that its message is “Add home page”. Finally, confirm each object’s type with git cat-file -t.
mkdir objects && cd objects && git init -b main
echo "<h1>Home</h1>" > index.html
git add . && git commit -qm "Add home page"
mkdir css && echo "h1 { color: navy; }" > css/site.css
git add . && git commit -qm "Add stylesheet"
# 1. The commit object
# 2. Its tree (the project root)
# 3. The css subtree, then the blob for site.css
# 4. The parent commit's message
# 5. Object typesmkdir objects && cd objects && git init -b main
echo "<h1>Home</h1>" > index.html
git add . && git commit -qm "Add home page"
mkdir css && echo "h1 { color: navy; }" > css/site.css
git add . && git commit -qm "Add stylesheet"
# 1. The commit object
git cat-file -p HEAD
# tree <id> / parent <id> / author … / committer … / message
# 2. Its tree (the project root)
tree=$(git rev-parse HEAD^{tree})
git cat-file -p "$tree"
# 040000 tree <id> css
# 100644 blob <id> index.html
# 3. The css subtree, then the blob for site.css
css=$(git rev-parse HEAD:css)
git cat-file -p "$css"
blob=$(git rev-parse HEAD:css/site.css)
git cat-file -p "$blob" # h1 { color: navy; }
# 4. The parent commit's message
git cat-file -p HEAD~1 | tail -1 # Add home page
# 5. Object types
git cat-file -t HEAD # commit
git cat-file -t "$tree" # tree
git cat-file -t "$blob" # blobSummary
- A blob stores the contents of one file — no name, no permissions, just the bytes.
- A tree is a directory listing: names, permissions and the IDs of blobs and subtrees.
- A commit points to one root tree, its parent commit(s), the author, the committer and the message.
git cat-file -p <id>prints any object;-tprints its type.- Annotated tags are a fourth object type: a named, signed pointer to a commit.