Basic Comparisons

Basic
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
Basic
git diff --staged

Shows changes that have been staged but not committed.

(Index vs. HEAD)

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git

File & Directory Comparisons

Basic
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
Basic
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() {}
Basic
git diff --name-only

Shows only the names of files with changes, not the actual diff content.

file.txt
src/app.js
README.md
Basic
git diff --name-status

Shows filenames with change type (A=added, M=modified, D=deleted).

M file.txt
A newfile.js
D oldfile.txt
Basic
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

Intermediate
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
Intermediate
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
Intermediate
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
Intermediate
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
Advanced
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

Advanced
git diff --word-diff

Shows word-level differences instead of line-level.

Hello [-World-]{+Git+}
Advanced
git diff -w

Shows differences while ignoring whitespace changes.

(No output if only whitespace differences exist)
Advanced
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",
Advanced
git diff @{1.day.ago} @{now}

Compare changes between two points in time.

(Shows all changes made in the last 24 hours)
Advanced
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)
Advanced
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

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

Workflow Integration

Intermediate
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
Intermediate
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(-)
Intermediate
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.