Git does not save your edits. It saves snapshots made of blobs, trees and commits, each named by a hash. Here is the simple version.
Many people learn Git as a list of commands to copy and paste: add, commit, push. But under those commands sits a small, simple storage system. Once you see it, Git stops feeling like magic.
The big surprise: Git does not store your changes. It stores snapshots, a picture of all your files at the moment you commit. The Pro Git book states Git thinks of its data as a "stream of snapshots".
Good question. Say you commit 500 files and change one. Does Git store all 500 again? No. Git's own docs say that if a file has not changed, Git "doesn't store the file again, just a link to the previous identical file".
How does Git know a file is "identical"? That is the next trick.
In a key-value box. The book calls Git "a content-addressable filesystem". That means you put in some content, and Git gives you back a key to find it later. The key is made from the content itself.
That key is a hash. A hash is a fixed-length code worked out from the content, like a fingerprint. Git uses a SHA-1 hash: 40 characters made of 0-9 and a-f.
The same content always gives the same key. Change one letter and the key is completely different. Git also notes that you cannot change a file without Git knowing.
So the box holds things named by fingerprints. What kinds of things?
Git's box holds a few kinds of objects. Three matter most.
A blob is the raw contents of one file. It has the contents only, not the file name. A tree is a folder: a list of entries, each with a mode, a type, a hash and a name. A commit points to the top tree. It also records who made it, when, and why.
| Object | What it is | Knows the file name? |
|---|---|---|
| Blob | One file's contents | No |
| Tree | One folder | Yes, it stores the names |
| Commit | One saved moment | Points to a tree and a parent |
Let's see them work together.
Say a project has README.md and a src folder with app.js. One commit builds a small family tree.
The commit also stores its parent, the commit that came right before it. That link is how Git knows the order of your history.
Now, what happens if you edit only the README?
Two files with the same contents get the same hash, so they are the same blob. Edit only README.md and the next commit makes a new blob, a new root tree, and a new commit. Everything else is reused.
Click Next to follow the second commit. Try to guess which objects are created before you click.
README.md changes, so its content has a new hash
Only the objects on the path to the changed file get replaced.
README blob A, src tree X, root tree R1
README blob B (new), src tree X (reused), root tree R2 (new)
So on paper it is a full snapshot, but it only costs what actually changed. Want to see it for real?
Yes. The book shows a few plumbing commands. These are the low-level commands that add and commit use behind the scenes.
echo 'test content' | git hash-object -w --stdin saves content and prints its key. In the book it prints d670460b4b4aece5915caf5c68d12f560a9fe3e4.git cat-file -p <hash> prints what is inside an object, and git cat-file -t <hash> tells you its type.git write-tree turns the staging area into a tree object.git commit-tree <tree> -p <parent> makes a commit from a tree.The book's point is that git add and git commit do these steps for you: store blobs, update the index, write trees, write commits. The index is the staging area, the list of what goes in the next commit.
On disk, each object is one file inside .git/objects. The first 2 characters of the hash become a folder name, and the other 38 become the file name.
Per the book, Git builds a header (the type, a space, the size, then a null byte, so blob 16 for 16 bytes). It hashes the header plus the content, compresses it with zlib, and writes it there. Blob content can be almost anything, but trees and commits follow a very strict format.
One last piece: where does a branch fit in?
A branch is a tiny file in .git/refs/heads that holds one commit's 40-character hash. That is all.
A human-friendly name (like main) that points to a commit's hash. A branch is the most common kind.
HEAD usually names the branch you are on, for example ref: refs/heads/master. When you commit, the branch file gets the new hash. That is why making a branch is instant: it is one small file, not a copy of your project.