Part II: Chapter 2.2

Trees (The Directories)

If Blobs are the files, Trees are the folders. But like everything else in Git, a Tree is just a simple text file under the hood.

Connecting Names to Hashes

Because Blobs don't know their own filenames, Git uses **Tree Objects** to organize them. A Tree object is literally just a list mapping a human-readable filename (like `index.js`) to a cryptographic Blob Hash (like `b2e6...`).

Working Directory (What you see)

↓ How Git saves it under the hood ↓

The Tree Object

Object Hash:
b2xb357851f9eafaa9430c5e7b2f6efb357851
Mode
Type
Blob Hash Pointer
Filename
100644
blob
b2x685a9...
index.js
100644
blob
b2x685a9...
utils.js
100644
blob
b2x1e2b2...
style.css

A Tree is basically a directory file. It maps human-readable filenames to the raw cryptographic Blobs.

The Blob Objects

BLOB OBJECTb2x685a9a8df9eafaa9430c5e7b2f6ef685a9a8d
console.log('hello');
DEDUPLICATED! Used by 2 files.
BLOB OBJECTb2x1e2b2510f9eafaa9430c5e7b2f6ef1e2b2510
body { background: black; }

Blobs strip away filenames. If you have 5,000 files with the exact same content, Git only stores ONE blob.

Reading the Tree

Look at the **Tree Object** in the simulation above. It looks exactly like output from an old UNIX terminal: 100644 blob [hash] index.js.

Hover over any row in the Tree Object list. You'll see exactly which Blob it points to in the database! Notice how both `index.js` and `utils.js` point down to the exact same Blob Hash. This Tree file acts as the blueprint to reconstruct your project directory.

Trees can point to Trees!

What happens if you have a folder inside a folder? Git creates a Tree object that points to another Tree object hash. Its type changes from `blob` to `tree`. This hierarchical nesting is exactly how the 'Merkle Tree' is built.

Seeing a real Tree

If you run ls-tree on any tree hash, or even just passing a branch name like `main` (which resolves to a commit, which resolves to a tree), you can see this exact table natively!

Terminal
$git ls-tree main
100644 blob f9eafaa9430c5e7b2f6ef329f6 index.html
040000 tree 8baefbb00668e092cc60a667 components/
100644 blob c5e7b2f6ef329f6f9eafaa94 package.json