Check file status (git status)
Check File Status (git status)
git status
is one of the most commonly used commands in Git, used to view the current state of the working directory and staging area. It shows which files have been modified, which files are staged, which files are untracked, and the relationship between the current branch and the remote branch, among other information.
Basic Usage
Run directly in the project root directory:
git status
Example output:
On branch main
Your branch is up to date with 'origin/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: src/index.js
Untracked files:
(use "git add <file>..." to include in what will be committed)
new-file.txt
no changes added to commit (use "git add" and/or "git commit -a")
Detailed Interpretation of Output
Branch Information
The first line shows the current branch:
On branch main
If in a detached HEAD state, it will display:
HEAD detached at a1b2c3d
Remote Branch Synchronization Status
Your branch is up to date with 'origin/main'.
Possible states include:
Your branch is up to date with...
: Local is synchronized with remoteYour branch is ahead of...
: Local has unpushed commitsYour branch is behind...
: Local is behind the remote branch
Change Areas
Git categorizes file states into several main areas:
Staged Changes (Changes to be committed)
After using git add
, files enter this area:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: package.json
Unstaged Changes (Changes not staged for commit)
Files have been modified but not staged:
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: src/utils.js
Untracked Files (Untracked files)
New files not yet tracked by Git:
Untracked files:
(use "git add <file>..." to include in what will be committed)
tests/spec.js
Common Options
Short Output
Use -s
or --short
for concise output:
git status -s
Example output:
M src/index.js
A lib/helper.js
?? config.local.json
Status flags explanation:
M
: ModifiedA
: AddedD
: DeletedR
: RenamedC
: Copied??
: Untracked
Show Branch Tracking Information
git status -b
Output includes more detailed branch information:
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
(use "git push" to publish your local commits)
Ignore Submodules
git status --ignore-submodules
Practical Application Scenarios
Scenario 1: Pre-Commit Check
# Check status after development
git status
# Output shows unstaged modifications
Changes not staged for commit:
modified: src/components/Button.js
# Add file to staging area
git add src/components/Button.js
# Check again
git status
Changes to be committed:
modified: src/components/Button.js
Scenario 2: Handling Merge Conflicts
Check status after merging branches:
git merge feature-branch
git status
If there are conflicts, it will display:
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: src/App.js
Scenario 3: Cleaning the Working Directory
git status
# Shows many untracked files
Untracked files:
(use "git add <file>..." to include in what will be committed)
tmp/
*.log
# Use .gitignore to ignore these files
echo "tmp/" >> .gitignore
echo "*.log" >> .gitignore
git status
# Untracked files disappear
Advanced Usage
Show Detailed Changes
git status -v
Additionally shows differences between the working directory and staging area.
Ignore Specific File Patterns
Temporarily ignore status checks for certain files:
git status --ignored
Colored Output
Git uses colors by default, but you can also enable it manually:
git config --global color.status always
Combining with Other Commands
Combining with git diff
git status
# Find unstaged changes
git diff
# View specific changes
Combining with git clean
git status
# Shows many untracked files
git clean -n
# Preview files to be deleted
git clean -f
# Actually delete
Common Issue Troubleshooting
Inconsistent Status Display
If git status
shows modifications but git diff
has no output, it may be due to file permission changes:
git config core.filemode false
Ignored Files Still Showing
When .gitignore
rules don't take effect:
git rm --cached <file>
Submodule Status Anomalies
git submodule update --init
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
下一篇:跟踪新文件(git add)