Part II: Chapter 2.3

Commits (The Glue)

The biggest misconception you will ever learn: Commits are not diffs. They do not store what "changed." A commit is just a small text file.

What is actually inside?

If you think about it, we already have all our data stored in Blobs, and all our directory structure stored in Trees. A commit acts as the glue. It literally just stores your Author Name, a Timestamp, a Commit Message, and then a pointer to the root Tree.

Commit Details (What you edit)

Raw Commit File

tree b2xb357851f9eafaa9430c5e7b2f6efb357851
parent a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
author Linus Torvalds <linus@linux.org> 1777819521 -0700
committer Linus Torvalds <linus@linux.org> 1777819521 -0700

Initial commit of the Linux kernel.
Generated SHA-1 Hash

The Avalanche Effect

Have you ever used git commit --amend to fix a typo in your commit message? Look at the simulation above.

Try adding a single dot `.` to the end of the commit message. Notice how the giant green hash at the bottom changes completely? Because Git calculates the hash based on the raw text file, altering anything—even changing the timestamp by one second—forces exactly half of the cryptographic bits to flip.

You can't edit commits

Because altering the timestamp or message completely changes the resulting SHA-1 hash, editing commits is impossible. When you use `--amend`, Git does not edit your commit. It creates a brand new one with a brand new hash, and abandons the old one to the garbage collector.

Wait, how is the timeline built?

Look at the second line of the raw commit file: parent a1b2c3.... This is how Git builds branching history! Every commit simply points a hash backward to the commit that came before it. This creates a Directed Acyclic Graph (DAG).

Terminal
$git cat-file -p HEAD
tree b2xb357851f9eafaa9430c5e7b2f6efb357851
parent a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
author Linus Torvalds <linus@linux.org> 1777265969 -0700
committer Linus Torvalds <linus@linux.org> 1777265969 -0700
Initial commit of the Linux kernel.