Skip to main content

Git Diff

Updated 3 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/git-diff

In short

Git diff is a Git command that shows the exact line-by-line differences between two versions of your code, such as working files, staged changes, or commits.

What is git diff?

git diff answers the question 'what exactly changed?'. It compares two versions of your files and prints only the lines that differ: lines starting with - were removed, lines starting with + were added, and a few unchanged lines around each change, called context, show where it happened. This format is called a unified diff, and it is the same view you see when reviewing a pull request.

What gets compared depends on the arguments. Plain git diff shows edits in your working directory that you haven't staged yet, git diff --staged shows what the next commit will contain, and git diff main feature compares the latest commits of two branches. You can also compare any two commits by hash, add a file path to limit the output to one file, or use --stat for a short summary of how many lines changed in each file.

A diff is like the track-changes view in a word processor, which shows deletions and insertions instead of the whole document. Developers run git diff before committing to double-check their work, code review tools display diffs so reviewers can focus on what's new, and the output can be saved as a patch file and applied elsewhere with git apply.

git diff is often confused with git status and git log. Status only lists which files have changed, log lists commits, and diff shows the actual lines inside the changes. A common surprise is that plain git diff shows nothing after git add: the changes are now staged, so you need git diff --staged to see them, and brand-new untracked files never appear in plain git diff at all.

Key takeaways

  • git diff shows line-by-line changes, with - for removed lines and + for added lines.
  • Plain git diff shows unstaged changes; git diff --staged shows what will be committed.
  • You can compare branches, commits, or single files, and --stat summarizes each file's changes.
  • git diff main...feature shows what a branch changed since it split off, as a pull request does.
  • Diffs are the basis of code review and of patch files that can be applied elsewhere.

Example

Comparing changes with git diffbash
# Unstaged changes in your working directory
git diff

# Staged changes: what the next commit will contain
git diff --staged

# What a feature branch changed since it split from main
git diff main...feature/search --stat

# Sample output for one changed line:
# @@ -12,2 +12,2 @@ function total(items) {
#    const sum = items.reduce((a, b) => a + b, 0);
# -  return sum;
# +  return sum + tax;

Readers ask

How do I see staged changes in Git?

Run git diff --staged, or its older synonym git diff --cached. Plain git diff only shows changes that haven't been staged yet.

How do I compare two branches in Git?

Use git diff branch-a branch-b to compare their latest commits directly, or git diff branch-a...branch-b to see only what branch-b changed since it split off from branch-a. Add --stat for a per-file summary or --name-only to list just the file names.

What do the plus and minus signs mean in a diff?

Lines starting with - exist in the old version but not the new one, and lines starting with + exist only in the new version. A modified line appears as a removal followed by an addition, and lines starting with @@ mark where each block of changes begins.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings