bashbeginner

Git Diff — Word, Stat, and Patch Options

Advanced diff techniques: word-level diff, stat summary, diff between branches, and patch creation.

bash
# Diff of unstaged changes
git diff

# Diff of staged changes
git diff --staged    # or --cached

# Word-level diff (great for prose/docs)
git diff --word-diff

# Color words inline
git diff --color-words

# Stat summary (files changed, lines added/removed)
git diff --stat

# Name-only (just list changed files)
git diff --name-only
git diff --name-status          # with A/M/D status

# Diff between branches
git diff main..feature/auth
git diff main..feature/auth -- src/  # specific directory

# Diff between commits
git diff abc1234..def5678

# Diff ignoring whitespace
git diff -w
git diff --ignore-space-change

# Show diff for a single file
git diff -- src/auth.ts

# Create a patch file
git diff > my-changes.patch
git diff --cached > staged-changes.patch

# Apply a patch
git apply my-changes.patch
git apply --check my-changes.patch   # dry run

# Diff with external tool
git difftool main..feature/auth

Use Cases

  • Reviewing changes before committing
  • Comparing feature branches against main
  • Creating and applying patches for code review

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.