Lesson 02 · foundation
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.
When you commit, Git stores one commit object. Per the Pro Git book, it contains:
| Part | What it is |
|---|---|
| snapshot | A 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. |
| metadata | Author name + email, and the message you typed. |
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 →
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.
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.
main labels the newest commit; HEAD says "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":
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.
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.
| Flag | Does |
|---|---|
--oneline | One compact line per commit (short SHA + message). |
--graph | Draws the parent arrows as ASCII lines on the left. |
--all | Show every branch, not just the current one. |
--decorate | Print the branch / HEAD labels (on by default in modern Git). |
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.
| CLI | VS Code |
|---|---|
git log --graph --all | The 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 |
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.
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.
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.