0
0
Gitdevops~5 mins

Why diffing matters in Git - Why It Works

Choose your learning style9 modes available
Introduction
When you work on code or files, you often change things. Diffing helps you see exactly what changed between two versions. This makes it easier to find mistakes and understand updates.
When you want to check what changes you made before saving or sharing your work
When you need to review changes made by a teammate before merging them
When you want to find out why a bug appeared by comparing current and previous versions
When you want to confirm that your fixes or updates are correctly applied
When you want to track progress by seeing what was added or removed over time
Commands
This command shows the changes you made in your working directory compared to the last saved version (commit). It helps you see what is new, changed, or deleted before you save.
Terminal
git diff
Expected OutputExpected
diff --git a/example.txt b/example.txt index e69de29..d95f3ad 100644 --- a/example.txt +++ b/example.txt @@ -0,0 +1,2 @@ +Hello world +This is a new line
--staged - Show changes that are staged for the next commit
--name-only - Show only the names of changed files
This command compares the last commit with the one before it. It shows what changed between these two saved versions.
Terminal
git diff HEAD~1 HEAD
Expected OutputExpected
diff --git a/example.txt b/example.txt index e69de29..d95f3ad 100644 --- a/example.txt +++ b/example.txt @@ -0,0 +1,2 @@ +Hello world +This is a new line
This shows the changes you have marked to be saved (staged) but not yet committed. It helps you review what will be included in your next save.
Terminal
git diff --cached
Expected OutputExpected
diff --git a/example.txt b/example.txt index e69de29..d95f3ad 100644 --- a/example.txt +++ b/example.txt @@ -0,0 +1,2 @@ +Hello world +This is a new line
Key Concept

If you remember nothing else from this pattern, remember: diffing shows exactly what changed so you can understand and control your work.

Common Mistakes
Running git diff after committing changes
git diff by default shows changes not yet saved; after commit, it shows nothing new
Use git diff HEAD~1 HEAD to compare the last two commits instead
Not staging changes before using git diff --cached
git diff --cached shows only staged changes; if nothing is staged, it shows nothing
Use git add to stage changes before running git diff --cached
Confusing git diff output with full file content
git diff shows only lines added or removed, not the whole file, which can confuse beginners
Focus on lines starting with + (added) or - (removed) to understand changes
Summary
git diff shows changes in your working files compared to the last saved version.
git diff HEAD~1 HEAD compares the last two commits to see what changed between them.
git diff --cached shows changes staged for the next commit, helping review before saving.