Systems / Git

How Git stores your code (and why a branch is 41 bytes)

A commit does not save a diff. Git writes a handful of small files named after the hash of their own contents, then writes 41 bytes to move a branch. Once you see that, half of Git stops being strange.

MockStruct engineering notes~7 min readYou have run git commit before

Run `git commit` and it feels like Git saved your changes — a diff, tucked away somewhere, that it can replay later. It didn't. Git wrote a few small files whose names are the hash of their own contents, then wrote 41 bytes to move a branch. Almost every confusing thing about Git gets simpler once you have seen those files.

Prefer to learn by doing?

Type a file, watch Git's own object IDs appear, and assemble a commit whose hash you can verify against Git on your own machine.

Open the 5-minute lesson →

Everything is an object named after itself

Git stores content in objects, and an object's name is the SHA-1 of its own bytes. To hash a file, Git does not hash the file — it hashes a short header followed by the contents: the word `blob`, a space, the length in bytes, a zero byte, then the data. Hash that, and you have the object ID.

This is the whole trick. The name is derived from the content, so identical content anywhere in your history is the same object and is stored exactly once. Two branches containing the same file do not contain two copies of it. They contain the same name, twice.

content:  hello\n                       (6 bytes)
hashed:   "blob 6\0hello\n"
SHA-1:    ce013625030ba8dba906f756967f9e9ca394464a

# check it yourself, no repository needed:
printf 'hello\n' | git hash-object --stdin

A tree is just a directory listing

A blob has no name and no permissions — it is only content. The filename lives in a tree object, which is Git's version of a directory. Each entry is a mode, a space, the filename, a zero byte, and then the 20 raw bytes of the child object's ID. A tree can point at blobs and at other trees, which is how a nested directory becomes a single hash.

Rename a file and its blob does not change at all; only the tree does. That is why Git can detect renames after the fact rather than recording them, and why moving a large file between directories costs you a new tree and nothing else.

Primary reading: Pro Git — Git Internals: Git References · Pro Git — Git Internals: Git Objects

A commit is a small text file

A commit object is plain text: the tree it points at, the parent commit or commits it came from, an author line, a committer line, a blank line, and your message. That is all a commit is. It names one complete snapshot of your project, plus where it came from.

Because the ID is the hash of all of that, changing any part of it produces a different commit. Fix a typo in a message with `git commit --amend` and you have not edited a commit — you have created a new one, and every commit after it gets a new ID too, because they named the old parent. “Rewriting history” is not a metaphor; it is a consequence of content addressing.

tree 57e9529754dc514a3ec10db2ff882018fbe1fcbf
author Ada <ada@example.com> 1700000000 +0000
committer Ada <ada@example.com> 1700000000 +0000

First commit

And a branch is 41 bytes

Open `.git/refs/heads/` and you will find one file per branch. Inside is a single 40-character object ID and a newline. Forty-one bytes. Pro Git puts it plainly: a branch is “a simple pointer or reference to the head of a line of work”. Creating one writes one small file; deleting one removes it. Nothing is copied, because the commits were already stored by content.

`HEAD` is the same idea one level up: usually a file containing `ref: refs/heads/main`, a pointer to a pointer. Checking out a different branch rewrites that one line. Two caveats before you go counting bytes on your own machine: Git packs refs into a single `packed-refs` file once there are many of them, and a repository created with SHA-256 stores 64-character IDs instead of 40.

$ cat .git/refs/heads/main
1a410efbd13591db07496601ebc7a059dd55cfe9

$ cat .git/HEAD
ref: refs/heads/main

What this buys

Three things fall out of naming objects after their contents. Deduplication is automatic, because identical content cannot be stored twice. Integrity is automatic, because a corrupted byte changes the hash and no longer matches the name it is filed under. And branching is close to free, which is the reason Git's branching model could be as casual as it is — it is cheap because of a storage decision, not because of a clever branching feature.

The lesson builds these objects in your browser and shows you the IDs Git would produce. You do not have to take that on trust: every hash it shows you can be reproduced with `git hash-object` on your own machine.

Turn the idea into a small project

In an empty repository, run `git hash-object -w` on a file, then `git cat-file -p` the hash it gives back. Commit, then use `git cat-file -p HEAD` to walk from the commit to its tree to its blobs. Finally, `cat .git/refs/heads/main` and count the bytes. Then make the same file in a second branch and confirm with `git cat-file -p` that both branches name the identical blob.

One question to take with you

If you commit the same unchanged file on two branches, how many blob objects does Git store — and how would you prove it?

Test your understanding →

Read next / Engineering problems

The thundering herd: when a 99% hit rate still takes down your database

Read it →

Published edition 2026-10-31.