🎓 A1Learner
🔍
0 of 50 lessons completed

Compare Changes with Diff

Diff prints the lines that changed. It compares two states and shows only the difference.

See the unstaged change

One line is new. The plus sign marks it.

$ git diff

See the staged change

After staging, the plain form prints nothing. The staged form shows the same line.

$ git add notes.txt
$ git diff --staged

See everything against HEAD

HEAD covers staged and unstaged changes together.

$ git diff HEAD

Example

$ git diff

Key points

  • Diff with no extra words compares the files with the last snapshot.
  • A plus line is new. A minus line is gone.
  • Diff after staging prints nothing, because the changes moved.
  • Diff with --staged compares the staging area with the last snapshot.
  • Diff with HEAD compares the files with the newest snapshot.
  • HEAD is the newest snapshot, so HEAD covers staged and unstaged changes together.
  • Read diffs before every commit. Surprises hide there.

Here, one line is new. The plus sign marks it. Everything else matches the last snapshot.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command shows staged changes?