TetSharing
Git & CLI tutorial · 12 chapters
  1. Terminal basics
  2. Working with files
  3. Pipes and redirection
  4. What Git is
  5. Staging and commits
  6. Viewing history
  7. Comparing changes
  8. Branches
  9. Merging
  10. Merge conflicts
  11. Undoing changes
  12. Remotes, push and pull

Final Git & CLI quiz
Cheat sheet: git commands
Cheat sheet: linux shell commands

Chapter 7 of 12 · By Ethan Cole · Updated November 21, 2026

Git diff: See Exactly What Changed, Line by Line

Before you commit, you should know exactly what you are committing. git diff shows changes line by line, and it is the habit that separates careful commits from "oops, I committed my debug prints".

Which changes?

git diff compares two states, and the arguments decide which:

  • git diff with no arguments: working directory versus the staging area. These are changes you have not staged yet.
  • git diff --staged (same as --cached): the staging area versus the last commit. This is exactly what will be committed.
  • git diff HEAD: everything that changed since the last commit, staged or not.
  • git diff abc1234 def5678: two commits. git diff HEAD~2 HEAD shows what the last two commits changed together.

Add a file path at the end (git diff -- notes.txt) to limit the output to one file.

Reading the output

A diff is made of a short header and one or more hunks:

  • --- a/file and +++ b/file name the old and new versions.
  • @@ -1,3 +1,4 @@ says the hunk starts at line 1 and covers 3 lines in the old file and 4 in the new.
  • Lines starting with - were removed, lines starting with + were added, and lines starting with a space are unchanged context to help you find your place.

A changed line appears as a removal followed by an addition.

Terminal · bash + git · copy and run locally
git init -q
printf 'eggs\nmilk\nbread\n' > list.txt
git add list.txt
git commit -q -m "Shopping list"
printf 'eggs\noat milk\nbread\ncoffee\n' > list.txt
git diff
git diff --staged
git add list.txt
git diff
git diff --staged --stat

Transcript (verified by our test suite in a fresh temp folder)

$ git init -q
$ printf 'eggs\nmilk\nbread\n' > list.txt
$ git add list.txt
$ git commit -q -m "Shopping list"
$ printf 'eggs\noat milk\nbread\ncoffee\n' > list.txt
$ git diff
diff --git a/list.txt b/list.txt
index f43d6e7..54b312c 100644
--- a/list.txt
+++ b/list.txt
@@ -1,3 +1,4 @@
 eggs
-milk
+oat milk
 bread
+coffee
$ git diff --staged
$ git add list.txt
$ git diff
$ git diff --staged --stat
 list.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

The transcript commits a short shopping list, edits it, and runs git diff before and after staging. Before git add, plain git diff shows the change and --staged shows nothing. After git add, it is the other way around. That flip is the clearest way to understand the staging area.

Summaries

git diff --stat replaces the line-by-line detail with one line per file and a bar of plus and minus signs, which is useful in large changes. git diff --word-diff highlights changed words inside a line, handy for prose and documentation.

A review routine

Before every commit:

  1. git status to see which files changed.
  2. git diff to review unstaged work, then git add what belongs together.
  3. git diff --staged for a last look at the commit you are about to make.

Common mistakes

  • Running plain git diff after staging and thinking there are no changes. Use --staged.
  • Reading the - and + lines backwards: - is the old version, + the new.

Summary

  • git diff is unstaged work; git diff --staged is the next commit.
  • Hunks show removed (-), added (+) and context lines.
  • --stat summarizes; review before every commit.

Quiz: Comparing changes

1. You ran git add on every change. Which command still shows those changes?

After staging, the difference is between the staging area and the last commit.

2. In a diff, what does a line starting with + mean?

+ marks added lines; - marks removed lines.

3. Which command compares the working directory with the last commit, including staged and unstaged changes?

git diff HEAD compares everything you have now with the last commit.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.