← Back to Git Course | Chapter 2: Basic Commands | Lesson 12 of 18

Git diff Command

git diff is like a spot-the-difference game: it shows exactly which lines you added or removed since the last time you saved.
Syntax
bash
git diff
git diff --staged
git diff commit1 commit2

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

bash
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

bash
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

bash
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

bash
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

bash
git diff --word-diff
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Running git diff after git add and seeing nothing, because it compares the working directory to the staging area; use git diff --staged.
  2. Confusing git diff with git status, when git diff shows the line-level changes and git status shows only file names.
  3. Reading the - and + lines backwards, when - marks a line removed and + marks a line added.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.