Imagine you have been working on a Java project for several hours. You have modified multiple files, fixed a few bugs, and added a new feature.
Before creating a commit, you ask yourself:
What exactly changed?
Which lines of code were added?
Which lines were removed?
Did I accidentally change something I shouldn't have?
What am I about to commit?
Instead of opening every file manually and comparing the changes, Git provides a powerful command:
git diff
The git diff command compares different versions of your files and shows exactly what has changed.
It is one of the most useful commands in Git because it allows you to review your work before creating a commit.
Professional developers use git diff regularly to verify their changes and avoid accidental mistakes.
In this article, you will learn:
What
git diffisHow Git compares files
Different types of
git diffUnderstanding the output
Viewing staged and unstaged changes
Comparing commits
Comparing branches
Common mistakes
Best practices
By the end of this article, you will be able to confidently inspect every change in your repository.
What is git diff?
The git diff command compares two versions of files and displays the differences between them.
Basic syntax:
git diff
It does not change your files.
It only shows the differences.
Think of it as a comparison tool.
Why is git diff Important?
Suppose you modified a file yesterday.
Today you don't remember what you changed.
Instead of searching manually, simply run:
git diff
Git immediately shows:
Added lines
Removed lines
Modified lines
This helps you verify your work before creating a commit.
Understanding Comparison
Suppose your original code was:
System.out.println("Hello");
Now you modify it to:
System.out.println("Hello Git");
Git compares these two versions.
The difference is:
Old line removed
New line added
This comparison is called a diff.
How Git Compares Files
Git compares:
Old Version
│
▼
Current Version
It identifies:
Added lines
Deleted lines
Modified lines
Git does not compare screenshots or compiled programs.
It compares the actual text content.
First Example
Suppose your file contains:
public class Main
{
public static void main(String args[])
{
System.out.println("Hello");
}
}
Save and commit the file.
Now change:
System.out.println("Hello");
to
System.out.println("Hello Git");
Run:
git diff
Git may display:
diff --git a/Main.java b/Main.java
index 8c9f123..5a2b891 100644
--- a/Main.java
+++ b/Main.java
@@ -4,5 +4,5 @@
- System.out.println("Hello");
+ System.out.println("Hello Git");
At first, this output may look confusing.
Let us understand it line by line.
Understanding diff --git
Example:
diff --git a/Main.java b/Main.java
This tells Git that it is comparing two versions of the same file.
The prefixes:
a/
and
b/
represent:
Old version
New version
respectively.
Understanding the --- Line
Example:
--- a/Main.java
This represents the original version of the file.
Understanding the +++ Line
Example:
+++ b/Main.java
This represents the modified version.
Understanding the @@ Line
Example:
@@ -4,5 +4,5 @@
This line tells Git where the changes occurred.
For beginners, you do not need to memorize this syntax.
Simply remember:
It tells Git which section of the file is being compared.
Understanding Removed Lines
Lines beginning with:
-
represent removed lines.
Example:
- System.out.println("Hello");
This line existed before but no longer exists.
Understanding Added Lines
Lines beginning with:
+
represent newly added lines.
Example:
+ System.out.println("Hello Git");
This line is part of the new version.
Visual Representation
Original File
│
▼
System.out.println("Hello");
│
▼
Modified File
System.out.println("Hello Git");
Git highlights:
- Hello
+ Hello Git
git diff Shows Unstaged Changes
The basic command:
git diff
compares:
Working Directory
│
▼
Staging Area
If you modify a file but have not staged it, Git displays those changes.
Example:
Committed
│
Modify File
▼
Working Directory
Run:
git diff
Git displays the modifications.
What Happens After git add?
Suppose you run:
git add Main.java
Now run:
git diff
Git may display:
(no output)
Why?
Because there are no unstaged changes.
The latest version is already in the Staging Area.
Viewing Staged Changes
To compare the Staging Area with the latest commit, use:
git diff --staged
or
git diff --cached
Git now displays the staged changes that will be included in the next commit.
Understanding the Three Comparisons
Git can compare three important areas.
Working Directory vs Staging Area
git diff
Shows unstaged changes.
Staging Area vs Repository
git diff --staged
Shows staged changes.
Commit vs Commit
git diff <commit1> <commit2>
Compares two commits.
Visual Diagram
Working Directory
│
│ git diff
▼
Staging Area
│
│ git diff --staged
▼
Repository
Comparing Two Commits
Suppose your history is:
Commit A
↓
Commit B
↓
Commit C
You can compare:
Commit A and Commit C
using:
git diff <commitA> <commitC>
Git displays every change introduced between those commits.
Comparing Two Branches
Suppose you have:
main
feature-login
Compare them:
git diff main feature-login
Git shows the differences between both branches.
This becomes extremely useful during team development.
Comparing One File
Suppose you only want to inspect:
Main.java
Run:
git diff Main.java
Git displays changes only for that file.
Comparing Multiple Files
git diff Main.java Student.java
Git compares both files.
Viewing Word-Level Differences
Git can highlight changes at the word level.
Example:
git diff --word-diff
Instead of entire lines, Git highlights individual changed words.
This is useful for documentation and text files.
Real-World Example
Suppose you are building a Library Management System.
You modify:
Login.java
Book.java
README.md
Before committing, run:
git diff
Git shows:
Login validation updated.
Book search improved.
README installation instructions changed.
You notice an accidental change inside Book.java.
Instead of committing immediately, you correct the mistake.
This is why professional developers always review their changes first.
Why Use git diff Before Every Commit?
Without checking differences, you might accidentally commit:
Debug code
Temporary print statements
Passwords
API keys
Unfinished features
Formatting mistakes
Running:
git diff
takes only a few seconds and helps prevent these problems.
Common Beginner Mistakes
Mistake 1
Thinking git diff changes files.
It doesn't.
It only displays differences.
Mistake 2
Running git diff after staging files and expecting output.
Remember:
git diff
shows only unstaged changes.
Use:
git diff --staged
for staged changes.
Mistake 3
Ignoring the diff before committing.
Always review your changes.
Mistake 4
Trying to memorize every symbol.
Focus first on:
+means added.-means removed.
The remaining details become easier with practice.
Best Practices
Run
git diffbefore staging changes.Run
git diff --stagedbefore committing.Review every modified file.
Never commit code you haven't reviewed.
Use file-specific comparisons when working on large projects.
Make reviewing changes a habit.
Useful git diff Commands
| Command | Purpose |
|---|---|
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
git diff --cached | Same as git diff --staged |
git diff Main.java | Compare one file |
git diff file1 file2 | Compare multiple files |
git diff commit1 commit2 | Compare two commits |
git diff main feature | Compare two branches |
git diff --word-diff | Show word-level differences |
Summary
The git diff command is one of Git's most valuable tools for reviewing changes before they become part of your project's history.
It compares different versions of files and highlights additions, deletions, and modifications. Whether you are reviewing unstaged changes, staged changes, commits, or branches, git diff helps you understand exactly what has changed.
Using git diff regularly improves code quality, prevents accidental commits, and helps maintain a clean and reliable Git history.
Comments
Post a Comment