Lesson 2: The Snapshot
A photographer doesn't save every photo they take — they select the best ones, then add them to the album. Git works the same way. You choose which changes to save, then take a snapshot (commit).
The Three Stages of Git
┌─────────────────┐ git add ┌─────────────────┐ git commit ┌─────────────────┐
│ │ ──────────────→ │ │ ──────────────→ │ │
│ Working Dir │ │ Staging Area │ │ Repository │
│ (Modified) │ │ (Ready) │ │ (Saved) │
│ │ ←────────────── │ │ │ │
└─────────────────┘ git restore └─────────────────┘ └─────────────────┘
git add — Selecting the Photos
The git add command moves changes from your working directory to the staging area. Think of it as saying "I want to include this in the next snapshot."
git add readme.txt # Stage a specific file
git add . # Stage ALL changes
git add *.js # Stage all JavaScript files
git commit — Taking the Snapshot
The git commit command permanently records everything in the staging area. Every commit gets a unique hash (ID) and requires a message describing what changed.
git commit -m "Add user login feature"
Anatomy of a Good Commit Message:
- ✅
"Fix login redirect on mobile browsers"— Specific, actionable. - ❌
"Fixed stuff"— Vague, useless. - ❌
"asdf"— Please don't.
The Lifecycle of a File
| Status | Meaning | |--------|---------| | Untracked | Git doesn't know about this file yet | | Staged | Marked for the next commit | | Committed | Safely saved in the repository | | Modified | Changed since the last commit |
Mission Objective
Take your first snapshot:
- Create evidence: Run
echo 'Hello, Git!' > readme.txtto create a file. - Select it: Stage the file with
git add readme.txt. - Save forever: Create your first commit with
git commit -m 'Initial commit'.
Pro Tip
Use git status between every step to see how files move through the stages. It's your best friend while learning Git!