git diff — Seeing What Changed
git status tells you which files changed. git diff tells you what changed — the exact lines added, removed, or modified. It is the tool you use before staging to review your own work, and before committing to make sure what you are about to save is intentional.
git diff: unstaged changes
Section titled “git diff: unstaged changes”git diff with no arguments shows changes in your working tree that have not been staged yet — the difference between your current files and the last staged version (or the last commit if nothing is staged).
Make a change to pine-ridge.txt in your git-practice folder without staging it, then run:
git diffYou will see output like this:
diff --git a/pine-ridge.txt b/pine-ridge.txtindex 8d6a2f1..c4b3e7a 100644--- a/pine-ridge.txt+++ b/pine-ridge.txt@@ -1 +1,2 @@ Tour notes for Pine Ridge+Best time to visit: spring or fallReading the unified diff format
Section titled “Reading the unified diff format”The diff format appears throughout Git. Here is what each part means:
diff --git a/pine-ridge.txt b/pine-ridge.txtThe file being compared. a/ is the “before” version, b/ is the “after” version.
--- a/pine-ridge.txt+++ b/pine-ridge.txtMarkers for the two versions. Lines starting with --- are from the old version; lines starting with +++ are from the new version.
@@ -1 +1,2 @@The hunk header. -1 means the old version had 1 line starting at line 1. +1,2 means the new version has 2 lines starting at line 1.
Tour notes for Pine Ridge+Best time to visit: spring or fallThe actual content. Lines with no prefix are context (unchanged). Lines with + are additions. Lines with - are removals.
When a line is modified, Git shows it as one removal and one addition:
Tour notes for Pine RidgeTour notes for Pine Ridge Loopgit diff —staged: staged changes
Section titled “git diff —staged: staged changes”git diff only shows unstaged changes. To see what is in the staging area — what will go into the next commit — add the --staged flag:
git diff --stagedThis shows the difference between the staging area and the last commit.
| Command | What it compares |
|---|---|
git diff | Working tree vs. staging area (unstaged changes) |
git diff --staged | Staging area vs. last commit (staged changes) |
git diff HEAD | Working tree vs. last commit (all changes, staged and unstaged) |
Comparing specific files
Section titled “Comparing specific files”To diff a single file instead of all changes:
git diff pine-ridge.txtgit diff --staged pine-ridge.txtComparing commits
Section titled “Comparing commits”You can diff any two commits using their hashes:
git diff a3f8c12 b7e9d03Or compare a commit to the current state:
git diff a3f8c12You will use this more in Module 04 when working with history.
When there is nothing to diff
Section titled “When there is nothing to diff”If nothing has changed since the last commit or stage, git diff produces no output and returns immediately. This is not an error — it means there is nothing to show.
Exercise
Section titled “Exercise”-
In your
git-practicefolder, editpine-ridge.txtto add a new line and change an existing one. -
Run
git diffand read the output. Identify the hunk header, context lines, added lines (+), and removed lines (-). -
Stage the file:
git add pine-ridge.txt-
Run
git diff. Notice there is no output — the unstaged diff is empty because you staged everything. -
Run
git diff --staged. Now you see the staged changes — the same diff you saw before staging. -
Make one more edit to
pine-ridge.txtwithout staging it. Now run bothgit diffandgit diff --stagedand compare what each shows.
git diffshows unstaged changes — the difference between your working tree and the staging area (or last commit).git diff --stagedshows staged changes — what will go into the next commit.git diff HEADshows all changes, staged and unstaged, compared to the last commit.- The unified diff format:
-for removed lines,+for added lines, no prefix for unchanged context. - The
@@hunk header tells you which line numbers are affected. - Diffing a specific file:
git diff <filename>.
Next: a closer look at the staging area — how to stage partial changes, unstage files, and check exactly what is staged before you commit.