Skip to content

diff Git Command Guide

The git diff command is used to 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.

Terminal window
git diff [<options>] [<commit>] [--] [<path>...]
OptionDescription
—cached, —stagedCompare staged changes with commit
—merge-baseCompare using merge-base of given commits
-p, —patchGenerate patch output (default)
-s, —no-patchSuppress diff output
—statShow diff statistics
—rawGenerate raw diff format
-U, —unified=Generate diffs with context
—colorShow colored diff
—exit-codeExit with status based on changes
—name-onlyShow only changed file names
—name-statusShow file names and status
—no-indexCompare files outside working tree
—ignore-cr-at-eolIgnore carriage return at EOL
ParameterDescription
[]Commit/tree to compare with (default: HEAD)
[…]Optional paths to limit comparison
Terminal window
git diff

Shows changes in the working directory that aren’t staged for commit.

Terminal window
git diff --staged

Shows changes that are staged for the next commit.

Terminal window
git diff HEAD

Shows all changes in the working tree compared to the last commit.

Terminal window
git diff HEAD~1 HEAD

Shows changes between HEAD~1 and HEAD commits.

Terminal window
git diff --merge-base main

Shows changes relative to the merge-base between current branch and main.

Terminal window
git diff --no-index file1.txt file2.txt

Compare two files not in the working tree.

How do I see what I’ve changed but not staged?

Section titled “How do I see what I’ve changed but not staged?”

To see unstaged changes, run:

Terminal window
git diff

How can I see what is staged for the next commit?

Section titled “How can I see what is staged for the next commit?”

To see staged changes, use:

Terminal window
git diff --staged

How do I compare my working tree with a specific commit?

Section titled “How do I compare my working tree with a specific commit?”

To compare working tree with a commit, execute:

Terminal window
git diff <commit>

How can I see only the names of changed files?

Section titled “How can I see only the names of changed files?”

To show only changed file names, use:

Terminal window
git diff --name-only

How do I compare files outside the working tree?

Section titled “How do I compare files outside the working tree?”

To compare files not managed by Git, use:

Terminal window
git diff --no-index <file1> <file2>

How can I get a summary of changes without showing the diffs?

Section titled “How can I get a summary of changes without showing the diffs?”

To get a summary, use:

Terminal window
git diff --stat
  1. Reviewing changes before committing
  2. Analyzing differences between any two commits
  3. Debugging code changes and regressions
  4. Creating patches for code review
  5. Comparing files outside Git repositories