Git - Status & Inspection
Beginner
Understanding your repository's current state is crucial for effective Git usage. The status and inspection commands help you see what's changed, what's staged, and what Git is tracking.
The git status Command
git status
is your most important diagnostic tool. It shows:
- Current branch name
- Files that are staged for commit
- Files that are modified but not staged
- Untracked files
- Repository state (ahead/behind remote, merge conflicts, etc.)
Basic Status Output
git status
Clean repository:
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Repository with changes:
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: README.md
new file: feature.js
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes to working directory)
modified: index.html
deleted: old-file.txt
Untracked files:
(use "git add ..." to include in what will be committed)
temp.log
new-feature/
Status Output Sections Explained
1. Branch Information
On branch main ← Current branch
Your branch is ahead of 'origin/main' ← Sync status with remote
2. Changes to be committed (Staged)
Changes to be committed: ← Green in colored terminals
modified: README.md ← File was changed and staged
new file: feature.js ← New file staged for first commit
3. Changes not staged for commit (Modified)
Changes not staged for commit: ← Red in colored terminals
modified: index.html ← File changed but not staged
deleted: old-file.txt ← File deleted but not staged
4. Untracked files
Untracked files: ← Red in colored terminals
temp.log ← New file Git doesn't know about
new-feature/ ← New directory with files
Status Command Variations
Short Status Format
git status -s
# or
git status --short
Short format output:
M README.md ← Modified, not staged
A feature.js ← Added (staged)
MM index.html ← Modified and staged, then modified again
D old-file.txt ← Deleted, not staged
?? temp.log ← Untracked
?? new-feature/ ← Untracked directory
Status Format Legend
Code | Meaning | Example |
---|---|---|
?? | Untracked | ?? new-file.txt |
A | Added (staged) | A new-file.txt |
M | Modified | M existing-file.txt |
D | Deleted | D removed-file.txt |
R | Renamed | R old.txt -> new.txt |
C | Copied | C original.txt -> copy.txt |
MM | Modified, staged, then modified again | MM file.txt |
Branch Status Options
# Show branch tracking information
git status -b
git status --branch
# Hide untracked files
git status -uno
git status --untracked-files=no
# Show ignored files too
git status --ignored
Understanding git diff
git diff
shows exactly what changed in your files. There are several variations depending on what you want to compare:
Basic Diff Commands
Show unstaged changes:
git diff
Example diff output:
diff --git a/README.md b/README.md
index 1234567..abcdefg 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,4 @@
# My Project
This is a sample project.
+Added a new line here.
Show staged changes (what will be committed):
git diff --staged
# or
git diff --cached
Show all changes (staged and unstaged):
git diff HEAD
Diff for Specific Files
# Diff specific file
git diff README.md
# Diff multiple files
git diff README.md src/main.js
# Diff directory
git diff src/
Reading Diff Output
Diff header explanation:
diff --git a/README.md b/README.md ← Files being compared
index 1234567..abcdefg 100644 ← Object hashes and file mode
--- a/README.md ← Original file (-)
+++ b/README.md ← New file (+)
@@ -1,3 +1,4 @@ ← Line numbers: old start,count new start,count
Change indicators:
unchanged line ← No prefix
-removed line ← Red, minus prefix
+added line ← Green, plus prefix
Advanced Diff Options
Diff Formatting
# Word-level diff instead of line-level
git diff --word-diff
# Show statistics
git diff --stat
# Show just file names
git diff --name-only
# Show file names with status
git diff --name-status
Diff with statistics:
$ git diff --stat
README.md | 2 +-
src/app.js | 15 +++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
Comparing Different States
# Compare working directory to specific commit
git diff a1b2c3d
# Compare two commits
git diff a1b2c3d b2c3d4e
# Compare branches
git diff main feature-branch
# Compare with remote branch
git diff origin/main
Practical Workflow Examples
Daily Development Workflow
1. Check current status:
git status
2. See what changed:
git diff
3. Stage specific changes:
git add filename.js
4. Review staged changes:
git diff --staged
5. Commit when ready:
git commit -m "Add new feature"
Pre-Commit Review Process
Complete review before committing:
# 1. See overall status
git status
# 2. Review all changes
git diff HEAD
# 3. Check what's staged
git diff --staged
# 4. See file-by-file statistics
git diff --stat
# 5. Commit if satisfied
git commit
Working Tree States
Understanding the different states your working tree can be in:
Clean Working Tree
Status: "nothing to commit, working tree clean"
Meaning: All changes are committed, no modifications
Meaning: All changes are committed, no modifications
Dirty Working Tree
Status: Shows modified, staged, or untracked files
Meaning: There are uncommitted changes
Meaning: There are uncommitted changes
Merge Conflicts
Status: "You have unmerged paths"
Meaning: Merge conflicts need to be resolved
Meaning: Merge conflicts need to be resolved
Status and Diff in Different Scenarios
During a Merge
Status during merge conflict:
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: config.js
no changes added to commit (use "git add" or "git commit -a")
During a Rebase
Status during rebase:
interactive rebase in progress; onto a1b2c3d
Last command done (1 command done):
pick b2c3d4e Add user authentication
Next commands to do (2 remaining commands):
pick c3d4e5f Update documentation
pick d4e5f6g Fix bug in login
(use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'feature' on 'a1b2c3d'.
(fix conflicts and run "git rebase --continue")
(use "git rebase --skip" to skip this patch)
(use "git rebase --abort" to check out the original branch)
nothing to commit, working tree clean
Configuration and Customization
Status Configuration
# Always show short status
git config --global status.short true
# Show branch information
git config --global status.branch true
# Show stash count
git config --global status.showStash true
# Use relative paths
git config --global status.relativePaths true
Diff Configuration
# Better diff algorithm
git config --global diff.algorithm histogram
# Show more context lines
git config --global diff.context 5
# Enable word diff
git config --global diff.wordRegex .
# Use external diff tool
git config --global diff.tool vimdiff
Troubleshooting Status Issues
Status Shows Wrong Information
Refresh Git's index:
git status --porcelain | wc -l # Count changes
git add -A # Stage everything
git reset # Unstage everything
git status # Should be accurate now
Large Repository Performance
# Skip untracked files check (faster)
git status -uno
# Use short format (faster)
git status -s
# Check specific paths only
git status src/
Aliases for Efficiency
# Useful status and diff aliases
git config --global alias.st status
git config --global alias.ss "status -s"
git config --global alias.d diff
git config --global alias.ds "diff --staged"
git config --global alias.dw "diff --word-diff"
Now you can use short commands:
git st # git status
git ss # git status -s
git d # git diff
git ds # git diff --staged
git dw # git diff --word-diff
Summary
You now understand Git status and inspection:
- ✅
git status
shows the current state of your repository - ✅ Status output has clear sections for staged, modified, and untracked files
- ✅
git diff
shows exactly what changed in files - ✅ Different diff commands compare different states
- ✅ Short status format provides concise information
- ✅ Status and diff work together for effective workflow
- ✅ Configuration options customize the output
Next Steps
Now that you can inspect your repository state, let's learn about viewing and exploring project history:
Practice Tip: Make
git status
and git diff
part of your muscle memory. Check status frequently during development to stay aware of your repository state.