To view the changes you’ve staged (added to Git’s staging area) but not yet committed, use the git diff --staged
(or git diff --cached
) command. Here’s how it works, with examples:
1. Basic Command
git diff --staged
or
git diff --cached # --staged and --cached are aliases
This shows a diff between the staging area (changes you’ve added with git add
) and the last commit.
Example Output:
diff --git a/file.txt b/file.txt
index 1234567..89abcde 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
Hello World!
-This line was removed.
+This line was added.
2. View Changes for a Specific File
To see staged changes for a single file:
git diff --staged -- path/to/file.txt
3. Summary of Staged Changes
For a simplified overview (e.g., filenames and line counts):
git diff --staged --stat
Example Output:
file.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
4. Alternative: git status -v
git status -v
shows the staged changes in diff format at the bottom:
git status -v
5. Key Notes
- Unstaged vs. Staged:
git diff
→ Shows unstaged changes (not yet added withgit add
).git diff --staged
→ Shows staged changes (ready to commit).- No Output? If nothing appears, there are no staged changes.
Example Workflow
- Modify
file.txt
and stage it:
git add file.txt
- View staged changes:
git diff --staged
Extra: Preview Before Committing
Use git commit -v
to include the staged diff in your commit message editor:
git commit -v
This ensures you review the exact changes before finalizing the commit.