What is actually in the .git folder?
You type git commit many times a day. Something gets saved, a long code appears, and the branch moves forward. What did Git write to disk?
The answer is small enough to hold in your head, and once you have it, commands like reset, rebase and checkout stop being magic. A commit involves four things.
$
Branch
commits
trees
blobs
Start. A new project with one file, hello.txt, containing the word "hi". Git's object store is empty.
- Blobs
- 0
- Trees
- 0
- Commits
- 0
Everything is named by its content
First, the idea underneath it all. Git stores objects, and the name of every object is the hash of its contents: a 40-character code computed from the bytes inside.
Two consequences follow, and they explain most of Git's behaviour.
- Identical content has an identical name. Store the same file twice and you get one object.
- An object can never change. Change one byte and the hash is different, which makes it a different object with a different name.
There are three kinds of object.
Thing 1: a blob holds a file's contents
When you run git add, Git takes the contents of the file, compresses them and stores them as a blob.
A blob contains the bytes of the file and nothing else. Not its name. Not its folder. Not the date. Just the contents.
That is why renaming a file costs almost nothing: the contents have not changed, so the blob is the same one.
Thing 2: a tree is a folder listing
Names have to live somewhere. A tree is a list of entries, each saying: this name refers to that object.
100644 blob 8f3a2c... hello.txt
100644 blob d41c9e... todo.txt
040000 tree 5b77e0... srcA tree can point at blobs (files) and at other trees (subfolders). The tree for the top folder therefore describes the entire project at one moment: every file's name and exact contents. It is a complete snapshot.
Thing 3: a commit points at a snapshot
A commit is a tiny text object.
tree 5b77e0...
parent a1c94f...
author Ada <ada@example.com> 1700000000
message Add todo list- tree: the snapshot this commit records.
- parent: the commit that came before. This link is what turns separate snapshots into a history.
- author, date, message.
Notice what a commit is not. It is not a list of changes. Git stores full snapshots. When it shows you a "diff", it is computing the difference between two snapshots on the spot.
Example
Storing a full snapshot every time sounds wasteful
It is not, because of the rule about content. If a commit changes one file out of a thousand, 999 blobs have the same contents as before and therefore the same names. The new tree simply points at the old ones.
In the animation, the second commit's tree points at the same hello.txt blob as the first. Only the genuinely new content is written.
Thing 4: a branch is a text file with one line
Here is the surprise. After writing the blob, tree and commit, Git updates one more file:
.git/refs/heads/mainOpen it and you will find a single line: the id of a commit. That is the whole branch.
A branch is not a copy of your files, and it is not a container that commits live in. It is a name that points at one commit. Everything else is found by following parent links backwards from there.
This explains a lot.
- Creating a branch is instant. Git writes one small file.
- Committing moves the branch. The file is rewritten with the new commit's id.
- Deleting a branch deletes a pointer, not the commits.
HEADis another small file, saying which branch you are currently on.
Putting it together
git add
Writes a blob for each changed file's contents.
git commit: trees
Writes a tree for each folder, mapping names to blobs and subtrees.
git commit: the commit
Writes a commit pointing at the top tree and at the previous commit.
git commit: the branch
Overwrites the branch file with the new commit's id.
You can look for yourself
Git has commands for inspecting its own storage.
git cat-file -t a1c94f # what kind of object is this? "commit"
git cat-file -p a1c94f # print it: tree, parent, author, message
git cat-file -p 5b77e0 # print a tree: the list of names and ids
cat .git/refs/heads/main # the one line that is your branchTry them in any repository. Nothing is hidden.
Why this picture is worth having
- Commits are safe. A commit's id depends on its tree, which depends on every blob, and on its parent. Change anything in history and every id after it changes. Git notices corruption and tampering automatically.
- "Rewriting history" really makes new commits. Since objects cannot change,
amendandrebasebuild new commits and move the branch to them. The old ones stay for a while, which is why they can be recovered. See Undo Anything in Git. - Almost every command is "move a pointer".
checkout,reset,mergeandrebasemostly create a commit or two and then change which commit a name points at.
Key takeaways
- Git stores objects named by the hash of their contents, so objects never change.
- A blob is a file's contents, with no name.
- A tree is a folder listing: names pointing at blobs and other trees. The top tree is a full snapshot.
- A commit points at one tree and at its parent commit, with an author and message.
- A branch is a one-line text file holding a commit id.