2. Content-Addressed Storage
Why every object is named by the hash of its contents
In the previous article we saw that every blob, tree, and commit is stored as a file
inside .git/objects/, and that its filename is a hash. We glossed over
why. This is the page where we don't.
Git is a content-addressed filesystem. That phrase means exactly what it sounds like: the address (name) of a piece of data is derived from its content. Change the content by a single byte and the address changes. This single design choice is what makes almost every interesting property of Git fall out for free.
What does "content-addressed" mean?
In a normal filesystem you name a file yourself: report.pdf,
notes.txt. The name has nothing to do with what's inside. Two files with
completely different contents can share a name (in different directories), and the
same file can be renamed without touching its contents.
In a content-addressed store, the name is a function of the content. You don't pick names. You compute them. To store a piece of data, you hash it; the hash is where it lives. To look something up, you need to know its hash.
Filesystem
You give the file a name. Name and contents are independent. Rename = cheap, rewrite = cheap.
Content-addressed
The hash of the contents is the name. Rewrite = new name. Two identical blobs are literally the same file.
Why SHA-1?
Git uses the SHA-1 hash function: every object's name is a 160-bit hash, written as 40 hexadecimal characters. It wasn't chosen for cryptographic reasons — Git doesn't care whether an attacker can forge hashes. (In fact, SHA-1 collisions have been demonstrated, and Git has since gained an option for SHA-256.) It was chosen because SHA-1 has two useful properties:
- Deterministic. The same input always produces the same hash. Run it on any machine, at any time, and you get the same 40 hex characters.
- Wide distribution. Two inputs that differ by a single bit produce hashes that look completely unrelated. The odds of two real, non-adversarial objects colliding are astronomically small.
Those two properties together mean: if two people in different parts of the world commit the exact same file, they independently arrive at the exact same hash. No coordination needed.
What exactly does Git hash?
A common misconception: people assume Git hashes the raw file contents. It doesn't, quite. Before hashing, Git wraps the content with a small header:
The blob framing
<type> <length>\0<content>
The type is blob, tree, or commit. The length
is the byte length of the content, written in ASCII. Then a single null byte
(\0), then the content itself.
So for a file containing the bytes Hello, world!\n (14 bytes):
The header is important because it lets Git tell object types apart. A tree
whose contents happen to encode the byte sequence Hello, world!\n would
hash differently from a blob with those same contents, because the header differs.
So when Git reads an object off disk and sees "tree 240", it knows to parse the
bytes that follow as a tree, not as a blob.
Trees and commits are framed the same way
When Git stores a tree, it serializes the tree entries into a compact binary format and then wraps them the same way:
Same for commits. This uniform framing is why a single implementation of
cat-file can pretty-print any object: parse the header, dispatch on type.
Try It Yourself — Hash a Blob
Type any text below. The demo will frame it exactly like Git does
(blob <length>\0<content>) and compute the SHA-1 hash —
the exact 40 hex characters Git would name this blob. Every keystroke recomputes it.
1. Raw content
2. Git's framing (<type> <length>\0<content>)
3. SHA-1 hash — this is the object's name
printf "%s" "$(cat -)" | git hash-object --stdin on the same content — you'll get the exact hash above.
Go back and edit the text by a single character. Notice how the hash changes completely — not slightly, not in one corner. A single-byte difference in the input scrambles the entire 40-character output. That's the "wide distribution" property. It's why Git never has to worry about near-duplicates accidentally colliding.
What content addressing buys you
1. Integrity for free
Every time Git reads an object off disk, it re-hashes the content and compares it to
the expected filename. If a single bit flipped due to disk corruption, the hash won't
match, and Git refuses to use the object. This is why git fsck (which
verifies every object in the database) is instant — it's just a pile of hash checks.
2. Automatic deduplication
If you commit the same file contents ten times — across different filenames, different directories, different branches — Git stores exactly one blob. All ten references point at the same object. Rename a file, and Git doesn't duplicate anything; it just points two tree entries at the same blob hash.
Why renames are "detected" instead of tracked
Git never records "this file was renamed from X to Y". It doesn't need to. When you
ask git log --follow to trace a file across renames, Git simply notices
that a blob hash disappeared from one tree entry and reappeared at a different path
in a subsequent tree. Rename detection is pattern-matching on hashes, not a separate
metadata channel.
3. Tamper evidence that propagates upward
A commit hash is computed over the commit's contents, which includes its tree hash. The tree hash is computed over its entries, which include its children's hashes. Any change, anywhere, ripples straight up to the commit hash. You can't alter a 10-year-old file inside a history without the terminal commit's hash changing — and once you change the terminal hash, every signature and reference to it breaks.
This is a Merkle tree, the same data structure that powers cryptocurrencies. Git shipped in 2005, several years before Bitcoin; Linus Torvalds was using Merkle trees for version control before "blockchain" was a buzzword.
4. Equality by hash
Two commits have the exact same hash if and only if they have the exact same tree, the
same parents, the same author, the same timestamps, and the same message. This makes
network protocols trivial: when Git wants to push or pull, it just asks "do you have
object abc123…?" No diffs, no renames, no special sauce. Equality is
string comparison on 40 characters.
Where objects live on disk
Inside .git/objects/, every object is stored as a file whose name is its
hash — almost. To avoid putting a million files into one directory, Git splits each
hash after the first two characters:
The file contents are zlib-deflated — Git's on-disk format is
compressed because, well, this is a text file repository. But decompressed, the file
is literally blob 14\0Hello, world!\n. The exact bytes we hashed.
$ cat .git/objects/af/5626b4… | zlib-flate -uncompress blob 14\0Hello, world!
What to take away
- Every object's name is the SHA-1 of its framed content:
<type> <length>\0<content>. - Content addressing gives you integrity, deduplication, rename detection, and tamper evidence for free — no extra machinery.
- The whole repository is a Merkle tree: change any leaf and the root commit hash changes.
- Objects live in
.git/objects/<first-two-hex>/<rest>, zlib-compressed.