Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Foundations

How does Git work inside? (Snapshots, not changes)

Git does not save your edits. It saves snapshots made of blobs, trees and commits, each named by a hash. Here is the simple version.

Derpx0619 Nov · 4 min read
Contents
Doesn't that waste space?Where does Git keep everything?What are blobs, trees and commits?What does one commit look like?So what's new when I edit one file?Can I look at this myself?Where do the files actually sit?So what is a branch then?Sources
0% read · 4 min leftBack to top ↑
On this page0%
Contents
Doesn't that waste space?Where does Git keep everything?What are blobs, trees and commits?What does one commit look like?So what's new when I edit one file?Can I look at this myself?Where do the files actually sit?So what is a branch then?Sources

Many people learn Git as a list of commands to copy and paste: add, commit, push. But under those commands sits a small, simple storage system. Once you see it, Git stops feeling like magic.

The big surprise: Git does not store your changes. It stores snapshots, a picture of all your files at the moment you commit. The Pro Git book states Git thinks of its data as a "stream of snapshots".

Diff ideaA common guess: each commit is a list of edits (+ this line, - that line).
→
Snapshot ideaWhat Git does: each commit points to a full picture of the project.

Doesn't that waste space?

Good question. Say you commit 500 files and change one. Does Git store all 500 again? No. Git's own docs say that if a file has not changed, Git "doesn't store the file again, just a link to the previous identical file".

A photo album

Picture an album where every page shows a whole shelf of books. If only one book moved, you do not photograph the other books again. You point to the old photos of them.

How does Git know a file is "identical"? That is the next trick.

Where does Git keep everything?

In a key-value box. The book calls Git "a content-addressable filesystem". That means you put in some content, and Git gives you back a key to find it later. The key is made from the content itself.

That key is a hash. A hash is a fixed-length code worked out from the content, like a fingerprint. Git uses a SHA-1 hash: 40 characters made of 0-9 and a-f.

The same content always gives the same key. Change one letter and the key is completely different. Git also notes that you cannot change a file without Git knowing.

So the box holds things named by fingerprints. What kinds of things?

What are blobs, trees and commits?

Git's box holds a few kinds of objects. Three matter most.

Commit: who, when, why, plus one tree
Tree: a folder, listing names and what they point to
Blob: just the contents of one file
Top to bottom: each layer points at the one below.

A blob is the raw contents of one file. It has the contents only, not the file name. A tree is a folder: a list of entries, each with a mode, a type, a hash and a name. A commit points to the top tree. It also records who made it, when, and why.

ObjectWhat it isKnows the file name?
BlobOne file's contentsNo
TreeOne folderYes, it stores the names
CommitOne saved momentPoints to a tree and a parent

Let's see them work together.

What does one commit look like?

Say a project has README.md and a src folder with app.js. One commit builds a small family tree.

The commit also stores its parent, the commit that came right before it. That link is how Git knows the order of your history.

Now, what happens if you edit only the README?

So what's new when I edit one file?

Two files with the same contents get the same hash, so they are the same blob. Edit only README.md and the next commit makes a new blob, a new root tree, and a new commit. Everything else is reused.

Click Next to follow the second commit. Try to guess which objects are created before you click.

New commit
Root tree
README blob
src tree
app.js blob

README.md changes, so its content has a new hash

Step 1 of 5
Three new objects (blob, root tree, commit). The src tree and app.js blob keep their old hashes and are pointed at again.

Only the objects on the path to the changed file get replaced.

Commit 1

README blob A, src tree X, root tree R1

Commit 2 (README edited)

README blob B (new), src tree X (reused), root tree R2 (new)

So on paper it is a full snapshot, but it only costs what actually changed. Want to see it for real?

Can I look at this myself?

Yes. The book shows a few plumbing commands. These are the low-level commands that add and commit use behind the scenes.

  1. echo 'test content' | git hash-object -w --stdin saves content and prints its key. In the book it prints d670460b4b4aece5915caf5c68d12f560a9fe3e4.
  2. git cat-file -p <hash> prints what is inside an object, and git cat-file -t <hash> tells you its type.
  3. git write-tree turns the staging area into a tree object.
  4. git commit-tree <tree> -p <parent> makes a commit from a tree.

The book's point is that git add and git commit do these steps for you: store blobs, update the index, write trees, write commits. The index is the staging area, the list of what goes in the next commit.

Where do the files actually sit?

On disk, each object is one file inside .git/objects. The first 2 characters of the hash become a folder name, and the other 38 become the file name.

Per the book, Git builds a header (the type, a space, the size, then a null byte, so blob 16 for 16 bytes). It hashes the header plus the content, compresses it with zlib, and writes it there. Blob content can be almost anything, but trees and commits follow a very strict format.

One last piece: where does a branch fit in?

So what is a branch then?

A branch is a tiny file in .git/refs/heads that holds one commit's 40-character hash. That is all.

Ref

A human-friendly name (like main) that points to a commit's hash. A branch is the most common kind.

HEAD usually names the branch you are on, for example ref: refs/heads/master. When you commit, the branch file gets the new hash. That is why making a branch is instant: it is one small file, not a copy of your project.

Key takeaways
  • Git stores snapshots of your whole project, not lists of changes.
  • Everything is an object named by a 40-character SHA-1 hash of its content: blobs (file contents), trees (folders) and commits.
  • Unchanged files are reused by pointing at the same blob, so snapshots stay cheap.
  • A branch is just a small file holding a commit's hash, which is why branching is instant.

Sources

  • Pro Git, Git Internals: Git Objects
  • Pro Git, Git Internals: Git References
  • Pro Git, Getting Started: What is Git?
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Foundations · 3 minWhy is 0.1 + 0.2 not 0.3 in code?Foundations · 5 minHow do maps apps find the fastest route?Foundations · 4 minWhy does one emoji have a length of 5, 7 or 17?
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does your computer know the time? (And why it can be wrong)03What is a Bloom filter? A tiny "definitely not" checker