检查文件状态(git status)
检查文件状态(git status)
git status
是Git中最常用的命令之一,用于查看工作目录和暂存区的当前状态。它能显示哪些文件被修改、哪些文件已暂存、哪些文件未被跟踪,以及当前分支与远程分支的关系等信息。
基本用法
直接在项目根目录下运行:
git status
输出示例:
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")
详细解读输出信息
分支信息
第一行显示当前所在分支:
On branch main
如果处于分离头指针状态(HEAD detached),会显示:
HEAD detached at a1b2c3d
远程分支同步状态
Your branch is up to date with 'origin/main'.
可能的状态包括:
Your branch is up to date with...
:本地与远程同步Your branch is ahead of...
:本地有未推送的提交Your branch is behind...
:本地落后于远程分支
变更区域
Git将文件状态分为几个主要区域:
已暂存变更(Changes to be committed)
使用git add
后,文件会进入这个区域:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: package.json
未暂存变更(Changes not staged for commit)
文件有修改但未暂存:
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)
Git尚未开始跟踪的新文件:
Untracked files:
(use "git add <file>..." to include in what will be committed)
tests/spec.js
常用选项
简短输出
使用-s
或--short
获得简洁输出:
git status -s
输出示例:
M src/index.js
A lib/helper.js
?? config.local.json
状态标志说明:
M
:已修改A
:已添加D
:已删除R
:已重命名C
:已复制??
:未跟踪
显示分支跟踪信息
git status -b
输出包含更详细的分支信息:
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
(use "git push" to publish your local commits)
忽略子模块
git status --ignore-submodules
实际应用场景
场景一:提交前的检查
# 开发完成后检查状态
git status
# 输出显示有修改未暂存
Changes not staged for commit:
modified: src/components/Button.js
# 添加文件到暂存区
git add src/components/Button.js
# 再次检查
git status
Changes to be committed:
modified: src/components/Button.js
场景二:处理合并冲突
合并分支后检查状态:
git merge feature-branch
git status
如果有冲突会显示:
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: src/App.js
场景三:清理工作目录
git status
# 显示大量未跟踪文件
Untracked files:
(use "git add <file>..." to include in what will be committed)
tmp/
*.log
# 使用.gitignore忽略这些文件
echo "tmp/" >> .gitignore
echo "*.log" >> .gitignore
git status
# 未跟踪文件消失
高级用法
显示详细变更内容
git status -v
会额外显示工作区与暂存区的差异。
忽略特定文件模式
临时忽略某些文件的状态检查:
git status --ignored
彩色输出
默认情况下Git会自动使用颜色,也可以手动启用:
git config --global color.status always
与其他命令结合
结合git diff
git status
# 发现有未暂存修改
git diff
# 查看具体修改内容
结合git clean
git status
# 显示大量未跟踪文件
git clean -n
# 预览将被删除的文件
git clean -f
# 实际删除
常见问题排查
状态显示不一致
如果git status
显示有修改但git diff
没有输出,可能是文件权限变更:
git config core.filemode false
忽略文件仍然显示
.gitignore
规则不生效时:
git rm --cached <file>
子模块状态异常
git submodule update --init
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
下一篇:跟踪新文件(git add)