Snapshots and hashes
Overview
You can use Git for years without knowing how it stores data. But a few minutes on the internals pays off enormously: branches, merges, rebase and recovery all become obvious once you know that Git is, at heart, a simple content-addressed store of snapshots.
Snapshots, not differences
Many older version control systems store a file’s first version plus a list of changes (“deltas”) for each later version. Git takes a different approach: each commit records the state of every file — a complete snapshot of the project.
That sounds wasteful, but it isn’t:
If a file didn’t change, the new snapshot simply points to the copy already stored. Only changed files take new space. A thousand commits that each touch one file store about a thousand file versions, not a thousand copies of the whole project.
Snapshots make most operations fast and simple: to switch to an old commit, Git just reads that snapshot — it doesn’t have to replay a chain of changes. When you ask for a diff, Git computes it on the fly by comparing two snapshots.
(On disk, Git does compress similar versions against each other in packfiles to save space — but that’s a storage detail. Conceptually, and in every command, a commit is a snapshot.)
Content addressing: names from hashes
How does Git know two files are the same? Every piece of data is named by a hash of its content — a 40-character hexadecimal ID computed with the SHA-1 algorithm:
echo "hello" | git hash-object --stdin
# ce013625030ba8dba906f756967f9e9ca394464aRun that on any computer in the world and you get the same ID, because the ID is calculated from the content alone (plus a small header saying it’s a file). Hence:
- Identical content is stored once. Two files with the same text in different folders, or the same file in a hundred commits, share one stored object.
- Any change gives a new ID. Change one character —
hellp— and the hash is completely different. There’s no way to edit stored content without its name changing. - IDs are global. A commit ID refers to exactly the same commit in your clone, your colleague’s clone and on GitHub.
Why the hash chain matters
A commit’s ID is a hash of the commit’s contents — and those contents include the ID of its snapshot and the ID of its parent commit. So each commit’s ID depends on its parent’s ID, which depends on its parent’s, all the way back to the first commit.
That has two big consequences:
- History is tamper-evident. If anyone changed a file, a message or an author in an old commit, that commit would get a new ID, and so would every commit after it. You can’t quietly rewrite the past — the change is visible to everyone.
- “Rewriting history” really means creating new commits. When you amend or rebase (later in this course), Git doesn’t edit commits; it makes new ones with new IDs and moves your branch to them. The old commits still exist for a while — which is why you can recover from mistakes.
Short IDs
You rarely type all 40 characters. Git accepts any unique prefix, usually 7 or more characters:
git show c91e5a0Git picks a long enough abbreviation automatically in its output. In a huge repository it may use 10–12 characters to stay unambiguous.
SHA-1 and SHA-256
SHA-1 is no longer considered secure for cryptography, and researchers have produced deliberate SHA-1 collisions. Git uses a hardened variant that detects the known attack, and has supported SHA-256 repositories since Git 2.29 (git init --object-format=sha256). Git 3.0, planned for spring 2027, makes SHA-256 the default for new repositories, with 64-character IDs.
For you, nothing changes except the length of the IDs. Everything in this course works the same either way.
Seeing it for yourself
Git has “plumbing” commands that show the raw data. You don’t need them day to day, but they make the model concrete:
git hash-object index.html # what ID would this file have?
git ls-tree HEAD # the snapshot of the latest commit
git ls-tree HEAD~1 # …and of the commit before100644 blob 3f1c2d9e… index.html
100644 blob 8ab686e1… style.cssCompare the two listings: files that didn’t change between the commits have the same blob ID. The next lesson explains blobs, trees and commits — the three kinds of object these IDs name.
Takeaway: a commit is a full snapshot, every object is named by the hash of its content, and nothing stored is ever edited in place. Hold on to that and Git will rarely surprise you.
Prove that Git stores content once
Show three things with Git’s low-level commands. (1) Two files with identical content have the same hash, and git hash-object gives the same answer on any computer. (2) After a second commit that only changes style.css, the unchanged index.html in both commits points to the same blob. (3) Changing a single character produces a completely different hash.
mkdir hashes && cd hashes && git init -b main
echo "hello" > a.txt
echo "hello" > b.txt
# 1. Hash both files
echo "<h1>Home</h1>" > index.html
echo "h1 { color: navy; }" > style.css
git add index.html style.css && git commit -m "First version"
echo "h1 { color: teal; }" > style.css
git commit -am "Change heading colour"
# 2. Compare the trees of the two commits
# 3. Change one character and hash againmkdir hashes && cd hashes && git init -b main
echo "hello" > a.txt
echo "hello" > b.txt
# 1. Hash both files
git hash-object a.txt b.txt
# ce013625030ba8dba906f756967f9e9ca394464a (both — and on every computer)
echo "<h1>Home</h1>" > index.html
echo "h1 { color: navy; }" > style.css
git add index.html style.css && git commit -m "First version"
echo "h1 { color: teal; }" > style.css
git commit -am "Change heading colour"
# 2. Compare the trees of the two commits
git ls-tree HEAD~1
git ls-tree HEAD
# index.html has the same blob ID in both; style.css has a new one
# 3. Change one character and hash again
echo "hellp" | git hash-object --stdin
# a completely different 40-character IDSummary
- Each commit records a complete snapshot of the project, not a list of differences.
- Every object is named by a SHA-1 hash of its content, so identical content is stored only once.
- Unchanged files are reused between snapshots, which keeps repositories small.
- Changing anything — a file, a message, a parent — changes the hash, so history can’t be altered silently.
- Git 3.0 will switch new repositories to the stronger SHA-256 hash; the ideas stay the same.