Comparing Changes with git diff (Working Tree vs Staged vs HEAD)0%
Ignoring Unwanted Files and Secrets using .gitignore Rules

Comparing Changes with git diff (Working Tree vs Staged vs HEAD)

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Comparing Changes with git diff (Working Tree vs Staged vs HEAD)

Before staging or committing code, careful developers verify their exact modifications. The git diff command is Git's microscope: it shows line-by-line additions, deletions, and alterations using unified diff format.


1. The Three Forms of git diff

Because Git has three trees (Working Directory, Staging Area, and HEAD Commit), git diff provides three primary comparison targets:

Output
[ Working Directory ] <====== 1. git diff ==============> [ Staging Area ]
^
|
2. git diff --staged
|
v
<====== 3. git diff HEAD =========> [ HEAD (Last Commit) ]

1. git diff (Working Directory vs. Staging Area)

Shows changes you have made in files that have NOT yet been staged:

Bash / Terminal
git diff

2. git diff --staged (Staging Area vs. Last Commit)

Shows changes that are currently staged and ready to be included in the next commit:

Bash / Terminal
git diff --staged
# Synonym:
git diff --cached

3. git diff HEAD (Working Directory vs. Last Commit)

Shows all changes on your disk (both staged and unstaged combined) compared against the last recorded commit:

Bash / Terminal
git diff HEAD

2. Reading Unified Diff Syntax

When you run git diff, Git outputs unified diff notation:

DIFF
diff --git a/src/app.py b/src/app.py
index e69de29..b2d9a10 100644
--- a/src/app.py
+++ b/src/app.py
@@ -10,7 +10,8 @@ def calculate_discount(price, customer_type):
- if customer_type == "regular":
- return price * 0.05
+ if customer_type == "vip":
+ return price * 0.20
+ elif customer_type == "regular":
+ return price * 0.10
return 0

Decoding the Diff:

  • --- a/src/app.py : Original version before changes (pre-image).
  • +++ b/src/app.py : Modified version after changes (post-image).
  • @@ -10,7 +10,8 @@ : Hunk header. In the original file, chunk begins at line 10 and spans 7 lines; in the new file, chunk begins at line 10 and spans 8 lines.
  • - (Red) : Removed / deleted lines.
  • + (Green) : Added / inserted lines.
  • White / uncolored text : Unchanged context lines around the modification.

3. Targeted Diffs

Compare a Specific File:

Bash / Terminal
git diff src/components/Button.jsx

Compare Two Different Branches:

See what changes exist in feature-login compared to main:

Bash / Terminal
git diff main..feature-login

Compare Two Commits by Hash:

Bash / Terminal
git diff 3a89e12..7b3f81e

Word-Level Diff (--word-diff):

When you change only one word inside a long paragraph, viewing the entire line as deleted and re-added can be noisy:

Bash / Terminal
git diff --word-diff

Interactive Quiz

Practice Quiz

Question 1
What does running git diff without any arguments compare?
Question 2
Which command shows the exact diff of changes that are already staged and about to be committed?
Question 3
In a Git diff output, what do lines beginning with a green + signify?
Question 4
What does git diff HEAD display?
Question 5
How can you view the changes introduced between two branches named dev and main?
Next Lesson

Ignoring Unwanted Files and Secrets using .gitignore Rules

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext