Basic Comparisons
git diff
Shows changes in your working directory that haven't been staged yet.
(Working Directory vs. Index)
diff --git a/file.txt b/file.txt
index abc123..def456
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-Hello World
+Hello Git
git diff --staged
Shows changes that have been staged but not committed.
(Index vs. HEAD)
@@ -1 +1 @@
-Hello World
+Hello Git
File & Directory Comparisons
git diff file.txt
Shows changes in a specific file only.
diff --git a/file.txt b/file.txt
index abc123..def456
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-Hello World
+Hello Git
git diff src/
Shows changes in all files within a specific directory.
diff --git a/src/app.js b/src/app.js
index 111222..333444
--- a/src/app.js
+++ b/src/app.js
@@ -5,6 +5,6 @@
-console.log("Old code");
+console.log("New code");
diff --git a/src/utils.js b/src/utils.js
index abcdef..123456
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,3 +1,3 @@
-function oldUtil() {}
+function newUtil() {}
git diff --name-only
Shows only the names of files with changes, not the actual diff content.
file.txt
src/app.js
README.md
git diff --name-status
Shows filenames with change type (A=added, M=modified, D=deleted).
M file.txt
A newfile.js
D oldfile.txt
git diff --stat
Shows a concise summary of changes instead of full diff.
file.txt | 2 +-
app.js | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
Commit & Branch Comparisons
git diff commit1 commit2
Shows differences between any two commits in your history.
diff --git a/file.txt b/file.txt
index abc123..xyz789
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-Hello World
+Hello Git
git diff HEAD~3 HEAD
Shows changes made in the last 3 commits (replace 3 with any number).
diff --git a/file1.txt b/file1.txt
index abc123..def456
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1 @@
-Version 1
+Version 2
diff --git a/file2.txt b/file2.txt
index 111222..333444
--- a/file2.txt
+++ b/file2.txt
@@ -1,3 +1,3 @@
-Old content
+New content
git diff main..feature
Shows changes in feature branch that aren't in main branch yet.
diff --git a/file.txt b/file.txt
index abc123..def456
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-Hello from main
+Hello from feature
git diff origin/main main
Compare local main branch with remote main branch.
diff --git a/file.txt b/file.txt
index abc123..def456
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-Hello from remote
+Hello from local
git diff --merge-base feature
Shows changes in your current branch compared to the merge base with another branch.
diff --cc file.txt
index abc123,def456..000000
--- a/file.txt
+++ b/file.txt
@@@ -1,1 -1,1 +1,1 @@@
-Hello from main
-Hello from feature
+Hello merged
Advanced Comparisons
git diff --word-diff
Shows word-level differences instead of line-level.
Hello [-World-]{+Git+}
git diff -w
Shows differences while ignoring whitespace changes.
(No output if only whitespace differences exist)
git diff commit1 commit2 -- package.json
Compare changes to a specific file between two commits.
diff --git a/package.json b/package.json
index 123..456
--- a/package.json
+++ b/package.json
@@ -5,6 +5,6 @@
- "version": "1.0.0",
+ "version": "2.0.0",
git diff @{1.day.ago} @{now}
Compare changes between two points in time.
(Shows all changes made in the last 24 hours)
git diff ":!*.min.js" ":!*.css"
Shows differences while excluding minified JS and CSS files.
diff --git a/app.js b/app.js
index 111222..333444
--- a/app.js
+++ b/app.js
@@ -5,6 +5,6 @@
-console.log("Old code");
+console.log("New code");
(No min.js or .css files shown)
git fetch origin && git diff origin/main main
First update remote references, then compare local with remote.
$ git fetch origin
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 2), reused 0 (delta 0)
remote: Total 5 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
From https://github.com/user/repo
abc123..def456 main -> origin/main
@@ -1 +1 @@
-Hello from remote
+Hello from local
Workflow Integration
git diff --name-status main..feature
Shows filenames and change types between two branches.
M file.txt
A newfile.js
D oldfile.txt
R100 oldname.txt newname.txt
git diff --stat origin/main
Quickly see what you're about to push to remote.
README.md | 4 +++-
src/index.js | 5 ++++-
2 files changed, 7 insertions(+), 2 deletions(-)
git diff @{1.day.ago} @{now}
See what changed in the last 24 hours.
diff --git a/file.txt b/file.txt
index abc123..def456
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-Yesterday's content
+Today's content
Pro Tip
Combine multiple flags for powerful workflows. For example:
git diff --name-status --stat origin/main
will show both detailed changes and a summary.
Complete Git Diff Cheat Sheet • Updated 2024
Use these commands to better understand your code changes and collaborate effectively.