๐งฎ Git Diff โ Compare Changes Between Commits, Branches, and Files
๐งฒ Introduction โ Why Use Git Diff?
When working with Git, one of the most essential things you need to do is see what has changed. Whether you’re reviewing your own edits, inspecting others’ work, or analyzing history, git diff provides a powerful way to compare differences between files, commits, branches, and more.
๐ฏ In this comprehensive guide, you’ll learn:
- What git diffis and what it compares
- Syntax and usage in various real-world scenarios
- How to compare working directory vs staging vs commits
- How to use git diffwith branches, files, and ranges
- Advanced options and best practices
๐ What Is git diff?
git diff is a command-line tool that lets you view the difference between:
- Files in your working directory and the staging area
- The staging area and the last commit
- Any two commits or branches
It shows line-by-line changes, additions, and deletions with color-coded output:
- +โ lines added
- -โ lines removed
๐งฐ Basic Syntax and Command Format
git diff [options] [<commit> [<commit>]] [--] [<path>โฆ]
๐ Key components:
- No arguments: compare working directory with the staging area
- One commit: compare that commit with the staging area or working tree
- Two commits: compare one commit against another
- --: separates file paths from commit arguments
๐ ๏ธ Use Cases and Examples
๐น 1. Compare Working Directory vs Staging Area
git diff
๐ก Shows what you’ve changed but not staged.
๐น 2. Compare Staging Area vs Last Commit
git diff --cached
๐ก Shows whatโs staged and ready to commit.
๐น 3. Compare Working Directory vs Last Commit (All Changes)
git diff HEAD
๐ก Useful for reviewing all changes (staged + unstaged) since your last commit.
๐น 4. Compare Two Branches
git diff main dev
๐ก Compares the tips of main and dev branches.
You can also use this format:
git diff origin/main..origin/dev
๐น 5. Compare Two Commits
git diff 5e2d4b1 6a9f834
๐ก Shows changes introduced between two specific commits.
๐น 6. Compare a Commit to Working Directory
git diff 5e2d4b1
๐ก Shows changes between the given commit and your current working directory.
๐น 7. Compare a File in Two Branches
git diff main dev -- index.js
๐ก Only shows differences in index.js between main and dev.
๐น 8. Compare Specific Files
git diff HEAD~1 index.html
๐ก Shows how index.html changed in the last commit.
๐จ Understanding Git Diff Output
The output looks like this:
diff --git a/main.py b/main.py
index 8c6b5f3..e3a1e2f 100644
--- a/main.py
+++ b/main.py
@@ def hello_world():
-    print("Hello, world!")
+    print("Hello, Git!")
Breakdown:
- ---is the original version
- +++is the new version
- @@shows the line number of changes
- -line removed
- +line added
โ๏ธ Useful Git Diff Options
| Option | Description | 
|---|---|
| --stat | Show summary of changes (files changed, insertions) | 
| --name-only | Show only the names of changed files | 
| --name-status | Show names + status (A, M, D) | 
| --color | Force color output (even in pipes) | 
| --word-diff | Highlights word-level instead of line-level changes | 
| --ignore-space-change | Ignores changes in amount of whitespace | 
| --no-index | Compare two files outside Git repo | 
๐ง Advanced Git Diff Scenarios
๐ 1. Compare Merge Base of Two Branches
git diff $(git merge-base main dev) dev
๐ก Useful to see only changes unique to a feature branch.
๐งช 2. Compare Untracked Files Using --no-index
git diff --no-index file1.txt file2.txt
๐ก Works outside a Git repository like the diff Linux tool.
๐ 3. Show Differences in Last Commit (Like git show)
git diff HEAD~1 HEAD
# OR
git show --name-only HEAD
๐งฉ Git Diff GUI Tools
Want visual diff comparisons?
- VS Code Git Panel: Highlighted file diffs and side-by-side comparison
- GitKraken: Visual diff view with change highlighting
- Sourcetree: Inline diff and file-by-file change views
- Meld/KDiff3: GUI diff tools for staging and merge conflict resolution
โ ๏ธ Common Mistakes and Misunderstandings
- โ Confusing git diffwithgit statusโgit diffshows what,git statusshows that something changed.
- โ Forgetting to use --cachedto view staged changes
- โ Not specifying file name when needed (git diff HEAD file.txt)
- โ Using git diffto compare commits whengit log -pmay be more appropriate for browsing changes
โ Best Practices for Git Diff
- Use git diffbefore every commit to review your work.
- Use --stator--name-onlyfor summaries in code reviews.
- Combine with tools like git log -pfor better traceability.
- Use git difftoolfor external diff tools if you prefer GUI-based comparisons.
๐ Git Diff Command Summary Table
| Command | Description | 
|---|---|
| git diff | Changes not staged for commit | 
| git diff --cached | Staged changes vs last commit | 
| git diff HEAD | All changes vs last commit | 
| git diff main dev | Compare two branches | 
| git diff commit1 commit2 | Compare two specific commits | 
| git diff --stat | Summary of changes (files, lines added/deleted) | 
| git diff --name-only | Only filenames changed | 
| git diff --word-diff | Show changes at word level | 
| git diff --no-index file1 file2 | Compare files not tracked by Git | 
๐ Summary โ Git Diff
git diff is a powerful and flexible way to compare code and track changes in a project. Whether youโre preparing a commit, reviewing branches, or debugging, it gives you fine-grained visibility into your code evolution.
๐ Key Takeaways:
- Use git diffto compare working directory, staging area, and commits.
- Understand the output format to spot additions/removals easily.
- Combine with flags like --statand--name-onlyfor clean summaries.
โ๏ธ Real-World Relevance: Every developer uses git diff during code reviews, debugging, and staging. Mastering it helps you spot bugs early, reduce merge conflicts, and write better commits.
โ Git Diff โ Frequently Asked Questions (FAQs)
โ How do I compare staged changes in Git?
โ
 Use:
git diff --cached
โ How can I view only the changed file names?
โ
 Use:
git diff --name-only
โ Whatโs the difference between git diff and git status?
โ
 git status tells you what has changed, while git diff shows you how it changed.
โ Can I use git diff to compare branches?
โ
 Yes:
git diff branch1 branch2
โ How do I compare a single file in two branches?
โ
 Use:
git diff main dev -- file.js
โ How do I summarize file changes instead of showing full diffs?
โ
 Use:
git diff --stat
Share Now :
