git diff

Basic Snapshotting

Show changes between your files, the staging area and commits


The 'git diff' command shows the exact lines that changed. Without arguments it compares your working files with the staging area — the changes you have not staged yet. With --staged it compares the staging area with the last commit — what your next commit will contain. Give it commits or branches to compare any two versions of the project.

Synopsis

git diff [<options>] [<commit>] [--] [<path>...]
git diff [<options>] --staged [<commit>] [--] [<path>...]
git diff [<options>] <commit> <commit> [--] [<path>...]
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (4)

Compare the staging area with the last commit: the changes the next commit will record. --cached is a synonym.

RecommendedReview exactly what you are about to commit

Example

git diff --staged

Show a summary: each changed file with the number of changed lines, then a total.

RecommendedQuick overview of how big a change is

Example

git diff --stat

Show only the names of changed files.

When neededList changed files, e.g. to pass them to a linter

Example

git diff --name-only

Show the names of changed files with a letter: A added, M modified, D deleted, R renamed.

When neededSee which files were added, modified or deleted

Example

git diff --name-status

Choosing the Right Option

What git diff compares

Pick the two sides you want to compare

CommandLeft sideRight side
git diffStaging areaWorking files
git diff --stagedLast commitStaging area
git diff HEADLast commitWorking files
git diff A BCommit ACommit B

Examples

Unstaged changes

git diff

What changed in your files but is not staged yet.


Staged changes

git diff --staged

What the next commit will contain.


Two commits

git diff HEAD~1 HEAD

What the last commit changed.


Two branches

git diff main feature/login

Everything that differs between two branches.

Workflow Examples

1
git diff

Read what you changed

2
git add app.js

Stage it

3
git diff --staged

Read what will be committed

4
git commit -m "Add to cart"

Commit

1
git diff main...feature/login --stat

Files the branch changed since it split from main

2
git diff main...feature/login

The full changes

Troubleshooting

git diff shows nothing but files changed

Cause: The changes are already staged

Use git diff --staged (or git diff HEAD for both)

A new file is missing from the diff

Cause: Untracked files are not compared

Stage it with git add, then use git diff --staged

The whole file shows as changed

Cause: Line endings or whitespace changed

Try git diff -w to ignore whitespace and check core.autocrlf

Tips & Pitfalls

Best Practices

  • ✓Review git diff --staged before every commit.
  • ✓Use --stat first on big changes, then read the details.
  • ✓Compare branches with three dots (main...branch) to see only what the branch added.

Common Mistakes

  • ✗Expecting git diff to show staged changes — it does not; use git diff --staged.
  • ✗Expecting untracked files in git diff: new files appear only once they are added.
  • ✗Mixing up the order of two commits: git diff A B shows how to go from A to B.

FAQ

A:

git diff --staged (or its synonym git diff --cached).

A:

git diff HEAD compares the last commit with your working files.

A:

git show <commit>, or git diff <commit>~1 <commit>.