๐Ÿ“š Advanced Git & GitHub Topics
Estimated reading: 5 minutes 16 views

๐Ÿงฎ 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 diff is 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 diff with 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

OptionDescription
--statShow summary of changes (files changed, insertions)
--name-onlyShow only the names of changed files
--name-statusShow names + status (A, M, D)
--colorForce color output (even in pipes)
--word-diffHighlights word-level instead of line-level changes
--ignore-space-changeIgnores changes in amount of whitespace
--no-indexCompare 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 diff with git status โ†’ git diff shows what, git status shows that something changed.
  • โŒ Forgetting to use --cached to view staged changes
  • โŒ Not specifying file name when needed (git diff HEAD file.txt)
  • โŒ Using git diff to compare commits when git log -p may be more appropriate for browsing changes

โœ… Best Practices for Git Diff

  • Use git diff before every commit to review your work.
  • Use --stat or --name-only for summaries in code reviews.
  • Combine with tools like git log -p for better traceability.
  • Use git difftool for external diff tools if you prefer GUI-based comparisons.

๐Ÿ“Š Git Diff Command Summary Table

CommandDescription
git diffChanges not staged for commit
git diff --cachedStaged changes vs last commit
git diff HEADAll changes vs last commit
git diff main devCompare two branches
git diff commit1 commit2Compare two specific commits
git diff --statSummary of changes (files, lines added/deleted)
git diff --name-onlyOnly filenames changed
git diff --word-diffShow changes at word level
git diff --no-index file1 file2Compare 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 diff to compare working directory, staging area, and commits.
  • Understand the output format to spot additions/removals easily.
  • Combine with flags like --stat and --name-only for 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 :

Leave a Reply

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

Share

Git Diff

Or Copy Link

CONTENTS
Scroll to Top