The Staging Area & git diff
The staging area is the feature that makes Git commits so clean. By the end of this lesson you'll know how to choose exactly which changes go into each commit, read git status at a glance, and use git diff to see precisely what you're about to commit.
Learn The Staging Area & git diff in our free Git course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Git course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
1️⃣ git status — Your Dashboard
git status tells you, at any moment, which changes are staged (ready to commit), which are unstaged , and which files are untracked . Run it constantly — it's the cheapest, safest command in Git. The short form git status -s uses two columns of one-letter codes: column 1 is the staged state, column 2 is the working-tree state.
2️⃣ Staging with git add
The staging area is a waiting room . git add copies the current state of a change into it, and your next commit snapshots exactly what's waiting there. In git status -s , a staged change shows its letter in the first column (e.g. M app.js ), while an unstaged change shows it in the second ( M styles.css ).
3️⃣ git diff vs git diff --staged
These two diffs answer two different questions. git diff compares your working directory to the index — "what have I changed but not staged yet?" git diff --staged (also written --cached ) compares the index to your last commit — "what will my next commit actually contain?" A handy consequence: once you git add a file, plain git diff shows nothing for it, because the change has moved into the index.
Your turn. Make a change, look at it unstaged, stage it, then confirm what your next commit will contain. Fill in the three blanks.
4️⃣ git add -p — Stage Part of a File
Sometimes one file holds two unrelated changes. git add -p (patch mode) breaks the file into hunks and asks about each: press y to stage it, n to skip it, s to split a big hunk into smaller pieces. It's the cleanest way to keep each commit focused on a single idea.
📋 Quick Reference
No commands given this time — just the plan. Curate a single focused commit out of a messy working tree, then check your diffs against the expected result in the comments.
Practice quiz
What is the staging area (also called the index)?
- A backup of your last commit
- A waiting room where you choose exactly which changes go into the next commit
- A remote copy of your repo
- A list of your branches
Answer: A waiting room where you choose exactly which changes go into the next commit. The staging area holds the precise set of changes that your next git commit will snapshot.
Which command moves a modified file into the staging area?
- git stage file.txt
- git commit file.txt
- git add file.txt
- git push file.txt
Answer: git add file.txt. git add copies the current state of the file into the staging area (index).
By default, what does plain show?
- Staged changes only
- Changes between two commits
- Unstaged changes — working directory vs the index
- Nothing, it needs a file argument
Answer: Unstaged changes — working directory vs the index. git diff with no flags compares your working directory to what is already staged.
Which command shows what is STAGED, i.e. what will go into the next commit?
- git diff
- git diff --staged
- git diff HEAD~1
- git status -s
Answer: git diff --staged. git diff --staged (a.k.a. --cached) compares the index against the last commit.
What does let you do?
- Add every file at once
- Add files matching a pattern
- Interactively stage individual hunks (parts) of a file
- Preview the commit message
Answer: Interactively stage individual hunks (parts) of a file. The -p (patch) flag walks you through each change hunk so you can stage some and skip others.
In , what does ' M file.txt' (space then M) mean?
- The file is staged
- The file has unstaged modifications in the working tree
- The file is untracked
- The file was deleted
Answer: The file has unstaged modifications in the working tree. The second column M means modified-but-not-staged; the leading space means nothing is staged yet.
In , what does 'M file.txt' (M then two spaces) mean?
- Unstaged change
- The modification is staged, ready for the next commit
- Merge conflict
- Untracked file
Answer: The modification is staged, ready for the next commit. M in the first column means the change is staged in the index.
What do '??' marks indicate in ?
- A merge conflict
- A staged deletion
- An untracked file Git is not following yet
- A renamed file
Answer: An untracked file Git is not following yet. ?? marks a file Git has never been told about — run git add to start tracking it.
After you a file, what does plain show for it?
- The same diff as before
- Nothing — there are no unstaged changes left
- The whole file
- An error
Answer: Nothing — there are no unstaged changes left. Once staged, the change lives in the index, so git diff (working vs index) shows nothing for it.
Why is the staging area useful for crafting clean commits?
- It compresses files
- It lets you group only related changes into one focused commit
- It speeds up pushes
- It prevents merge conflicts
Answer: It lets you group only related changes into one focused commit. Staging selectively means each commit can contain one logical change instead of a messy grab-bag.