How to Use git diff staged to See Changes Ready to Commit
Use
git diff --staged or git diff --cached to see the differences between the staged files and the last commit. This shows what changes are ready to be committed.Syntax
The command git diff --staged compares the staged changes with the last commit. You can also use git diff --cached as an alias. This helps you review what you have added to the staging area before committing.
git diff: Shows unstaged changes.git diff --staged: Shows staged changes.git diff --cached: Same as--staged.
bash
git diff --staged
Example
This example shows how to stage a file and then use git diff --staged to see the changes ready to commit.
bash
echo 'Hello World' > file.txt
# Stage the file
git add file.txt
# Show staged changes
git diff --stagedOutput
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+Hello World
Common Pitfalls
One common mistake is using git diff without --staged, which only shows unstaged changes. Another is confusing git diff --staged with git status, which shows file states but not detailed line changes.
Always remember:
git diff= unstaged changesgit diff --staged= staged changes
bash
git diff # shows unstaged changes, not staged git diff --staged # shows staged changes ready to commit
Quick Reference
| Command | Description |
|---|---|
| git diff | Show unstaged changes in working directory |
| git diff --staged | Show staged changes ready to commit |
| git diff --cached | Alias for --staged |
| git status | Show file states but not detailed diffs |
Key Takeaways
Use
git diff --staged to see changes added to the staging area.Remember
git diff shows unstaged changes only.git diff --cached is the same as --staged.Review staged changes before committing to avoid mistakes.
git status shows file states but not detailed line differences.