How to view differences in Git

· Category: Git

Short answer

Use git diff to see changes between your working directory, staging area, and commits.

Steps

  1. View unstaged changes:
git diff
  1. View staged changes:
git diff --staged
  1. Compare two commits:
git diff abc1234 def5678
  1. Compare a specific file across branches:
git diff main..feature -- file.txt

Tips

  • git diff --stat shows a summary of changed files.
  • Use --word-diff for prose documents to see word-level changes.
  • Redirect output to a file: git diff > changes.patch.

Common issues

  • No output after staging: remember --staged is required for staged changes.
  • Binary files: diffing binaries is not useful; use external diff tools for images.