Commits are snapshots
The one idea everything else in git is built on.
1 Learn the idea
Most git confusion comes from memorising commands without a picture of what they do. So before any commands: what is git actually storing?
A commit has four things
- A snapshot of every tracked file, exactly as it was at that moment (not a list of changes — git works out diffs later, on demand).
- A message describing why this snapshot exists.
- A parent pointer to the previous commit. Following parents backwards is your history.
- An ID (hash) like
a1b2c3d, calculated from all of the above. Change anything — even a comma — and you get a different ID.
2 Watch it happen
Press Play or use → to step through. Watch the graph as each command runs.
Read this demo as text
$ git init
An empty repository. Git is ready to take photos, but the album is empty.
$ echo "# My Recipes" > README.md
We create a file. It now exists in the working directory (your folder), but git is not tracking it yet.
$ git add README.md
git addputs the file into the staging area — "include this in the next photo".$ git commit -m "Start recipe book"
Click! Our first commit appears. It has no parent: it is the root of history. Note its hash under the dot.
$ echo "Pancakes: flour, milk, eggs" > pancakes.md $ git add pancakes.md
A new file, staged for the next snapshot.
$ git commit -m "Add pancakes"
Second commit. The line to the left is its parent pointer — it remembers where it came from.
$ edit pancakes.md "Pancakes: flour, milk, eggs, a pinch of salt" $ git commit -am "Salt the pancakes"
commit -astages every change to already-tracked files and commits in one go. Three snapshots now, each pointing to the previous one.$ git log --oneline
git logwalks the chain backwards from where you are. Newest first. The labelsmainandHEADare pointers — that is the next lesson.
- A commit stores the changes I made
- History is a list of edits
- Rewriting history edits old commits
- A commit stores the full snapshot + a parent
- History is a chain of snapshots
- Commits are immutable; new ones get created
git initTurn the current folder into a repositorygit add <file>Put a change into the next snapshotgit commit -m "msg"Take the snapshotgit log --onelineWalk history backwards from here3 Check your understanding
What does a commit fundamentally store?
You fix a typo in the message of your last commit. What happens to its hash?
How does git know the order of your history?
4 Practice for real
Start your own history
Create a repository with 3 commits. Any files, any messages. End with a clean working tree.