Lesson 02 · foundation

What a Commit Really Is

A snapshot with a parent. Once you see the graph, every undo command becomes predictable instead of scary.

In Lesson 01 you learned where changes live before they're committed. This lesson is about what git commit actually creates — because the shape of that thing is the whole reason branching, merging, and "undo" work the way they do. Get this and you stop memorizing recipes; you start reasoning about the graph.

A commit is three things bolted together

When you commit, Git stores one commit object. Per the Pro Git book, it contains:

PartWhat it is
snapshotA pointer to the complete state of every tracked file at that moment — not a diff.
parent(s)A pointer to the commit(s) that came directly before. This is what threads commits into history.
metadataAuthor name + email, and the message you typed.
The idea that unlocks everything Each commit points backward to its parent. A commit is a snapshot plus a pointer to the one before it. String those pointers together and you get history — a chain, running from newest back to the very first commit (which has zero parents).
Pro Git, 3.1 A commit object contains "a pointer to the snapshot of the content you staged … and pointers to the commit or commits that directly came before this commit (its parent or parents): zero parents for the initial commit, one parent for a normal commit, and multiple parents for a commit that results from a merge."
Branches in a Nutshell →

Snapshots, not diffs

This is the single biggest way Git differs from older tools (SVN, CVS) and from how most people imagine it. Git does not store "line 4 changed". Every commit stores the full tree. Unchanged files are just re-pointed to the identical stored content, so it costs nothing — but conceptually, each commit is a complete photo of the project.

Why this matters for your mission Because every commit is a full, self-contained snapshot, you can jump to any one of them and get the entire project exactly as it was. That is the mechanism behind "never lose work" — every commit is a save point you can return to whole.

A branch is just a sticky note on a commit

Here is the part that makes branches stop being scary. Pro Git's exact words:

"A branch in Git is simply a lightweight movable pointer to one of these commits."

That's the entire definition. main is not a copy of your files. It is not a folder. It is a label pointing at one commit — a sticky note stuck to the latest commit on that line of work. Creating a branch writes a new 40-byte pointer; that's why it's instant.

a1b2c3
initial
d4e5f6
add auth
7a8b9c
fix login
main HEAD
Arrows point to parents (backward). main labels the newest commit; HEAD says "you are here".

HEAD = "you are here"

One more pointer completes the picture. HEAD is a pointer to the branch you're currently on. It's Git's "you are here" marker on the map. When you run git switch main, all that changes is HEAD now points at main.

Now watch what one commit does. You're on main, you commit "add logout":

7a8b9c
fix login
e0f1a2
add logout
main HEAD
New commit's parent is the old tip. Then main slides forward to it, and HEAD rides along.

Committing does two moves, every time: (1) create a snapshot whose parent is wherever HEAD currently points, (2) advance the current branch pointer to the new commit. Nothing else. Branching, merging, resetting — all of it is moving these little pointers around a graph of immutable snapshots.

See it in your own repo

git log --oneline --graph --all --decorate

Memorize this command — it's the single most useful view in Git and you'll live in it once you're on a team. Each line is a commit; the labels in parentheses are the branch pointers and HEAD. You are literally reading the graph from the diagrams above.

FlagDoes
--onelineOne compact line per commit (short SHA + message).
--graphDraws the parent arrows as ASCII lines on the left.
--allShow every branch, not just the current one.
--decoratePrint the branch / HEAD labels (on by default in modern Git).
Watch the wording A short SHA like 7a8b9c is the commit's real name — a fingerprint of its content. Two different snapshots can never share one. This is why Git can tell instantly whether your teammate's history matches yours: same SHAs, same history.

The same thing in VS Code

CLIVS Code
git log --graph --allThe Git Graph extension, or Source Control ▸ ⋯ ▸ View History
current branch (where HEAD points)the branch name in the bottom-left status bar
git switch <branch>click that status-bar branch name ▸ pick from the list

Do this now (5 minutes, in a real repo)

git log --oneline --graph --all --decorate   # read the labels: where is HEAD? where is main?

git switch -c experiment      # new branch pointer at the SAME commit; HEAD now on experiment
git log --oneline --all       # main and experiment sit on the identical commit

echo "hi" > x.txt && git add x.txt && git commit -m "on experiment"
git log --oneline --graph --all --decorate    # experiment moved forward; main stayed put

git switch main               # HEAD re-points to main — x.txt vanishes from your folder
git log --oneline --graph --all --decorate    # same graph, HEAD label jumped back

The moment to sit with: after git switch main, x.txt disappears from disk — not deleted, just not part of the snapshot main points to. Switch back to experiment and it returns. You're watching pointers move over immutable snapshots. Nothing is ever lost; you're just changing which photo is on the table.

Your win You can now read git log --graph as an actual graph of pointers, and you know a branch is a cheap movable label. Next lesson — the undo playbook — is nothing but "move HEAD or a branch pointer to an earlier commit," which only makes sense now that you can see the pointers.

Go deeper

Primary source — read this one: Pro Git §3.1, "Branches in a Nutshell". It walks the exact same pointer diagrams with Git's own illustrations. Ten minutes and the model is airtight.

Then play: Learn Git Branching, intro sequence. It animates these pointers as you type real commands — the fastest way to make the graph intuition stick.