Pointer Walk

What git commit Writes

A file change becomes a blob, a tree, a commit and a moved branch name. A branch is a tiny text file holding one commit id.

Developer ToolsIntermediate9 min read

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.

What git commit writesInteractive

$

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.

text
100644 blob 8f3a2c...    hello.txt
100644 blob d41c9e...    todo.txt
040000 tree 5b77e0...    src

A 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.

text
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:

text
.git/refs/heads/main

Open 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.
  • HEAD is another small file, saying which branch you are currently on.

Putting it together

  1. git add

    Writes a blob for each changed file's contents.

  2. git commit: trees

    Writes a tree for each folder, mapping names to blobs and subtrees.

  3. git commit: the commit

    Writes a commit pointing at the top tree and at the previous commit.

  4. 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.

bash
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 branch

Try 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, amend and rebase build 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, merge and rebase mostly 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.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    7 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress