Git Diff

The git diff command shows the exact differences between two versions of a file — what lines were added, what lines were removed, and what lines remained unchanged.

Think of it as a "compare documents" feature. Just as a word processor can show what changed between two versions of a document (tracked changes), git diff shows the same for code files.

Reading the diff Output

Before learning the commands, it is important to understand how to read the diff output:

diff --git a/index.html b/index.html
index 3a5b6c7..8d9e0f1 100644
--- a/index.html
+++ b/index.html
@@ -1,4 +1,5 @@
 <html>
   <body>
-    <h1>Hello</h1>
+    <h1>Hello World</h1>
+    <p>Welcome to my site</p>
   </body>
 </html>
SymbolMeaning
---The original version of the file (before change)
+++The new version of the file (after change)
- (red)Line that was removed
+ (green)Line that was added
No symbolLine that did not change (context)
@@ -1,4 +1,5 @@Shows which line numbers changed in each version

Common git diff Use Cases

1. Compare Working Directory to Last Commit

git diff

Shows changes in the working directory that are NOT yet staged. In other words, what has been modified since the last commit, but not yet added with git add.

2. Compare Staged Changes to Last Commit

git diff --staged

(Also written as git diff --cached)

Shows what is currently in the staging area compared to the last commit. This is very useful before committing — to review exactly what will be saved.

3. Compare Working Directory + Staged to Last Commit

git diff HEAD

Shows all changes (both staged and unstaged) compared to the last commit.

Practical Example

# Create a file and make the first commit
echo "<h1>Hello</h1>" > index.html
git add index.html
git commit -m "Add index.html"

# Modify the file
echo "<p>Welcome</p>" >> index.html

# See what changed (not yet staged)
git diff
# Output shows: + <p>Welcome</p> (new line added)

# Stage the file
git add index.html

# Now git diff shows nothing (changes are staged, not unstaged)
git diff
# Output: (empty - nothing unstaged)

# But this shows what's staged vs last commit
git diff --staged
# Output shows: + <p>Welcome</p>

Comparing Two Specific Commits

git diff a3f92b1 b7c3d45

Shows all differences between commit a3f92b1 and commit b7c3d45. Commit hashes come from git log --oneline.

Comparing Two Branches

git diff main feature-login

Shows all differences between the main branch and the feature-login branch. This is very useful before merging a branch.

Comparing a Specific File Only

git diff index.html

Shows changes only in index.html, ignoring all other files.

Comparing a Specific File Between Two Branches

git diff main feature-login -- style.css

Shows how style.css differs between the main and feature-login branches.

Show Only the Names of Changed Files

git diff --name-only

Output:

index.html
style.css

No details — just the list of which files changed.

Show a Summary of Changes

git diff --stat

Output:

index.html | 3 ++-
style.css  | 10 +++++++---
2 files changed, 10 insertions(+), 3 deletions(-)

Summary of Common git diff Commands

CommandWhat it Compares
git diffUnstaged changes vs last commit
git diff --stagedStaged changes vs last commit
git diff HEADAll changes (staged + unstaged) vs last commit
git diff branch1 branch2Two branches
git diff commit1 commit2Two specific commits
git diff --name-onlyOnly the file names that changed
git diff --statA summary of how much changed in each file

Summary

git diff shows the exact line-by-line differences between versions of files. Lines prefixed with + were added, and lines prefixed with - were removed. Use git diff to see unstaged changes, git diff --staged to review what will be committed, and git diff branch1 branch2 to compare branches.

Leave a Comment

Your email address will not be published. Required fields are marked *