Git Diff Command - Complete Guide with Examples

Mahesh Mahesh Waghmare
2 min read

The git diff command shows differences between commits, branches, files, and the working directory. It’s essential for reviewing changes, understanding modifications, and preparing commits.

This guide covers all aspects of git diff with practical examples.

Introduction to git diff

git diff displays changes between different states of your repository. It’s one of the most useful Git commands for understanding what has changed.

Common Use Cases:

  • Review changes before committing
  • Compare branches
  • View commit differences
  • Check staged vs unstaged changes
  • Understand file modifications

Basic Usage

Unstaged Changes

git diff

Shows differences between working directory and staging area.

Staged Changes

git diff --staged
# or
git diff --cached

Shows differences between staging area and last commit.

Specific File

git diff file.txt

Shows changes in specific file.

Advertisement

Comparing Commits

Compare Two Commits

git diff commit1 commit2

Compare with HEAD

git diff HEAD~1 HEAD

Compare with Previous Commit

git diff HEAD~1

Comparing Branches

Compare Branches

git diff branch1 branch2

Compare with Current Branch

git diff main

Compare Branch Files

git diff main -- file.txt

Diff Options

Stat Summary

git diff --stat

Shows file names and change statistics.

Word Diff

git diff --word-diff

Highlights word-level changes.

Color Output

git diff --color

Color-coded diff output.

Advertisement

Advanced Usage

Ignore Whitespace

git diff -w

Unified Diff Format

git diff -u

Side-by-Side

git diff --side-by-side

Conclusion

git diff is essential for:

  • Reviewing changes before commits
  • Comparing different repository states
  • Understanding modifications
  • Debugging issues

Key Points:

  • Shows unstaged changes by default
  • Use --staged for staged changes
  • Compare commits, branches, files
  • Many options for different views
  • Essential for code review

Mastering git diff improves your Git workflow and code review process.

Advertisement
Mahesh Waghmare

Written by Mahesh Waghmare

I bridge the gap between WordPress architecture and modern React frontends. Currently building tools for the AI era.

Follow on Twitter

Read Next