How Git stores history

Beginner · 15 min

Git stores snapshots, not diffs: commits point to trees, trees point to blobs, and every object is named by a hash of its content. See it with git cat-file.

Before thisReading history with log and diff

Missing Semester · 70 min · from 6:16

How Git stores history

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Missing SemesterReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Git stores each commit as a snapshot of the whole project, not as a list of differences from the previous commit.
  • There are three main object types: a blob holds a file's contents, a tree holds a folder listing, and a commit points to a tree and its parent.
  • An object's ID is a hash of its content, so identical content is stored once and any change gives a different ID.
  • A commit's ID depends on its tree, its parent and its metadata, so changing anything in the past changes every ID after it.
  • git cat-file -p shows what any object contains, which lets you check what Git claims about your history.

The question this answers

Everything so far suggested that each commit stores “what changed”. git log -p even prints changes. It’s natural to picture a pile of diffs, replayed in order to reach today’s files. That picture is wrong, and fixing it explains a lot: why switching branches is instant, why a commit can’t be quietly edited, and why Git rarely loses anything.

Git stores snapshots. Each commit points at a complete picture of the project. The diffs you see are computed on demand by comparing two snapshots.

Everything is an object with a name

Inside .git/objects, Git keeps a simple database. You hand it content and it hands back a name. The name is a hash of the content: a 40-character fingerprint computed from the bytes. The same bytes always give the same name, and a one-character change gives a completely different one.

Try it. I ran this in a repository:

$ printf 'hello\n' | git hash-object --stdin
ce013625030ba8dba906f756967f9e9ca394464a

That is Git computing the name for the text hello and a newline, without storing anything. -w stores it too. The name is the SHA-1 hash of the text blob 6, a zero byte, and the content. I checked by hashing those bytes with shasum and got the same ce01362.... My Git reports sha1 as its object format; newer Git can use SHA-256 for new repositories, but SHA-1 is what you will meet almost everywhere.

Because the name comes from the content, copying a file under a new name gives it the same ID. I copied README.md to COPY.md and git hash-object COPY.md printed the same 521d5ec... as the original.

Three object types

Blob. The contents of one file. No file name, no permissions, just bytes.

$ git cat-file -t 521d5ecf38feb8a772986216d3d71e27d791551a
blob
$ git cat-file -p 521d5ecf38feb8a772986216d3d71e27d791551a
# tinynotes

A tiny command-line notes tool.
...

-t prints the type and -p pretty-prints the content.

Tree. One folder: a list of names, each pointing at a blob or another tree. This is where file names live.

$ git ls-tree HEAD
100644 blob 521d5ecf38feb8a772986216d3d71e27d791551a	README.md
100644 blob 8458c7d2dddf418cf46cda395b2640f07d0013e6	notes.py

Commit. The thing git log lists:

$ git cat-file -p HEAD
tree 1b143eed6dcb21af5c1ef152947fc160a5b1253b
parent b6062b1aaca12e8dac86a9ccb7f526d201ff785e
author Ada Example <ada@example.com> 1767866400 +0000
committer Ada Example <ada@example.com> 1767866400 +0000

Add a clear command

That is the whole commit: a pointer to one tree (the entire project at that moment), a pointer to its parent commit, who and when, and the message. The first commit has no parent line. A commit’s own ID is the hash of exactly this text.

So history is a chain. The commit points to a tree, the tree points to blobs, and the commit also points back to its parent commit.

Why unchanged files cost nothing

In tinynotes, README.md never changed after the second commit. Look at its entry in two different commits’ trees:

$ git rev-parse HEAD~1:README.md HEAD:README.md
521d5ecf38feb8a772986216d3d71e27d791551a
521d5ecf38feb8a772986216d3d71e27d791551a

Same ID, so one blob, referenced by both trees. notes.py changed, so each version has its own blob (19f69ec... earlier, 8458c7d... now). That is how snapshots stay cheap: only changed content makes new objects. A repository with 4 commits here held 13 object files: 4 commits, 4 trees, and 5 blobs, one of which was the stray hello I wrote on purpose.

Git also packs old objects together and stores similarity between versions compactly in a later optimisation, so a real repository is far smaller than “every snapshot in full” would suggest. The .git/objects layout you see here is the simple form. Packing is covered in the Inside Git group.

Why you can’t quietly edit the past

A commit’s ID is a hash of its tree ID, its parent’s ID, and its metadata. Change anything in an old commit and its ID changes. Its child names that parent ID, so the child’s text changes and its ID changes too, all the way to the tip. This is why rewriting history (amending, rebasing) produces new commits with new IDs rather than editing old ones in place. The old ones still exist until Git’s cleanup removes them. You will see this concretely in the amend and rebase lessons.

It also means an ID is a trustworthy name: if two people hold the same commit ID, their histories up to that point are identical.

What the video covers

The embed plays the start of the data-model section of MIT’s Missing Semester lecture 5, “Version Control and Git” (2026), from 6:16 to 20:55: snapshots, modelling history by relating snapshots, the data model as pseudocode, and objects and content-addressing, per the creator’s chapter list. That is about fifteen minutes of a 70-minute lecture, and it stops before the chapter on references, which the next group’s lesson on refs covers. The lecture notes describe the same material: files as blobs, directories as trees, commits pointing at a tree and parents, and objects named by a hash of their contents. I couldn’t read a transcript, and I didn’t check that the pseudocode chapter matches the git cat-file commands above. The lecture is in English, judged from the title and description. Computerphile’s look inside .git and Brief’s object walkthrough are linked as hands-on companions.

Where this fits

That finishes the first group. You know what Git is for, how to install it, the three areas, how to commit, how to read history, and how history is stored. Everyday work comes next, beginning with committing exactly the changes you mean to: staging precisely with add -p.

Check yourself

  1. 01Two commits both contain an unchanged README.md. How many copies of it does Git store?

    One. Both trees point at the same blob, because the blob's ID is a hash of its content and the content is identical.

  2. 02What are the three things a commit object holds?

    A pointer to a tree (the project snapshot), a pointer to its parent commit (none for the first), and the author, committer and message.

  3. 03Why does rewriting an old commit change the IDs of all later commits?

    Each commit's ID is a hash that includes its parent's ID. Change one commit and its ID changes, so its child's content changes, and so on down the chain.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark