How Git Works: Snapshots, Not Differences
There are two ways to remember how a room looked over the years.
The first is a list of changes: "moved the sofa to the window", "painted the wall blue", "added a lamp". To know how the room looked last March, you start from the beginning and replay every change up to March.
The second is a photo album. Every time something changes, you take a photo of the whole room. To know how it looked last March, you open the album at that page.
Most older version control systems keep the list of changes. Git keeps the photo album. This one design choice explains why Git is fast, why its history is trustworthy, and why branches cost almost nothing. In this lesson, you will see the snapshots with your own eyes.
Differences vs Snapshots
A difference (also called a delta) is a record of what changed in a file: "line 2 was added". Tools such as CVS and Subversion think about history this way. They keep a first version of each file and then a list of changes to it.
A snapshot is a record of what every file looked like at one moment. Each Git commit is a snapshot of the whole project.
"A full copy of every file in every commit" sounds wasteful. It would be, if Git copied files that didn't change. It doesn't. When a file is the same as before, the new snapshot points to the copy Git already has. Only new content is stored.
See the Snapshots Yourself
Navika starts the Fee Tracker repository with two files and makes a commit. Then she asks Git what is inside that commit's snapshot with git ls-tree HEAD. (HEAD means the commit you are on now.)
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add README.md fees.txt
git commit --quiet -m "Add README and fee list"
git ls-tree HEAD100644 blob d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7 README.md
100644 blob 2de12a7bc775374fc0d44b3171f096fd4a1fc80a fees.txtEach line describes one file in the snapshot:
| Part | Meaning |
|---|---|
100644 | The file's mode: an ordinary file |
blob | Git's word for the stored content of a file |
d5924b0d02d9... | The ID of that content |
README.md | The file's name |
Now she adds one line to fees.txt, leaves README.md alone, and commits again:
echo 'Term 2 fee: $12,500' >> fees.txt
git add fees.txt
git commit --quiet -m "Add term 2 fee"
git ls-tree HEAD100644 blob d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7 README.md
100644 blob 6da94ac60fd2e51285424309ff1b0a4186a0e29b fees.txtCompare the two outputs:
- The new snapshot lists both files, even though only
fees.txtchanged. Every commit describes the whole project. README.mdhas the same ID as before,d5924b0. Git did not store it a second time. The second snapshot points to the same stored content.fees.txthas a new ID,6da94ac, because its content is new.
Every ID Is Made from the Content
The long IDs are not counters and they are not random. Git puts the content through a mathematical function called a hash function, which turns any content into a fixed-length string of 40 characters. This string is the hash, and Git uses it as the content's name.
Two facts about hashes explain a lot of Git:
- The same content always gives the same ID. That is how Git knew
README.mdhadn't changed: its ID was the same. - Changing even one character gives a completely different ID. So if a file is damaged on disk or altered in secret, its ID no longer matches and Git notices.
Commits have IDs made the same way. Here is the full content of the newest commit:
git cat-file -p HEADtree 6a341257eb78714199e4490de57c277f547a72f6
parent df01aee7c96dd09a3525ce79b322a8b93b5e640c
author Navika Anamaneni <navika@example.com> 1767629520 -0600
committer Navika Anamaneni <navika@example.com> 1767629520 -0600
Add term 2 feeA commit is a small piece of text with five parts:
| Line | Meaning |
|---|---|
tree | The ID of this commit's snapshot (the list you saw with git ls-tree) |
parent | The ID of the commit that came before |
author | Who wrote the change, and when (the number is the time, counted in seconds) |
committer | Who saved the commit, and when |
| The last line | The commit message |
The commit's own ID is the hash of this text. Because the text includes the parent's ID, each commit is tied to all the history before it. Nobody can change an old commit without changing the ID of every commit after it. This is why a Git history can be trusted.
Why Snapshots Make Git Fast
Because each commit is a complete picture, Git can answer most questions without replaying anything.
Going back is instant. To see a file as it was one commit ago, Git reads it straight from that snapshot. HEAD~1 means "the commit before HEAD":
git show HEAD~1:fees.txtTerm 1 fee: $12,000Comparing is cheap. To find out which files differ between two commits, Git compares IDs. Equal IDs mean equal content, so it never opens files that are the same.
Branches cost almost nothing. A branch is a label that points to a commit. Creating one writes a single short line to disk, however large the project is.
Everything is local. All the snapshots are in the .git folder on your computer, so these commands don't wait for a network.
Common Mistakes
Saying "Git stores the changes between versions". It is the most common wrong answer in interviews. Git stores snapshots. When you run git diff or look at a commit's changes, Git calculates the difference at that moment by comparing two snapshots.
Worrying that snapshots will fill the disk. Unchanged files are never stored twice, and Git also compresses what it stores. A repository with years of history is often smaller than you expect.
Thinking commit IDs count upward. 94f2914 did not come "after" df01aee in any numeric sense. The order of history comes from each commit's parent line.
Expecting the same IDs as a tutorial. A commit's ID depends on its author and time, so your commits get different IDs from Navika's. The IDs of file content are different: Fee Tracker in a file gives d5924b0 on every computer in the world, because the content is identical.
Under the Hood
Git stores three kinds of objects, and you have now met all three:
| Object | What it holds | You saw it with |
|---|---|---|
| Blob | The content of one file (no name, no date) | The IDs in git ls-tree |
| Tree | A list of names and the blobs they point to: one snapshot of a folder | git ls-tree HEAD |
| Commit | A tree ID, the parent's ID, the author, the committer and the message | git cat-file -p HEAD |
A file's name is stored in the tree, not in the blob. That is why two files with the same content, even in different folders, share one blob.
So does Git never store differences? To save disk space, Git later gathers objects into packfiles, and inside a packfile it may store one version of a file as a difference from a similar one. This is a storage trick that you never see. Every command still behaves as if each commit holds a full snapshot, and that is the right way to think about Git.
Git 2.53 calculates IDs with the SHA-1 hash function by default. It can also create repositories that use the newer SHA-256, which gives 64-character IDs, and making SHA-256 the default is planned for Git 3.0.
Interview Corner
Does Git store differences or snapshots?
Snapshots. Every commit records the state of the whole project through a tree object. Files that did not change are not stored again; the new tree points to the existing blob. Differences shown by git diff or git show are calculated on demand by comparing two snapshots. (Packfiles may use delta compression on disk, but that is a storage detail and does not change the model.)
What is a commit hash, and how is it produced?
It is the commit's unique ID: a 40-character SHA-1 hash of the commit's content. That content is the ID of its tree, the IDs of its parent commits, the author and committer with their timestamps, and the message. Any change to any of these gives a different hash.
Why can't an old commit be changed without anyone noticing?
Because each commit's hash covers its parent's hash. Changing an old commit changes its hash, which changes the parent line of the next commit and therefore its hash, and so on up to the newest commit. Every later ID would be different, so the change is visible at once.
What are blobs, trees and commits?
A blob stores the content of a file. A tree stores a folder listing: names, modes, and the IDs of blobs and other trees. A commit points to one tree (the snapshot of the project) and to its parent commits, and adds the author, the committer and the message.
Two files in a repository have the same content. How many blobs does Git store?
One. A blob's ID depends only on the content, and the file name is kept in the tree. Both tree entries point to the same blob.
Practice
Challenge: predict the next snapshot. Continue the session from this lesson. Add the line Contact: office@example.com to README.md, commit it with the message Add office contact, and print the snapshot with git ls-tree HEAD.
What does Git print? Before you open the solution, decide which of the two IDs will change and which will stay the same:
README.mdisd5924b0now.fees.txtis6da94acnow.
Show solution
echo "Contact: office@example.com" >> README.md
git add README.md
git commit --quiet -m "Add office contact"
git ls-tree HEAD100644 blob 8fd2f3ff1263a312216fb60be49fbe28248bf4d4 README.md
100644 blob 6da94ac60fd2e51285424309ff1b0a4186a0e29b fees.txtREADME.md has new content, so it gets a new ID, 8fd2f3f. fees.txt did not change, so the new snapshot reuses the blob 6da94ac from the commit before.
One more check. git rev-parse prints the ID that a name stands for. This asks for the ID of fees.txt in the newest commit and in the one before it:
git rev-parse HEAD:fees.txt HEAD~1:fees.txt6da94ac60fd2e51285424309ff1b0a4186a0e29b
6da94ac60fd2e51285424309ff1b0a4186a0e29bThe same ID twice: two snapshots, one stored file.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git ls-tree HEAD | List the files in the current commit's snapshot, with their IDs |
git cat-file -p <id> | Print the content of any object, such as a commit |
git show <commit>:<file> | Print a file as it was in that commit |
git rev-parse <name> | Print the full ID that a name stands for |
HEAD~1 | A way to write "the commit before the current one" |
Was this tutorial helpful?