What is the difference between git diff and git diff --staged?
git diff shows changes in your working files that are not yet staged. git diff --staged shows changes already added to the staging area, which is exactly what the next commit will contain.
Explain it simply
Think of a shop bill in two stages. The items in your hand are the working directory. The items already on the billing counter are the staged ones. git diff compares what is in your hand with the counter. git diff --staged compares the counter with what the shop already recorded, the last commit.
So a clean sequence is: edit, git diff to review, git add, git diff --staged to review once more, then commit. After git add, plain git diff goes empty for that file, which surprises beginners. git diff HEAD shows both stages together against the last commit.
Example
echo "line two" >> notes.txt
git diff # shows the new line
git add notes.txt
git diff # prints nothing now
git diff --staged # shows the new line
git diff HEAD # staged plus unstagedWhat to say in the interview
"git diff compares the working directory with the index, so it shows unstaged changes. git diff --staged, also spelt --cached, compares the index with HEAD, so it shows what will be committed. I run both before committing, and git diff HEAD when I want everything at once."
Common mistakes
- Seeing an empty git diff after git add and thinking the changes vanished.