Complete Git Diff Guide

Master Comparing Changes in Git

Git diff is one of the most powerful tools for understanding changes in your codebase. This comprehensive guide covers all essential git diff commands with practical examples.

Basic Comparisons

git diff Unstaged Changes

Shows changes in your working directory that haven't been staged yet.

(Working Directory vs. Index)

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git
git diff --staged Staged Changes

Shows changes that have been staged but not committed.

(Index vs. HEAD)

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git
git diff --cached Staged Changes

Shows changes that have been staged but not committed (same as --staged).

(Index vs. HEAD)

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git
git diff HEAD All Uncommitted Changes

Shows both staged and unstaged changes (all modifications since last commit).

(Working Directory vs. Last Commit)

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git diff --git a/app.js b/app.js index 111222..333444 --- a/app.js +++ b/app.js @@ -5,6 +5,6 @@ - console.log("Old code"); + console.log("New code");

File & Directory Comparisons

git diff file.txt Specific Files

Shows changes in a specific file only.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git
git diff src/ Directory Comparison

Shows changes in all files within a specific directory.

diff --git a/src/app.js b/src/app.js index 111222..333444 --- a/src/app.js +++ b/src/app.js @@ -5,6 +5,6 @@ - console.log("Old code"); + console.log("New code"); diff --git a/src/utils.js b/src/utils.js index abcdef..123456 --- a/src/utils.js +++ b/src/utils.js @@ -1,3 +1,3 @@ - function oldUtil() {} + function newUtil() {}
git diff --name-only Show Only Filenames

Shows only the names of files with changes, not the actual diff content.

file.txt src/app.js README.md
git diff --name-status Filename + Status

Shows filenames with change type (A=added, M=modified, D=deleted).

M file.txt A newfile.js D oldfile.txt
git diff --stat Summary of Changes

Shows a concise summary of changes instead of full diff.

file.txt | 2 +- app.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)

Commit & Branch Comparisons

git diff commit1 commit2 Compare Two Commits

Shows differences between any two commits in your history.

diff --git a/file.txt b/file.txt index abc123..xyz789 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git
git diff HEAD~3 HEAD Compare Last N Commits

Shows changes made in the last 3 commits (replace 3 with any number).

diff --git a/file1.txt b/file1.txt index abc123..def456 --- a/file1.txt +++ b/file1.txt @@ -1 +1 @@ -Version 1 +Version 2 diff --git a/file2.txt b/file2.txt index 111222..333444 --- a/file2.txt +++ b/file2.txt @@ -1,3 +1,3 @@ -Old content +New content
git diff main..feature Compare Two Branches

Shows changes in feature branch that aren't in main branch yet.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello from main +Hello from feature
git diff main feature Compare Two Branches

Shows changes in feature branch that aren't in main branch.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello from main +Hello from feature
git diff origin/main main Local vs Remote

Compare local main branch with remote main branch.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello from remote +Hello from local
git diff --merge-base feature 3-Way Diff for Merge

Shows changes in your current branch compared to the merge base with another branch.

diff --cc file.txt index abc123,def456..000000 --- a/file.txt +++ b/file.txt @@@ -1,1 -1,1 +1,1 @@@ -Hello from main -Hello from feature +Hello merged
git diff --word-diff Word-Level Changes

Shows word-level differences instead of line-level.

Hello [-World-]{+Git+}
git diff -w Ignore Whitespace

Shows differences while ignoring whitespace changes.

(No output if only whitespace differences exist)

Advanced Comparisons

git diff commit1 commit2 -- package.json Specific File in Commits

Compare changes to a specific file between two commits.

diff --git a/package.json b/package.json index 123..456 --- a/package.json +++ b/package.json @@ -5,6 +5,6 @@ - "version": "1.0.0", + "version": "2.0.0",
git diff @{1.day.ago} @{now} Timeline Diffs

Compare changes between two points in time.

(Shows all changes made in the last 24 hours)
git diff ":!*.min.js" ":!*.css" Ignore Certain Files

Shows differences while excluding minified JS and CSS files.

diff --git a/app.js b/app.js index 111222..333444 --- a/app.js +++ b/app.js @@ -5,6 +5,6 @@ - console.log("Old code"); + console.log("New code"); (No min.js or .css files shown)
git fetch origin && git diff origin/main main Updated Remote Comparison

First update remote references, then compare local with remote.

$ git fetch origin remote: Counting objects: 5, done. remote: Compressing objects: 100% (3/3), done. remote: Total 5 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (5/5), done. From https://github.com/user/repo abc123..def456 main -> origin/main diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello from remote +Hello from local
git diff --name-status main..feature Branch Comparison with Status

Shows filenames and change types between two branches.

M file.txt A newfile.js D oldfile.txt R100 oldname.txt newname.txt
git diff --stat origin/main Pre-push Checks

Quickly see what you're about to push to remote.

README.md | 4 +++- src/index.js | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-)

Table of Contents

Basic Comparisons

git diff Unstaged Changes

Shows changes in your working directory that haven't been staged yet.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git
git diff --staged Staged Changes

Shows changes that have been staged but not committed.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git
git diff HEAD All Uncommitted Changes

Shows both staged and unstaged changes since last commit.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git

File & Directory Comparisons

git diff -- file.txt Specific File

Shows changes to a specific file only.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git
git diff --name-only Only Filenames

Shows only names of files with changes.

file.txt src/app.js README.md
git diff --name-status Filename + Status

Shows filenames with change type (A=added, M=modified, D=deleted).

M file.txt A newfile.js D oldfile.txt
git diff --stat Summary of Changes

Shows a concise summary of changes instead of full diff.

file.txt | 2 +- app.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)

Commit Comparisons

git diff commit1 commit2 Between Two Commits

Shows differences between any two commits.

diff --git a/file.txt b/file.txt index abc123..xyz789 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello World +Hello Git
git diff HEAD~3 HEAD Last N Commits

Shows changes made in the last 3 commits.

diff --git a/file1.txt b/file1.txt index abc123..def456 --- a/file1.txt +++ b/file1.txt @@ -1 +1 @@ -Version 1 +Version 2
git diff @{yesterday} Since Yesterday

Shows changes since yesterday (relative date reference).

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Old content +New content

Branch Comparisons

git diff main..feature Between Branches

Shows changes in feature branch not yet in main.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello from main +Hello from feature
git diff origin/main main Local vs Remote

Compare local main branch with remote main branch.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Hello from remote +Hello from local
git diff --merge-base feature 3-Way Diff

Shows changes compared to the merge base with another branch.

diff --cc file.txt index abc123,def456..000000 --- a/file.txt +++ b/file.txt @@@ -1,1 -1,1 +1,1 @@@ -Hello from main +Hello merged

Advanced Techniques

git diff --word-diff Word-Level Diff

Shows word-level differences instead of line-level.

Hello [-World-]{+Git+}
git diff -w Ignore Whitespace

Ignores whitespace changes when comparing.

(No output if only whitespace differences)
git diff --find-renames Detect Renames

Detects renamed files and shows them appropriately.

diff --git a/old_name.txt b/new_name.txt similarity index 100% rename from old_name.txt rename to new_name.txt
git diff --color-words Color Word Diffs

Shows word-level differences with color highlighting.

Hello World Git

Workflow Integration

git diff --cached --stat Pre-commit Check

Quick overview of what you're about to commit.

file.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
git diff --stat origin/main Pre-push Check

See what you're about to push to remote.

README.md | 4 +++- src/index.js | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-)
git diff @{1.day.ago} @{now} Daily Standup

See what changed in the last 24 hours.

diff --git a/file.txt b/file.txt index abc123..def456 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -Yesterday's content +Today's content
Pro Tip

Combine multiple flags for powerful workflows. For example: git diff --name-status --stat origin/main will show both detailed changes and a summary.