As its name suggests, git diff shows the difference: the exact changes you made in each file.

An example

In a file, we delete one line, change another and add a new one. git status only says the file is modified. To see the details:

git diff
-a deleted line
-the old version of the line
+the new version of the line
+an added line
  • Lines starting with - (in red) were removed.
  • Lines starting with + (in green) were added.
  • A changed line shows up as a removal of the old version, followed by an addition of the new one.

Why use it before every commit

It’s the best way to spot what doesn’t belong in your commit:

  • spaces or line breaks added by accident;
  • changes added automatically by your editor;
  • leftover debugging.

Nothing serious, but it makes the commit less clean and harder to review. Personally, I run git diff before every commit.

Useful variants

CommandWhat it shows
git diffChanges not yet staged with git add
git diff --stagedChanges already staged, which will go into the next commit
git diff <file>Changes in a single file

Exercise · What goes into the commit

You ran git add on two files, then changed one of them again. Which command shows exactly what will go into your next commit?

Show the solution
git diff --staged

A plain git diff only shows the latest change, not yet staged. It won’t go into the commit until you run git add again.

End of the course

Well done, you’ve finished Git in Practice! You can now install Git, clone and initialize a project, work with branches, set work aside, write clean commits, contribute with forks and pull requests, and keep your repository tidy.


This is Chapter 11 of the Git in Practice course. Follow along to learn how to use Git every day and as a team.