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

Git diff Command

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

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.