0
0
Gitdevops~3 mins

git diff --staged for staged changes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to see what changes you have prepared to save before actually saving them. The command git diff --staged shows the differences between the last saved version and what you have marked to save next.
When you want to review the exact changes you have marked to save before making the save permanent.
When you accidentally staged some files and want to check what changes are included.
When you want to compare your staged changes with the last saved version to ensure correctness.
When you are preparing a save and want to double-check what will be included.
When you want to see the difference between your staged files and the last commit.
Commands
This command marks the file example.txt to be saved in the next commit. It moves changes from unstaged to staged.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
This command shows the differences between the last saved version and the files you have staged to save next. It helps you review staged changes.
Terminal
git diff --staged
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, this is a new line. +Another added line.
--staged - Shows differences for staged files compared to the last commit.
Key Concept

If you remember nothing else from this pattern, remember: git diff --staged shows exactly what changes are ready to be saved next.

Common Mistakes
Running git diff without --staged to see staged changes.
git diff without --staged only shows unstaged changes, so you miss what is staged.
Use git diff --staged to see changes that are staged and ready to be saved.
Expecting git diff --staged to show unstaged changes.
It only shows staged changes, so unstaged changes are not visible.
Use git diff alone to see unstaged changes.
Summary
Use git add to stage changes you want to save.
Use git diff --staged to review what staged changes will be saved.
This helps avoid mistakes by confirming staged content before committing.