Git Notes
Master the git diff command to compare changes between commits, branches, and staging areas. Learn diff output format, comparison options, and practical examples.
The git diff command shows the differences between various states of your repository. Whether you want to see what you have changed before committing, compare two branches, or review specific file modifications, git diff is your go-to tool.
Understanding Git Diff Output
git diffdiff --git a/index.html b/index.html index 3b18e51..f3c17f8 100644 --- a/index.html +++ b/index.html @@ -1,6 +1,7 @@ <!DOCTYPE html> <html> <head> - <title>Old Title</title> + <title>New Title</title> + <meta name="description" content="Updated page"> </head> <body>
Reading the Diff Output
| diff --git a/file b/file | Files being compared |
| index 3b18e51..f3c17f8 | SHA hashes of the two versions |
| --- a/index.html | Original file (before changes) |
| +++ b/index.html | Modified file (after changes) |
| @@ -1,6 +1,7 @@ | Location: starting at line 1, showing 6/7 lines |
| (space) unchanged line | Context lines (no change) |
| -removed line | Lines removed (red in terminal) |
| +added line | Lines added (green in terminal) |
Types of Diff Comparisons
Common Git Diff Commands
Compare Working Directory vs Staging Area
Shows unstaged changes (modifications not yet added with git add):
git diffCompare Staging Area vs Last Commit
Shows staged changes (what will be included in the next commit):
git diff --staged
# or the older alias
git diff --cacheddiff --git a/app.js b/app.js
index a2b3c4d..e5f6g7h 100644
--- a/app.js
+++ b/app.js
@@ -10,4 +10,8 @@ function greet(name) {
return `Hello, ${name}!`;
}
+function farewell(name) {
+ return `Goodbye, ${name}!`;
+}
+
module.exports = { greet };Compare Working Directory vs Last Commit
Shows all changes since the last commit (both staged and unstaged):
git diff HEADCompare Specific File
git diff path/to/file.js
git diff --staged path/to/file.jsCompare Two Commits
git diff abc1234 def5678Compare Two Branches
git diff main develop
git diff main..developdiff --git a/src/feature.js b/src/feature.js
new file mode 100644
index 0000000..abc1234
--- /dev/null
+++ b/src/feature.js
@@ -0,0 +1,15 @@
+// New feature added in develop branch
+class Feature {
+ constructor() {
+ this.enabled = true;
+ }
+
+ execute() {
+ console.log('Feature executed');
+ }
+}Compare with Remote Branch
git diff origin/main
git diff HEAD..origin/mainUseful Diff Options
Summary Only (No Content)
git diff --statsrc/app.js | 12 ++++++------ src/utils.js | 5 +++++ tests/app.test.js | 23 +++++++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-)
Show Only File Names
git diff --name-onlysrc/app.js src/utils.js tests/app.test.js
Show Names with Status
git diff --name-statusM src/app.js A src/utils.js A tests/app.test.js
Status codes: M (modified), A (added), D (deleted), R (renamed), C (copied)
Word-Level Diff
git diff --word-diffThe quick [-brown-]{+red+} fox jumps over the [-lazy-]{+sleeping+} dog.Ignore Whitespace
git diff -w
# or
git diff --ignore-all-spaceLimit Context Lines
# Show only 1 line of context instead of default 3
git diff -U1Color-Words Mode
git diff --color-wordsComparing Across Time
Changes in Last N Commits
# What changed in the last commit
git diff HEAD~1 HEAD
# What changed in the last 3 commits
git diff HEAD~3 HEADChanges Between Tags
git diff v1.0 v2.0Changes Since a Specific Date
git diff HEAD@{'2024-01-01'}
git diff HEAD@{'2 weeks ago'}External Diff Tools
Configure a Visual Diff Tool
# Set VS Code as diff tool
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
# Use the diff tool
git difftoolPopular Diff Tools
| Tool | Command | Platform |
|---|---|---|
| VS Code | code --diff | Cross-platform |
| Meld | meld | Linux/Windows |
| Beyond Compare | bcomp | Cross-platform |
| KDiff3 | kdiff3 | Cross-platform |
| FileMerge | opendiff | macOS |
Practical Examples
Check What You Are About to Commit
# Stage your changes
git add .
# Review what will be committed
git diff --staged
# If everything looks good, commit
git commit -m "Add new feature"Find Which Files Changed Between Releases
git diff --stat v1.0..v2.0README.md | 15 ++++++++++++--- package.json | 4 ++-- src/api/routes.js | 45 +++++++++++++++++++++++++++++++++++++++++++ src/models/user.js | 12 ++++++++++++ 4 files changed, 71 insertions(+), 5 deletions(-)
Generate a Patch File
# Create a patch from diff
git diff > my-changes.patch
# Apply the patch elsewhere
git apply my-changes.patchInterview Questions
- What is the difference between
git diffandgit diff --staged?
git diff shows changes in the working directory that have not been staged. git diff --staged shows changes that have been staged (added) but not yet committed.
- How do you compare changes between two branches?
Use git diff branch1 branch2 or git diff branch1..branch2. This shows all differences between the tips of the two branches.
- What does
git diff HEADshow?
It shows all changes in the working directory compared to the last commit, including both staged and unstaged changes.
- How can you see only the names of files that changed?
Use git diff --name-only for just filenames, or git diff --name-status to also see whether each file was modified, added, or deleted.
- How do you ignore whitespace changes in a diff?
Use git diff -w or git diff --ignore-all-space to ignore all whitespace differences when comparing files.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Diff - Compare Changes in Your Repository.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, basics, diff, git diff - compare changes in your repository
Related Git & GitHub Topics