How to See File Changes Before Commit in Git
Use
git diff to see unstaged changes and git diff --staged to view changes staged for commit. Additionally, git status shows which files have been modified or staged.Syntax
The main commands to see file changes before commit are:
git diff: Shows changes in files that are not yet staged.git diff --staged(orgit diff --cached): Shows changes that are staged and ready to be committed.git status: Lists files that are modified, staged, or untracked.
bash
git diff git diff --staged git status
Example
This example shows how to check changes before committing. First, modify a file, then check unstaged changes with git diff. Stage the file with git add, then check staged changes with git diff --staged. Finally, use git status to see the file states.
bash
echo "Hello" > file.txt # Modify file.txt echo "World" >> file.txt # Check unstaged changes git diff # Stage the file git add file.txt # Check staged changes git diff --staged # Check status git status
Output
diff --git a/file.txt b/file.txt
index e69de29..b6fc4c6 100644
--- a/file.txt
+++ b/file.txt
@@ -0,0 +1,2 @@
+Hello
+World
(diff output shows added lines)
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file.txt
nothing else to commit, working tree clean
Common Pitfalls
Common mistakes include:
- Running
git diffafter staging changes, which shows no output because it only shows unstaged changes. - Confusing
git diff --stagedwithgit diff. - Not staging files before checking staged changes.
Always check git status to understand file states.
bash
git add file.txt git diff # Shows no output because changes are staged git diff --staged # Shows staged changes
Quick Reference
| Command | Description |
|---|---|
| git diff | Shows unstaged changes in files |
| git diff --staged | Shows staged changes ready to commit |
| git status | Shows file states: modified, staged, untracked |
Key Takeaways
Use
git diff to see changes not yet staged for commit.Use
git diff --staged to see changes staged and ready to commit.Always run
git status to check the current state of files.Staging files with
git add moves changes from unstaged to staged.Confusing unstaged and staged changes is a common source of errors.