When working with Git, one of the most common questions we ask before committing is:
What exactly changed in my files?
Git provides a powerful command called git diff that helps us inspect changes between different states of our repository.
Using git diff, we can see exactly which lines were added, removed, or modified. This helps us review our work before committing changes.
In this post, we will understand how git diff works and practice using it.
Before We Begin
Run the commands in this guide inside a terminal and within a Git repository folder.
- Windows: Open Git Bash, PowerShell, or Windows Terminal
- macOS / Linux: Open the Terminal
1. What Does git diff Show?
git diff compares two versions of files and shows the differences between them.
In most cases, it compares:
Working Directory vs Staging Area
Meaning:
It shows changes we made but have not yet staged.
Example:
git diff
This displays the modifications in our files.
2. Setting Up a Practice Repository
Let’s create a small repository to experiment safely.
mkdir diff-demo
cd diff-demo
git init
Create a file:
echo "Line 1" > notes.txt
Add and commit it:
git add notes.txt
git commit -m "Initial commit"
Now our repository contains one committed file.
3. Viewing Changes Before Staging
Modify the file:
echo "Line 2" >> notes.txt
Check the status:
git status
Git will show that notes.txt has been modified.
Now run:
git diff
You will see output similar to:
Line 1
+Line 2
Explanation:
- Lines starting with
+were added - Lines starting with
-were removed
This helps us review changes before committing.
4. Viewing Changes After Staging
Now stage the file:
git add notes.txt
If you run:
git diff
You will see no output.
Why?
Because git diff compares the working directory and staging area, and they now match.
To view staged changes, run:
git diff --staged
This shows the differences between:
Staging Area vs Last Commit
5. Viewing Changes Between Commits
We can also compare commits.
First, make another change:
echo "Line 3" >> notes.txt
git add notes.txt
git commit -m "Add line 3"
Now view commit history:
git log --oneline
Then compare two commits:
git diff <commit-id-1> <commit-id-2>
This shows the differences between those commits.
6. Viewing Changes for a Specific File
Sometimes we want to inspect changes for a single file.
Example:
git diff notes.txt
This limits the diff output to that file.
7. Understanding the git diff Output
A typical git diff output contains several sections.
Example:
diff --git a/notes.txt b/notes.txt
index e69de29..5b8256f 100644
--- a/notes.txt
+++ b/notes.txt
@@
Line 1
+Line 2
Important parts:
---indicates the old version+++indicates the new version+lines show additions-lines show deletions
8. Quick Command Reference
View unstaged changes:
git diff
View staged changes:
git diff --staged
View changes for a specific file:
git diff filename
Compare two commits:
git diff commit1 commit2
9. Why git diff Is Important
Before committing code, it’s always a good habit to review what changed.
git diff helps developers:
- review modifications before committing
- detect accidental changes
- debug issues by comparing versions
- understand exactly what was modified
For many developers, git diff is one of the most frequently used Git commands.
Summary
git diff shows the differences between versions of files in a Git repository.
It helps us understand:
- what changes we made
- what changes are staged for commit
- how files differ between commits
Using git diff regularly helps us commit cleaner, more accurate changes.