Skip to main content

Synopsis

git diff [<options>] [<commit>] [--] [<path>...]
git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]
git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]
git diff [<options>] <commit>...<commit> [--] [<path>...]
git diff [<options>] <blob> <blob>
git diff [<options>] --no-index [--] <path> <path>

Description

Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes resulting from a merge, changes between two blob objects, or changes between two files on disk. The git diff command is one of the most frequently used Git commands for reviewing changes before staging or committing them.

Common Usage

1

View unstaged changes

See what you’ve changed but not yet staged:
git diff
2

View staged changes

See what will be committed:
git diff --cached
3

Compare branches

View differences between two branches:
git diff main..feature-branch

Options

View the changes you staged for the next commit relative to the named commit (typically HEAD).
git diff --cached
This shows what you would be committing if you run git commit without -a option.
Compare two paths on the filesystem without requiring a Git repository.
git diff --no-index file1.txt file2.txt
Instead of comparing directly, use the merge base of the commits.
git diff --merge-base main feature
Equivalent to git diff $(git merge-base main feature) feature.
Generate a diffstat showing which files changed and by how much.
git diff --stat
Show only names of changed files.
git diff --name-only
Show only names and status of changed files (Added, Modified, Deleted).
git diff --name-status
Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), or have their type changed (T).
git diff --diff-filter=MRC
Swap the input sources, showing the diff in reverse.
git diff -R

Examples

Checking your working tree

# Changes in the working tree not yet staged
git diff

# Changes between the index and your last commit
git diff --cached

# Changes in the working tree since your last commit
git diff HEAD

# Changes made to resolve merge conflicts
git diff AUTO_MERGE

Comparing with arbitrary commits

# Compare with the tip of "test" branch
git diff test

# Compare a specific file with current branch tip
git diff HEAD -- ./test

# Compare the version before last commit with last commit
git diff HEAD^ HEAD

Comparing branches

# Changes between the tips of topic and master branches
git diff topic master

# Same as above
git diff topic..master

# Changes on master since topic branched from it
git diff topic...master

Limiting the diff output

# Show only modifications, renames, and copies
git diff --diff-filter=MRC

# Show only names and nature of change
git diff --name-status

# Limit diff to specific directories
git diff arch/i386 include/asm-i386

Advanced options

# Find renames, copies and complete rewrites (expensive)
git diff --find-copies-harder -B -C

# Output diff in reverse
git diff -R