πŸ“š Advanced Git & GitHub Topics
Estimated reading: 5 minutes 279 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 :
Share

Git Diff

Or Copy Link

CONTENTS
Scroll to Top