Why does git store snapshots, not changes?
Content addressing, hashes, cheap commits and 41-byte branches.
Imagine history as a chain of edits. To open a version from six months ago, you'd take the very first version and "replay" thousands of edits on top of it, in order. And if one edit in the middle got corrupted, nothing after it could be recovered either. A snapshot stands on its own: open a commit and you get all the files, whole, right away.
But then .git should balloon, right? No — thanks to content addressing. For each file git computes a hash: a short fingerprint of the content (classic git uses SHA-1: 40 hexadecimal characters). Same content, same hash. The file is stored in .git under a name equal to its hash. If a file didn't change in a new commit, it has the same hash — and git simply points to the copy it already has.
function hash(text) {
let h = 0;
for (const ch of text) h = (h * 31 + ch.charCodeAt(0)) % 16777216;
return h.toString(16).padStart(6, "0");
}
const store = {};
function save(text) {
const id = hash(text);
store[id] = text; // same text, same key: no second copy
return id;
}
const v1 = { "cart.js": save("discount 10%"), "README.md": save("Byte's Shop") };
const v2 = { "cart.js": save("discount 15%"), "README.md": save("Byte's Shop") };
console.log(v1, v2);
console.log(v1["README.md"] === v2["README.md"]);
console.log(Object.keys(store).length);The same trick works one floor up. A snapshot of a folder (git calls it a tree) is a list of "file name → hash". A commit is the tree's hash, the parent's hash, the author, the date and the message. And the commit has a hash too — of all that together. That gives three superpowers:
- commits are cheap: new objects appear only for changed files;
- history can't be quietly swapped: change an old commit and its hash changes, and so do the hashes of all its descendants, because each one records its parent's hash;
- comparing is instant: equal hashes mean equal content, no need to even open the files.
And a branch? A branch is a file with one hash in it (.git/refs/heads/main): 40 characters and a newline. Creating a branch means writing 41 bytes. That's why people create git branches for every little thing. In older systems like SVN a branch was a copy of the whole project folder, and people created them reluctantly.
const commits = {
a1: { parent: null, message: "Shop skeleton" },
b2: { parent: "a1", message: "Cart" },
c3: { parent: "b2", message: "Discounts" },
};
const branches = { main: "c3" };
branches["new-cart"] = branches.main; // git branch new-cart
console.log(branches);
const messages = [];
for (let id = branches["new-cart"]; id; id = commits[id].parent) {
messages.push(commits[id].message);
}
console.log(messages.join(" ← "));What about git diff with its pluses and minuses? Git computes it on the fly by comparing two snapshots. Snapshots are primary; changes are derived. git merge rests on the same idea: git finds the common ancestor of two branches (the merge base) and compares both versions with it. If only one side changed a line, take that side's version. If both changed it, differently, that's a conflict.
Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.