The object model

Topic 2 of 3 in How Git works7 min readBeginnerFree course

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.

Inside .git/objects: commits, trees and blobs. Commit c9 holds a message, author, parent and tree; it points to its parent commit 7d and to a tree for the project root. The root tree points to blobs for index.html and README.md and to a tree for css/, which points to a blob for style.css. Example commands: git cat-file -t c9e5a0f prints commit; git cat-file -p HEAD^{tree} lists 100644 blob 8ab686e… index.html and 040000 tree 3f1c2d9… css.

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.

Terminal
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:

Terminal
git cat-file -p HEAD^{tree}
Text
040000 tree 3f1c2d9e5b6a7c8d9e0f1a2b3c4d5e6f7a8b9c0d	css
100644 blob 8ab686eafeb1f44702738c8b0f24f2567c36da6d	index.html
  • 100644 is a normal file, 100755 an executable file, 040000 a subdirectory (another tree), 120000 a 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:

Terminal
git cat-file -p HEAD
Text
tree 9c1f4e2a7b3d5f6e8a0c1b2d3e4f5a6b7c8d9e0f
parent 7d02b4e1c3f0a9e8d7c6b5a4f3e2d1c0b9a8f7e6
author Asha Rao <asha@example.com> 1764835200 +0530
committer Asha Rao <asha@example.com> 1764835200 +0530

Add stylesheet

What a commit contains. git cat-file -p HEAD shows a tree line (all the files), a parent line (the commit before), an author line (who wrote it, when), a committer line (who committed it) and the message (why the change was made). The commit’s ID is a SHA-1 hash of all of this: change anything — even one character of the message — and the ID changes.

  • 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
Terminal
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.

Text
.git/objects/ce/013625030ba8dba906f756967f9e9ca394464a

Over 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.

Terminal
git count-objects -vH       # how many objects, and how much space

The 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:

Text
commit c9 ──tree──▶ root ──▶ index.html, css/ ──▶ site.css
   │
 parent
   ▼
commit 7d ──tree──▶ root ──▶ index.html

Everything 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.

Exercise

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.

Terminal
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 types

Summary

  • 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; -t prints its type.
  • Annotated tags are a fourth object type: a named, signed pointer to a commit.