Objects, blobs, trees and commits

Intermediate · 9 min

Build a commit by hand with plumbing commands to see that Git is a content-addressed store of four object types, and why identical content is only stored once.

Before thisHow Git stores historyThe three areas of Git

CodeLucky · 9 min

Objects, blobs, trees and commits

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodeLuckyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Git's database stores four kinds of objects, each named by the hash of its content: blobs (file contents), trees (directories), commits and annotated tags.
  • A blob holds only file contents, with no name, and the same content always produces the same hash, so identical files are stored once.
  • A tree maps names and modes to blobs and sub-trees, and a commit points to one tree, its parent commits, an author and a message.
  • A commit's hash covers its tree, parents and message, which is why changing anything in history changes every later hash.
  • `git cat-file -p <hash>` prints any object, and the plumbing commands `hash-object`, `write-tree` and `commit-tree` can build a commit without `git add` or `git commit`.

Git is a key-value store with a version-control UI

Everything you do with Git rests on a small idea: a folder that maps a key to a value, where the key is derived from the value. Put in some content, get back a hash. Give the hash, get the content. That is a content-addressable store, and the whole of .git/objects is one.

To prove it, I made a commit using only plumbing, the low-level commands that git add and git commit are built from. Git 2.54, SHA-1 object names (git rev-parse --show-object-format says sha1). Hashes are the same on your machine for the same content, except for anything involving a timestamp or an author.

Blob: file contents, nameless

$ printf 'buy milk\n' > a.txt
$ git hash-object a.txt
033cf0e445d41ea46c78db8a666132423a10fe4b
$ git hash-object -w a.txt
033cf0e445d41ea46c78db8a666132423a10fe4b
$ find .git/objects -type f
.git/objects/03/3cf0e445d41ea46c78db8a666132423a10fe4b

Without -w Git only computes the hash. With -w it also writes the object, in a folder named by the first two hex digits. The hash isn’t magic: it’s the SHA-1 of the text blob 9, a zero byte, and the contents. I checked:

$ printf 'blob 9\0buy milk\n' | shasum | cut -c1-40
033cf0e445d41ea46c78db8a666132423a10fe4b

Same hash. Read the object back:

$ git cat-file -t 033cf0e
blob
$ git cat-file -s 033cf0e
9
$ git cat-file -p 033cf0e
buy milk

Notice what the blob lacks: a file name. It is just bytes. So two files with the same content share one blob. I tested it later with copy.txt and notes.txt, and git ls-files -s showed them both pointing at 7361ecf....

Tree: a directory listing

Names live in trees. A tree entry is a mode, a type, a hash and a name:

$ git update-index --add --cacheinfo 100644,033cf0e...,notes.txt
$ git write-tree
4b5a6788d646b9898b86d398ca456544d8f86a54
$ git cat-file -p 4b5a678
100644 blob 033cf0e445d41ea46c78db8a666132423a10fe4b	notes.txt

I registered the blob in the index under the name notes.txt (the index is the staging area), and write-tree turned the index into a tree object. A sub-directory is an entry of type tree, with mode 040000. In a later commit that contained a sub/ folder, git cat-file -p HEAD^{tree} listed 040000 tree 0479003... sub.

Commit: a pointer with a story

$ git commit-tree 4b5a678 -m "Add first note"
fcaa0691dcac2e47041de0f03ee723c0905298d7
$ git cat-file -p fcaa069
tree 4b5a6788d646b9898b86d398ca456544d8f86a54
author Ada Example <ada@example.com> 1795378995 +0530
committer Ada Example <ada@example.com> 1795378995 +0530

Add first note

A commit names one tree (the whole project snapshot), its parents (none for this first one), who and when, and the message. A second commit would add a line parent fcaa069.... That one line is what links history together. Since the parent’s hash is part of the commit’s text, changing an old commit changes its hash and so the hash of every descendant. This is exactly why rewriting history makes new commits.

A commit exists, but no branch points to it yet. Attach one:

$ git update-ref refs/heads/main fcaa069
$ git log --oneline
fcaa069 Add first note

(git status then showed D notes.txt, because I had never created the file in my working directory. The commit has it, my working tree didn’t. That is the three areas, mismatched on purpose.) How a name like main finds a commit is the subject of refs, branches and HEAD.

Snapshots, not diffs

Each commit points to a complete tree. It looks wasteful, but unchanged files reuse the same blob. In my second commit, a.txt and the earlier notes.txt were the same blob 033cf0e.... Only the changed file and the trees on the path to it were new objects. git cat-file --batch-all-objects --batch-check lists everything the database holds: eight objects (three blobs, three trees, two commits) after two commits. Packfiles then compress similar objects into deltas, which is the next-but-one lesson.

The fourth object type, an annotated tag, is the one you met in tags and releases. It points to a commit and adds a tagger and message.

What the video covers

CodeLucky’s “Git Internals: Demystifying Git Objects (Blobs, Trees, Commits, Tags)” is about nine minutes and was published around 2025. From its title it covers all four object types. I judge English from the title, description and channel only, and I could not read a transcript. CodeLucky also made the refs video in the next lesson and a packfiles video I did not choose. Wait, How?’s three-minute explainer is linked as a runner-up.

Where this fits

Objects are immutable. Names that move, like main and HEAD, are a separate layer: refs, branches and HEAD.

Check yourself

  1. 01What does a blob contain, and what doesn't it contain?

    It contains a file's bytes only. It has no file name, no path and no permissions, which live in the tree that points at it.

  2. 02Two files with identical content are committed. How many blobs does Git store?

    One. The blob's name is the hash of its content, so both tree entries point at the same blob.

  3. 03What are the parts of a commit object?

    A tree hash, zero or more parent hashes, author and committer lines with timestamps, and the message.

  4. 04Why does amending an old commit change the hash of every commit after it?

    Each commit's hash includes its parent's hash. A new parent hash makes a new commit, then a new hash for its child, and so on down the chain.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark