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 diffshows line-by-line changes, with-for removed lines and+for added lines.- Plain
git diffshows unstaged changes;git diff --stagedshows what will be committed. - You can compare branches, commits, or single files, and
--statsummarizes each file's changes. git diff main...featureshows 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
# 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
- Staging AreaVersion Control, p. 37The staging area in Git is a holding zone between your files and the next commit, where you place exactly the changes you want that commit to include.
- CommitVersion Control, p. 5A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
- Code ReviewVersion Control, p. 4A code review is the practice of having other developers check code changes before they are merged, to catch bugs, improve quality, and share knowledge.
- Pull RequestVersion Control, p. 32A pull request is a proposal to merge changes from one branch into another, giving teammates a place to review, discuss, and test the code before it is merged.
- BranchVersion Control, p. 2A branch in Git is an independent line of development that lets you work on a feature or fix in isolation, without affecting the main code until you merge it.
- Merge ConflictVersion Control, p. 30A merge conflict happens when Git can't automatically combine two branches because both changed the same lines of a file, so a person must decide the result.
Spotted a mistake or something missing on this page?Suggest an edit