Git Diff
What Will We Learn?
Learn how to read and understand git diff output and where to use it.
- Explore useful options for comparing specific files and summarizing changes.
- Learn common usage patterns.
- See how word-level differences make small code changes easier to spot.
What git diff Shows?
git diff is your go-to tool for visualizing differences (generally of files) between commits, branches, index, etc. Generally, we use it to show the difference between:
- Working Directory Vs Stagging Area
- Stagging Area Vs Latest Commit
- Commit Vs Commit/Branch/Tag
- OR any combination above
Important
By default, git diff shows the difference between the Working Dir vs Stagging Area if changes are stagged. Else it shows the difference between Working Dir vs Latest Commit.
Understanding git diff Output
The git diff command shows the differences between two versions of a file. It is one of the most useful Git commands for reviewing changes before committing them.
Consider the following example:
diff --git a/main.c b/main.c
index 5d0d8e6..c3b32d7 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
#include<stdio.h>
-void main(){
+int main(){
printf("Hello World!\n");
}Here is the breakdown of the Output:
1. File Comparison
diff --git a/main.c b/main.c- Indicates that Git is comparing two versions of the file
main.c. a/main.crefers to the old version.b/main.crefers to the new version.
2. Index Line
index 5d0d8e6..c3b32d7 100644This line contains metadata about the file.
5d0d8e6is the hash of the old file.c3b32d7is the hash of the new file.100644is the file mode (normal non-executable file).
3. Old File
--- a/main.c- The
---line represents the original (old) version of the file.
4. New File
+++ b/main.c- The
+++line represents the updated (new) version of the file.
5. Hunk Header
@@ -1,5 +1,5 @@This line tells Git where the changes occurred.
The format is:
@@ -<old_start>,<old_lines> +<new_start>,<new_lines> @@For this example:
-1,5means the old file starts at line 1 and contains 5 lines.+1,5means the new file starts at line 1 and also contains 5 lines.
6. Context Lines
#include<stdio.h>Lines without any prefix are unchanged. They are shown to provide context around the modifications.
7. Removed Line
-void main(){A line beginning with - indicates that it was removed from the old version.
8. Added Line
+int main(){A line beginning with + indicates that it was added in the new version.
In this example, the function declaration was changed from:
void main()to
int main()9. Unchanged Lines
printf("Hello World!\n");
}These lines remain unchanged and are included to help understand where the modification occurred.
Symbols Used in git diff
| Symbol | Meaning |
|---|---|
diff --git | Indicates the files being compared |
index | Shows object hashes and file mode |
--- | Original file |
+++ | Updated file |
@@ | Hunk header showing line ranges |
- | Removed line |
+ | Added line |
| (space) | Unchanged context line |
Why git diff Is Useful
git diff helps developers:
- Review changes before committing
- Identify accidental modifications
- Compare working directory changes with the staging area
- Compare commits or branches
- Understand exactly what code has been added, removed, or modified
Common git diff Commands
Compare Working Directory with Staging Area
git diffShows changes that have not been staged.
Compare Staged Changes
git diff --stagedShows changes that are staged for the next commit.
Compare Two Commits
git diff commit1 commit2Example:
git diff HEAD~1 HEADCompare Two Branches
git diff main feature-loginShows differences between the main and feature-login branches.
Comparing Specific Files
Use the -- <file> option to view changes made to a specific file instead of the entire repository.
git diff -- main.cThis command shows only the differences in main.c.
You can also compare a specific file between commits:
git diff HEAD~1 HEAD -- main.cViewing Summary
The --stat option displays a summary of the changes instead of the full diff.
git diff --statExample output:
main.c | 2 +-
helper.c | 5 +++--
2 files changed, 4 insertions(+), 3 deletions(-)This summary includes:
- Files that were modified
- Number of insertions (
+) - Number of deletions (
-)
It’s useful for getting a quick overview of changes.
Word-Level Changes
Instead of showing entire changed lines, the --word-diff option highlights changes at the word level.
git diff --word-diffExample:
[-void-]{+int+} main() {
This makes it easier to identify small edits within a line, such as renamed variables, modified function signatures, or text changes.
Summary
The git diff output consists of:
- The files being compared.
- Metadata (hashes and file mode).
- The old and new file names.
- A hunk header showing where changes occurred.
- Context lines that remain unchanged.
- Removed lines marked with
-. - Added lines marked with
+.
Understanding these components makes it easier to review code changes, debug issues, and collaborate effectively using Git.