Lessons · Git · what changed, line by line
Seeing the change before you record it
git diff shows working-tree edits that are not staged. git diff --staged shows what is staged, which is exactly what the next commit will contain.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Reading the diff before committing catches the debug print, the stray file and the half-finished edit. It is code review of yourself, thirty seconds, every time.
How to think about it
Two questions, two commands: what have I changed that is not staged (diff), and what am I about to commit (diff --staged). If diff shows nothing after add, the change moved to --staged.
Worked example
echo change >> a.txtEdit a tracked file.
git diffWorking tree against the index: the edit shows as a + line, unstaged.
git add a.txt && git diffNothing: the change is staged now.
git diff --stagedIndex against the last commit: what the next commit will contain.
Your turn
Review what the next commit will contain.
git diff
Try it in a real repository
The trap
git diff after git add shows nothing and looks like the change vanished. It moved to --staged.