Git diff Command
In this page:
Viewing Unstaged Changes
git diff with no arguments shows every change in your Working Directory that hasn't been staged yet, comparing your current files line-by-line against what's already in the staging area (or last commit, if nothing's staged).
Example: Viewing Unstaged Changes
git diff
Viewing Staged Changes
git diff --staged (or the equivalent --cached) shows the opposite view: exactly what's about to go into your next commit, comparing the staging area against your last commit -- the review step right before you actually commit.
Example: Viewing Staged Changes
git diff --staged
Comparing Two Commits
git diff <commit1> <commit2> compares any two points in your project's history directly, regardless of how far apart they are or which branches they're on, letting you see the full accumulated difference between them.
Example: Comparing Two Commits
git diff a1b2c3d e4f5g6h
Comparing Files Across Commits
Appending a file path to any diff command -- git diff commit1 commit2 -- path/to/file -- narrows the comparison to just that one file, which is much easier to read than scrolling through changes across an entire commit.
Example: Comparing Files Across Commits
git diff a1b2c3d e4f5g6h -- path/to/file.txt
Word-Level Diff
By default git diff highlights whole changed lines even if only one word differs; git diff --word-diff instead highlights the specific words that changed within a line, which is much clearer for prose or long single-line statements.
Example: Word-Level Diff
git diff --word-diff
Chapter Quiz — Complete all 18 topics to unlock
0/18 topics done
Complete these topics first:
- Git init Command
- Git New Files
- Git Staging Environment
- Git clone Command
- Git status Command
- Git help Command
- Git add Command
- Git commit Command
- Git tags Command
- Git stash Command
- Git log Command
- Git diff Command
- Git show Command
- Git rm Command
- Git mv Command
- Git restore Command
- Git clean Command
- Git ignore (.gitignore)