bashintermediate

Advanced Git Diff Techniques

Compare changes between branches, commits, and working tree with filtering and formatting options.

bash
# Working directory vs staged
git diff

# Staged vs last commit
git diff --staged

# Between two branches
git diff main..feature-branch

# Between two commits
git diff abc1234 def5678

# Only show file names
git diff --name-only main..feature-branch

# Show stat summary
git diff --stat main..feature-branch

# Filter by file type
git diff main..feature-branch -- '*.ts' '*.tsx'

# Ignore whitespace
git diff -w main..feature-branch

# Word-level diff (not line-level)
git diff --word-diff

# Show only added/deleted/modified files
git diff --diff-filter=A main..feature-branch  # Added only
git diff --diff-filter=D main..feature-branch  # Deleted only
git diff --diff-filter=M main..feature-branch  # Modified only

# Compare specific file between branches
git diff main..feature-branch -- src/lib/registry.ts

# Create a diff patch
git diff main..feature-branch > changes.patch

# Color-moved lines detection
git diff --color-moved=dimmed-zebra

# Three-dot diff (changes since branch point)
git diff main...feature-branch

Use Cases

  • Reviewing changes before code review
  • Comparing branches for release preparation
  • Filtering diffs by file type or change kind

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.