Master Comparing Changes in Git
Git diff is one of the most powerful tools for understanding changes in your codebase. This comprehensive guide covers all essential git diff commands with practical examples.
Shows changes in your working directory that haven't been staged yet.
(Working Directory vs. Index)
@@ -1 +1 @@
-Hello World
+Hello Git
Shows changes that have been staged but not committed.
(Index vs. HEAD)
@@ -1 +1 @@
-Hello World
+Hello Git
Shows changes that have been staged but not committed (same as --staged).
(Index vs. HEAD)
@@ -1 +1 @@
-Hello World
+Hello Git
Shows both staged and unstaged changes (all modifications since last commit).
(Working Directory vs. Last Commit)
@@ -1 +1 @@
-Hello World
+Hello Git
@@ -5,6 +5,6 @@
- console.log("Old code");
+ console.log("New code");
Shows changes in a specific file only.
@@ -1 +1 @@
-Hello World
+Hello Git
Shows changes in all files within a specific directory.
@@ -5,6 +5,6 @@
- console.log("Old code");
+ console.log("New code");
@@ -1,3 +1,3 @@
- function oldUtil() {}
+ function newUtil() {}
Shows only the names of files with changes, not the actual diff content.
file.txt
src/app.js
README.md
Shows filenames with change type (A=added, M=modified, D=deleted).
M file.txt
A newfile.js
D oldfile.txt
Shows a concise summary of changes instead of full diff.
file.txt | 2 +-
app.js | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
Shows differences between any two commits in your history.
@@ -1 +1 @@
-Hello World
+Hello Git
Shows changes made in the last 3 commits (replace 3 with any number).
@@ -1 +1 @@
-Version 1
+Version 2
@@ -1,3 +1,3 @@
-Old content
+New content
Shows changes in feature branch that aren't in main branch yet.
@@ -1 +1 @@
-Hello from main
+Hello from feature
Shows changes in feature branch that aren't in main branch.
@@ -1 +1 @@
-Hello from main
+Hello from feature
Compare local main branch with remote main branch.
@@ -1 +1 @@
-Hello from remote
+Hello from local
Shows changes in your current branch compared to the merge base with another branch.
@@@ -1,1 -1,1 +1,1 @@@
-Hello from main
-Hello from feature
+Hello merged
Shows word-level differences instead of line-level.
Hello [-World-]{+Git+}
Shows differences while ignoring whitespace changes.
(No output if only whitespace differences exist)
Compare changes to a specific file between two commits.
@@ -5,6 +5,6 @@
- "version": "1.0.0",
+ "version": "2.0.0",
Compare changes between two points in time.
(Shows all changes made in the last 24 hours)
Shows differences while excluding minified JS and CSS files.
@@ -5,6 +5,6 @@
- console.log("Old code");
+ console.log("New code");
(No min.js or .css files shown)
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)
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
Shows filenames and change types between two branches.
M file.txt
A newfile.js
D oldfile.txt
R100 oldname.txt newname.txt
Quickly see what you're about to push to remote.
README.md | 4 +++-
src/index.js | 5 ++++-
2 files changed, 7 insertions(+), 2 deletions(-)
Shows changes in your working directory that haven't been staged yet.
@@ -1 +1 @@
-Hello World
+Hello Git
Shows changes that have been staged but not committed.
@@ -1 +1 @@
-Hello World
+Hello Git
Shows both staged and unstaged changes since last commit.
@@ -1 +1 @@
-Hello World
+Hello Git
Shows changes to a specific file only.
@@ -1 +1 @@
-Hello World
+Hello Git
Shows only names of files with changes.
file.txt
src/app.js
README.md
Shows filenames with change type (A=added, M=modified, D=deleted).
M file.txt
A newfile.js
D oldfile.txt
Shows a concise summary of changes instead of full diff.
file.txt | 2 +-
app.js | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
Shows differences between any two commits.
@@ -1 +1 @@
-Hello World
+Hello Git
Shows changes made in the last 3 commits.
@@ -1 +1 @@
-Version 1
+Version 2
Shows changes since yesterday (relative date reference).
@@ -1 +1 @@
-Old content
+New content
Shows changes in feature branch not yet in main.
@@ -1 +1 @@
-Hello from main
+Hello from feature
Compare local main branch with remote main branch.
@@ -1 +1 @@
-Hello from remote
+Hello from local
Shows changes compared to the merge base with another branch.
@@@ -1,1 -1,1 +1,1 @@@
-Hello from main
+Hello merged
Shows word-level differences instead of line-level.
Hello [-World-]{+Git+}
Ignores whitespace changes when comparing.
(No output if only whitespace differences)
Detects renamed files and shows them appropriately.
Shows word-level differences with color highlighting.
Hello World
Git
Quick overview of what you're about to commit.
file.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
See what you're about to push to remote.
README.md | 4 +++-
src/index.js | 5 ++++-
2 files changed, 7 insertions(+), 2 deletions(-)
See what changed in the last 24 hours.
@@ -1 +1 @@
-Yesterday's content
+Today's content
Combine multiple flags for powerful workflows. For example:
git diff --name-status --stat origin/main will show both
detailed changes and a summary.