How to See Changes in Git: Commands and Examples
To see changes in Git, use
git status to view modified files and git diff to see line-by-line changes. For committed changes, use git log to review the history.Syntax
Here are the main commands to see changes in Git:
git status: Shows which files have changed but are not yet committed.git diff: Shows the exact line changes in files that are not staged.git diff --staged: Shows changes that are staged for the next commit.git log: Shows the commit history with details.
bash
git status
git diff
git diff --staged
git logExample
This example shows how to check changes after editing a file named app.txt:
bash
echo "Hello" > app.txt # Edit app.txt to add a new line git status git diff git add app.txt git diff --staged git commit -m "Update app.txt" git log -1
Output
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: app.txt
--- a/app.txt
+++ b/app.txt
@@ -1 +1,2 @@
Hello
+New line added
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: app.txt
[main abc1234] Update app.txt
1 file changed, 1 insertion(+)
commit abc1234def5678901234567890abcdef1234567 (HEAD -> main)
Author: Your Name <you@example.com>
Date: Thu Jun 1 12:00:00 2024 +0000
Update app.txt
Common Pitfalls
Common mistakes when checking changes in Git include:
- Not staging changes before using
git diff --staged, which shows nothing if no files are staged. - Confusing
git diffoutput with committed history; usegit logfor commits. - Ignoring untracked files that
git statusshows butgit diffdoes not.
bash
git diff --staged
# No output if nothing staged
# Correct way:
git add file.txt
git diff --stagedQuick Reference
| Command | Purpose |
|---|---|
| git status | Show changed and untracked files |
| git diff | Show unstaged changes line-by-line |
| git diff --staged | Show staged changes line-by-line |
| git log | Show commit history |
Key Takeaways
Use
git status to quickly see which files have changed.Use
git diff to view detailed line changes before committing.Stage files with
git add to include them in the next commit.Use
git diff --staged to see what is staged for commit.Use
git log to review past commits and their messages.