Compare one file between two Git commits

Compare a file across Git commits, tags, or branches, inspect word-level changes, and handle renamed paths and three-dot diffs.

Best practices
Illustration of Git branches and version history

Use git diff A B -- path/to/file to compare one file at two commits. Replace A and B with commit IDs, tags, branches, or relative references such as HEAD~3. The path after -- limits the comparison to that file.

Compare two versions

Find the relevant commits, then compare their snapshots:

git log --oneline -- src/app.js
git diff HEAD~3 HEAD -- src/app.js

# Two-dot syntax compares the same endpoints.
git diff HEAD~3..HEAD -- src/app.js

HEAD~3 follows the first parent three times; the repository must have that much history. These commands display changes from the older endpoint to the newer one. They do not change the working tree.

Choose the output you need

# Summary of changed lines
git diff --stat HEAD~3 HEAD -- src/app.js

# Show changes within lines
git diff --word-diff HEAD~3 HEAD -- src/app.js

# Use your configured external diff tool
git difftool HEAD~3 HEAD -- src/app.js

A configured difftool may open a graphical tool; the exact interface depends on your local setup.

Compare a commit with your working copy

With only one commit argument, Git compares that snapshot with the current working file:

git diff HEAD~3 -- src/app.js

To inspect the patch introduced by a particular ordinary commit, use show:

git show HEAD~3 -- src/app.js

To read the complete file at that commit instead of its patch, use:

git show HEAD~3:src/app.js

FAQ

Does argument order matter?

Yes. In git diff A B, removed lines belong to A and added lines belong to B. Reversing the arguments reverses the comparison.

What if the file was renamed?

Use git log --follow -- src/new-name.js to trace a single file's history across renames. A diff restricted to the new path can hide the old path needed for rename detection. For a direct content comparison across different names, specify both blobs:

git diff 'v1.0:src/old-name.js' 'v2.0:src/new-name.js'

Replace the tags and paths with versions that exist in your repository.

How is three-dot syntax different?

git diff A...B -- path compares the merge base of A and B with B, which is useful for reviewing changes introduced on B. git diff A..B -- path compares A directly with B. Neither diff form means “print every commit in this range.”

See git-diff, git-log, and git-show.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting