Skip to content

Volume ITechnology

13.11.5 — What Git Actually Stores ​

You have used Git for four pages without looking inside it. That is fine, but it leaves one thing as magic: how does Git remember all this?

Most Git confusion comes from one wrong picture. People imagine that Git stores changes: "line 5 added, line 9 removed". It does not.

Git stores complete snapshots of your files, and files are found by the fingerprint of their content. A diff is something Git works out when you ask for one. Once this is clear, branches stop being mysterious, rebase stops being frightening, and the commands from the last pages look like things you could have predicted.

You can see the whole model in about ten minutes. This page does that, using real commands on a real repository.

1. The four kinds of object ​

Open the .git folder of any repository and look in objects. Everything in there is one of four kinds of thing, and each is filed under a fingerprint of its own content. This idea is called content addressing: the address of something is computed from what it contains. Chapter 8.2.2 explains fingerprints (hashes) in general.

Let us make a tiny repository with two files and look at what Git created.

text
$ git init -q
$ echo "# Shop" > README.md
$ printf 'export function total(prices: number[]): number {\n  return prices.reduce((a, b) => a + b, 0);\n}\n' > cart.ts
$ git add . && git commit -q -m "Start the shop"
$ find .git/objects -type f
.git/objects/a1/be7c4d8c9e3183c1eacab2250c248a3d4eeb62
.git/objects/23/924a4f7e07c79d720f0538ce6a888cb2aa03d0
.git/objects/8a/98c027d028106f1074319f0cf2a6974c652e77
.git/objects/a0/0621bb3a9b990ee018f8d04c2d3140800d6ca6

Two files and one commit created four objects. Each is a file whose name is its fingerprint. The first two characters are the folder name, and the other 38 are the file name. Let us open them one at a time with git cat-file, which prints an object. -t asks for its type and -p prints its content.

Blob: the contents of a file ​

text
$ git cat-file -t 23924a4
blob
$ git cat-file -p 23924a4
export function total(prices: number[]): number {
  return prices.reduce((a, b) => a + b, 0);
}

A blob is the contents of one file and nothing else. It has no file name, no folder and no permissions. It is just the bytes.

Here is a neat consequence. If two files anywhere in your history have identical content, they are one blob, stored once. You can even prove that the id is only a fingerprint of the content:

text
$ printf '# Shop\n' | git hash-object --stdin
a00621bb3a9b990ee018f8d04c2d3140800d6ca6

git hash-object computes the fingerprint of any text. This one equals the id of our README blob. The id is the content, summarised.

Tree: a folder listing ​

text
$ git cat-file -p HEAD^{tree}
100644 blob a00621bb3a9b990ee018f8d04c2d3140800d6ca6	README.md
100644 blob 23924a4f7e07c79d720f0538ce6a888cb2aa03d0	cart.ts

HEAD^{tree} means "the tree that the latest commit points to". A tree is a folder listing. Each line says: the file mode (100644 is a normal file), what kind of object it points to, its id, and its name. A tree entry can point to a blob (a file) or to another tree (a sub-folder).

So names live in trees, and contents live in blobs. That split is why renaming a file does not copy any content.

Commit: a snapshot plus a note ​

text
$ git cat-file -p HEAD
tree a1be7c4d8c9e3183c1eacab2250c248a3d4eeb62
author Ana Ruiz <ana@example.com> 1793190323 +0000
committer Ana Ruiz <ana@example.com> 1793190323 +0000

Start the shop

A commit is a small text file. It names one tree, which is the complete snapshot of the whole project at that moment. Then it records who made it, when, and the message. This first commit has no parent line because it is the first. A normal commit has one parent line pointing at the previous commit. A merge commit has two (Chapter 13.11.2).

Tag: a named label with a message ​

The fourth kind is the annotated tag object: a name, a target (usually a commit), a tagger, a message, and optionally a signature. You use it to mark releases like v1.4.0. Chapter 13.11.7 covers tags.

How they connect ​

Now make a second commit that edits one file, and look at what changed.

text
$ echo "// edited" >> cart.ts
$ git commit -a -q -m "Edit cart"
$ git ls-tree HEAD
100644 blob a00621bb3a9b990ee018f8d04c2d3140800d6ca6	README.md
100644 blob 4c498f1038d9f1e38ddf2b73fe1a60f188128cf7	cart.ts
$ git ls-tree HEAD~1
100644 blob a00621bb3a9b990ee018f8d04c2d3140800d6ca6	README.md
100644 blob 23924a4f7e07c79d720f0538ce6a888cb2aa03d0	cart.ts

git ls-tree lists a tree. Compare the two. The README.md line is identical in both: same blob a00621. Only cart.ts has a new blob, 4c498f. Git did not copy the README for the second snapshot. It pointed at the one it already had.

commitstreesblobscommit "Start the shop"8a98c0commit "Edit cart"f31e92parenttreea1be7ctreedb9995cart.ts (old)23924aREADME.mda00621cart.ts (edited)4c498fone README blob, used by both trees: stored once
Two commits, drawn as Git stores them. Each commit points to one tree (a folder listing), and each tree points to blobs (file contents). The README did not change, so both trees point to the same blob. Only the edited file has a second blob.

The figure shows the whole structure. Each commit points to one tree, and each tree points to blobs. Two snapshots, but only three file contents stored in total. This is how "Git stores complete snapshots" and "Git is small" are both true.

Let us read off the consequences.

A commit id covers everything. The id of a commit is computed from its content: the tree id, the parent id, the author, the date and the message. The tree id in turn is computed from the ids of everything inside it. So one commit id fixes the entire history behind it, and every file in it.

Original historycommit A8a98c0commit B3f1e22commit Cc97b10After changing one byte in Bcommit A8a98c0commit B′71d0a4commit C′e20f6bunchangednew idnew id too
Each commit id is computed from the commit's contents, including its parent's id. Change anything in B and B's id changes. That changes C's content (it names its parent), so C's id changes too. Every commit after the change gets a new id, which is why rewriting history changes everything above it.

Change one byte in one file of an old commit. The blob id changes. So the tree id changes. So the commit id changes. And since the next commit names this one as its parent, its content changes, so its id changes too, and so on all the way up. This chain structure is called a Merkle tree or hash chain (Chapter 8.2.2). It means Git history is tamper-evident: you cannot alter the past without every id after it changing, and everyone would notice.

Renaming a file creates no new blob. The blob is unchanged. Only a tree changes, listing the same blob under a new name. Git never recorded "this file was renamed". It works that out later by noticing that a deleted file and an added file have nearly the same content. That is why git log --follow is a guess rather than a lookup.

Nothing is ever modified. Objects never change. "Changing history" always means writing new objects and moving a label to them. You saw this with --amend in Chapter 13.11.4.

A note on the fingerprint function. Git has used SHA-1, which researchers have shown can be forced to collide (Chapter 8.2.2). Git added a check that rejects the known attack, and it supports repositories that use SHA-256 instead. In practice the risk was always small, because an attacker would need to get write access to your repository and also craft two colliding files. The change to SHA-256 is under way.

2. Refs: a branch is a file ​

You know that a branch is "a label on a commit". Now look at the label itself.

text
$ cat .git/refs/heads/main
8a98c027d028106f1074319f0cf2a6974c652e77
$ wc -c .git/refs/heads/main
41 .git/refs/heads/main

A ref (short for reference) is a text file that holds an id. This one holds the id of our latest commit. A branch is a 41-byte file: 40 characters of id and a new line. That is the entire mechanism.

This is why creating a branch is instant, why deleting one removes only a pointer and none of the commits, and why having two hundred costs nothing.

The folders under .git/refs hold the three kinds of label:

  • refs/heads/ holds your local branches.
  • refs/tags/ holds tags.
  • refs/remotes/origin/ holds your bookmarks of the remote (Chapter 13.11.3).

HEAD says where you are. It is also a file.

text
$ cat .git/HEAD
ref: refs/heads/main
Normal: you are on a branch.git/HEADref: refs/heads/main.git/refs/heads/main8a98c027d028…commit object8a98c027d028…Detached: HEAD holds a commit id directly.git/HEAD8a98c027d028…commit object8a98c027d028…no branch in between
Three small steps from HEAD to a commit. Both HEAD and a branch are plain text files, which is why moving them is instant. In a detached state the first step is skipped.

Follow the chain in the top row: HEAD names a branch, the branch file holds an id, and the id names a commit. A detached HEAD is when the HEAD file holds an id directly instead of naming a branch (the bottom row). You are standing on a commit, not a branch. New commits then have nothing pointing at them, and moving away loses them from view (rescue with a branch, as in Chapter 13.11.2).

Now you can describe what a commit does in one short paragraph, using only things you have seen:

  1. Git writes a blob for every file whose content changed.
  2. It writes a tree for each folder that changed, listing the blobs.
  3. It writes a commit object that names the top tree and the previous commit.
  4. It writes the new commit's id into the branch file that HEAD points to.

That last step is the whole "branch moves forward when you commit" idea. Every branch operation is that last step on its own.

3. The index: the staging area, opened ​

In Chapter 13.11.1 you met the staging area as a waiting room. Here is what it physically is.

The index is one binary file, .git/index. It lists every tracked path together with the blob id and the file's size and timestamps. It is a proposed next tree: exactly the snapshot that git commit will write.

When you run git add cart.ts, Git writes the file's content as a blob and updates the index entry to point at it. When you run git commit, Git turns the index into a tree, wraps it in a commit, and moves the branch.

So there are three versions of your project at any moment:

VersionWhereWhat writes to it
Working directoryYour actual filesYour editor, and git switch
Index.git/indexgit add
HEADThe latest commitgit commit

Now here is every common command as an arrow between those three places.

Forward arrows save your work. Backward arrows get something back.Working directoryyour filesStaging areathe next commitRepositorythe last commit(HEAD)addcommitrestorerestore--stagedswitch · restore --source · reset --hardthese copy from the last commit all the way into your files
Almost every Git command is one of these arrows: it copies content from one of the three places to another. Forward arrows save work. Backward arrows restore it.

git status compares all three, which is why it has two sections. "Changes to be committed" is index versus HEAD. "Changes not staged" is working directory versus index.

This also explains reset's three modes exactly, without memorising them. --soft moves only the branch. --mixed also resets the index. --hard also overwrites the working directory. Only --hard can destroy uncommitted work, and it is the one Git command that routinely does (Chapter 13.11.4).

What the index buys you is the ability to commit part of your changes. git add -p walks through your edits hunk by hunk, so one working session becomes two clear commits. A readable history is made on purpose, and the index is the tool for it.

4. Storage: loose objects and packfiles ​

When you commit, Git writes each new object as its own small compressed file. That is a loose object, the files you saw under .git/objects. Each holds the full content of one blob, tree or commit.

text
$ git count-objects -v
count: 7
size: 28
in-pack: 0
packs: 0

count: 7 loose objects (we made two commits: 2 commits, 2 trees, and 3 blobs), and in-pack: 0 means nothing is packed yet.

Writing a full copy of every version would waste space on a large history. So Git runs git gc ("garbage collect") from time to time. It gathers many objects into one packfile and applies delta compression: when two objects are similar, it stores one in full and the other as "the first one, with these differences".

This reconciles the two things you heard. The model is snapshots (every commit names a full tree). The storage uses deltas as an optimisation. The deltas are between similar objects, not necessarily between neighbouring versions. Git chooses whatever compresses best.

Practical consequences:

A repository gets faster and smaller after git gc, which also runs automatically now and then.

Binary files do not compress well as deltas. A 10 MB image edited fifty times costs about 500 MB in history, forever. This is what Git LFS (Large File Storage) solves: Git stores a small pointer file, and the big file lives on a separate server.

Deleting a large file does not shrink the repository. It is still in the old commits, and every clone downloads it. Removing it means rewriting history with git filter-repo, which changes every later commit id and forces everyone to clone again. So the rule is: never commit a large binary or a secret. For a secret the cleanup is also incomplete, because the key must be treated as stolen (Chapter 8.6.1, and Chapter 13.11.4).

git clone --depth 1 downloads only the latest commit, not the history. That is the right default on a build server, where a full history would be downloaded and never read.

5. The reflog, in one paragraph ​

Every time a label moves, Git appends a line to a log under .git/logs/: the old id, the new id and why. That log is the reflog. It is how a "lost" commit after a bad reset can be found again. It lives on your laptop only, entries expire after about 90 days, and it never sees uncommitted work. Chapter 13.11.4 shows how to use it, step by step.

6. Settings worth having ​

Git reads settings from three places, and each overrides the one before: the system (for the whole computer), global (~/.gitconfig, for you), and repository (.git/config, for one project).

ini
[core]
    autocrlf = input          # (1)
[pull]
    rebase = true             # (2)
[rebase]
    autosquash = true
[merge]
    conflictstyle = zdiff3    # (3)
[fetch]
    prune = true              # (4)
[rerere]
    enabled = true            # (5)
[init]
    defaultBranch = main
  1. Line endings. Windows ends a line with two characters, and macOS and Linux with one. autocrlf = true (Windows) converts on checkout and back on commit. input (macOS and Linux) converts on commit only. When a team has mixed settings you get diffs where every line changed. The cure is a .gitattributes file in the repository, which everyone obeys regardless of their local settings.
  2. On git pull, replay your commits on top instead of creating a merge commit (Chapter 13.11.6).
  3. zdiff3 shows the original text alongside both sides of a conflict. That often makes the right answer obvious. It is a one-line change and a real improvement over the default.
  4. Delete bookmarks of remote branches that no longer exist, so git branch -r stays accurate.
  5. rerere means "reuse recorded resolution". Git remembers how you resolved a conflict and replays the same answer when that conflict appears again. On a long branch that you rebase repeatedly, this saves solving the same conflict five times.

.gitignore holds patterns for untracked files. As Chapter 13.11.1 showed, it does not affect files already tracked: that needs git rm --cached. Put editor and operating-system noise in a personal list (core.excludesFile) rather than the project's file.

7. Hooks ​

A hook is a script that Git runs automatically at a certain moment. The scripts live in .git/hooks/.

Client-side hooks run on your laptop: pre-commit (run a linter, a formatter or a secret scanner), commit-msg (enforce a message style), pre-push (run a quick check).

Server-side hooks run on the remote when you push: pre-receive and update. They are the only ones that cannot be bypassed, because client hooks are just files on your laptop that anyone can delete or skip with --no-verify.

So the rule is: client hooks are a convenience, and server-side checks are enforcement. A secret scanner in pre-commit is a helpful nudge. The real control is push protection on the server (Chapter 8.6.1).

Hooks are not committed, because .git/ is not tracked. Tools like Husky or pre-commit keep the hook definitions inside the repository and install them for you, which is how a team shares them.

8. A ten-minute tour you can repeat ​

Everything above is a handful of commands. Run them on any repository you own and the model becomes concrete.

bash
git cat-file -t <id>              # what kind of object is this
git cat-file -p <id>              # print it
git rev-parse HEAD                # turn a name into a full id
git rev-parse --short HEAD        # the short form
git ls-tree -r HEAD               # every file in the current commit, with blob ids
git count-objects -vH             # how big is the repository
git log --oneline --graph --all   # the shape of the history
cat .git/HEAD                     # where am I
cat .git/refs/heads/main          # where does main point

Resolve HEAD to a commit, print it, print its tree, print a blob. After that, every later command is predictable instead of memorised.

What the interviewer will push on ​

These questions check whether you understand Git or only use it.

"Does Git store changes or snapshots?" Snapshots. Each commit points to a complete tree of the project. The diff is computed on demand. Add the efficiency point to show depth: unchanged files are shared between commits as the same blob, and packfiles use delta compression underneath as an optimisation.

"What is in a commit object?" A pointer to one tree, zero or more parent ids (one normally, two for a merge), the author, the committer, the dates and the message. The id is a hash of all that. The tell of understanding is mentioning that the parent id is part of what is hashed, so the id fixes the whole history.

"What is a branch, technically?" A file under .git/refs/heads/ holding one commit id, 41 bytes. That is why creating one is instant. A strong follow-up is HEAD: a file naming the current branch, or holding an id in a detached state.

"Why does rewriting history change every commit id after it?" Because each id includes the parent's id. Change one commit and all the later ones have different content, so different ids. This is also why a force-push disturbs everyone.

"Why can a deleted large file still make a repository huge?" Because it is still in the old commits, and every clone downloads all history. Fixing it needs git filter-repo and a re-clone. The wrong answer is "delete it and commit".

"What is the staging area, and what is it physically?" A proposed next tree, stored in the binary file .git/index. It lets you build a commit from part of your changes.

One thing to volunteer: mention that you can open any object with git cat-file -p, and that you once did it to learn what Git stores. It sounds small, and it shows you treat Git as a system you understand rather than a tool you fear.

Recall

  • Git stores snapshots, not changes. Four kinds of object, each filed under the fingerprint of its content (content addressing): blob (file contents), tree (folder listing with names), commit (one tree, parents, author, message) and tag.
  • A commit id covers everything: it includes the tree id and the parent id, so it fixes the whole history behind it. Change one commit and every later id changes. That is a Merkle tree, and it makes history tamper-evident.
  • Unchanged files are shared between commits as the same blob. Renaming creates no new blob, so --follow is a guess. Objects never change: changing history means writing new objects and moving a label.
  • A branch is a 41-byte file holding a commit id. HEAD is a file naming a branch, or holding an id directly in a detached HEAD. A commit writes blobs, trees and a commit object, then updates the branch file.
  • The index is .git/index: a proposed next tree. Working directory → git add → index → git commit → HEAD. reset --soft, --mixed and --hard move one, two or three of these, and only --hard loses work.
  • Storage packs objects with delta compression as an optimisation. Binaries do not delta well, deleting a large file does not shrink history, and removal rewrites every later id. Never commit large binaries or secrets.
  • The reflog records every label move: local, expires in about 90 days, and blind to uncommitted work.
  • Configure merge.conflictstyle = zdiff3, fetch.prune and rerere.enabled, and put line-ending rules in .gitattributes. Client hooks are convenience; only server-side hooks enforce.

Self-test: What does a commit object contain? · Why do two commits share a blob? · Why is a branch instant to create? · What are the three places a file can be, and which command moves a change between each? · Why does deleting a big file not shrink the repository? · Why can a pre-commit hook not be a security control?

Next: 13.11.6 uses this model on the two operations people fear most: merge versus rebase, what happens to those commit ids, and when each is the right tool.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Volume I — Technology
Part 0 — Retention & Notation
Part 1 — Foundations of Computation
Part 2 — Operating Systems
2.1 The Kernel
2.2 Processes
2.3 Threads & Scheduling
2.4 Concurrency Under the Hood
2.5 Memory Management
2.6 File Systems
2.7 Input/Output
2.8 Linux for Engineers
2.9 Windows & macOS in Practice
Part 3 — Languages, Compilers & Runtimes
3.6 JavaScript & V8
3.7 TypeScript
3.8 Node.js Internals
3.13 Python & CPython
3.14 FastAPI & Production Python
Part 4 — Data Structures & Algorithms
4.0 Classic Problems — Before NeetCode
4.4 Arrays & Hashing — Problems
4.5 Two Pointers
4.6 Sliding Window
4.8 Stack — Problems
4.9 Linked List — Problems
4.11 Binary Search — Problems
4.13 Trees
4.14 Tree — Problems
4.15 Trie — Problems
4.17 Heap — Problems
4.18 Backtracking — Problems
4.19 Graphs
4.20 Graph — Problems
4.21 Advanced Graph — Problems
4.23 1-D DP — Problems
4.24 2-D DP — Problems
4.26 Greedy — Problems
4.27 Interval — Problems
4.28 Math & Geometry — Problems
4.30 Bit Manipulation — Problems
Part 5 — Networking & the Web
5.3 IP
5.4 Transport
5.6 HTTP
Part 6 — Frontend Engineering
6.1 Browser Internals
6.2 HTML & CSS Mastery
6.3 JavaScript in the Browser
6.4 React, from First Component to Full Shop
6.5 Frameworks & Rendering
6.8 UI Engineering & Patterns
Part 7 — Databases & Storage
7.2 SQL Mastery
7.3 Storage Engine Internals
7.4 Transactions
7.5 NoSQL Taxonomy
7.8 Analytics & Big Data
Part 8 — Security, Identity & Cryptography
8.2 Cryptography
8.3 Protocols of Trust
8.4 Identity: AuthN & AuthZ
8.5 Application Security
8.6 Infrastructure Security
Part 9 — Software Design (LLD)
9.2 OOP Foundations
9.3 Design Principles
9.4 Design Patterns
9.5 Concurrency
9.6 API Design
9.7 LLD Interview Machines
9.9 Node/Express Production Playbook
Part 10 — Distributed Systems & HLD
10.7 Consistency & Consensus
10.8 Messaging & Microservices
10.14 Caching
Building blocks from the design seat
Part 11 — System Design Case Studies
Part 12 — AI & ML Engineering
12.1 What AI Engineering Is
12.2 The Math You Need
12.3 Classical Machine Learning
12.4 Data for ML
12.5 Deep Learning
12.6 Embeddings & Vector Search
12.7 Transformers & LLMs
12.8 Building with LLMs
12.9 Retrieval-Augmented Generation
12.10 Agents
12.11 Adapting Models
12.12 Production AI
12.13 Multimodal AI
12.14 AI Security & Governance
12.15 Case Studies & Capstone
Part 13 — Cloud, DevOps & Infrastructure
13.11 Git, from First Commit to Team Workflow
Part 14 — Containers: Docker & Kubernetes
14.1 Foundations
14.2 Docker
14.3 Kubernetes
14.4 Kubernetes on Azure
Part 15 — The Craft & Career
Nothing picked yet