0
0
GitHow-ToBeginner · 3 min read

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 --staged
Output
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 changes
  • git diff --staged = staged changes
bash
git diff
# shows unstaged changes, not staged

git diff --staged
# shows staged changes ready to commit
📊

Quick Reference

CommandDescription
git diffShow unstaged changes in working directory
git diff --stagedShow staged changes ready to commit
git diff --cachedAlias for --staged
git statusShow 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.