Part III: Chapter 3.4

git commit

The Snapshot

The git commit command is often thought of as the command that "saves your files". But as we learned with git add, your files were already saved as Blobs! So what does git commit actually do? It just runs three simple plumbing commands to wrap everything up.

The 3-Step Process

Click the commit button below to watch the exact sequence of events that occurs when you finalize a commit.

1. Index

Blob HashFilename
5a1b3index.js
8f9a1style.css

2. Objects Database

Waiting for tree...
Waiting for commit...

3. References

HEAD / branch
.git/refs/heads/main
a1b2c

Step 1: Write the Tree

Git looks at the Staging Area (.git/index). It takes all the Blobs listed there, bundles them together with their filenames, and hashes them into a single Tree Object. This Tree represents the exact directory structure of your project at this moment.

Step 2: Write the Commit

Git creates a Commit Object. This object is just a small text file containing your name, email, the commit message, the hash of the parent commit, and a pointer to the Tree Object created in Step 1.

Step 3: Update the Reference

Finally, Git looks at where your HEAD is currently pointing (e.g., .git/refs/heads/main) and simply replaces the old commit hash in that text file with the new commit hash it just generated.

Proving the Architecture

We can completely bypass git commit and build a commit ourselves using the exact low-level plumbing commands Git uses under the hood.

Terminal
$# Step 1: Write the index into a Tree object
$git write-tree
t9x2a5...
$# Step 2: Create a Commit object pointing to that Tree and a Parent
$git commit-tree t9x2a5 -p a1b2c -m "Update"
c4d5e8...
$# Step 3: Update the main branch pointer to this new Commit
$git update-ref refs/heads/main c4d5e8