Skip to content

git 状态

Inspecting 状态的 你的仓库使用 git 状态 — 你的 Git dashboard.

你将学到

  • 如何 读取 git 状态输出
  • three 小节: staged, unstaged, and untracked
  • Using git 状态 -s for compact view
  • 为什么 应该运行 git 状态 constantly

概念讲解

git status 的作用

git 状态 is the most frequently used Git command. It tells you the current state of your repository: what has changed, what is staged, and what is untracked. Run it constantly — before and after every other command.

git status

Reading the Output

The output has three sections:

On branch main

Changes to be committed:        ← STAGED (will be in next commit)
  modified:   index.html

Changes not staged for commit:  ← UNSTAGED (modified but not added)
  modified:   style.css

Untracked files:                ← UNTRACKED (Git is not tracking these)
  new-file.js
  • Changes to be committed — files in the staging area. These will be included in your next git 提交.
  • Changes not staged for commit — tracked files that have been modified but not yet git 添加-ed.
  • Untracked files — new files that Git has never seen. Use git 添加 to start tracking them.

The Short Format

For a quick, compact view, use git 状态 -s (or --短):

git status -s
M  index.html     ← M in first column = staged modification
 M style.css      ← M in second column = unstaged modification
?? new-file.js    ← ?? = untracked
A  added-file.js  ← A = staged new file
D  deleted-file.js ← D = staged deletion

The first column shows the staged state, the second shows the unstaged state. Once you can read this, you will prefer it over the long format.

Why git status Is Essential

git 状态 prevents mistakes:

  • Before committing — make sure you are committing what you think you are
  • After cloning — confirm which branch you are on
  • Before branching — make sure your working tree is clean
  • After merging — check for conflicts

If git 状态 says 无 到 提交, 工作 树 clean, it means there are no uncommitted changes — you are in a safe state to branch, pull, or push.

Useful Flags

git status                  # full output
git status -s               # short, compact output
git status --branch         # include branch tracking info
git status --ignored        # show ignored files too
git status -uno             # hide untracked files

示例代码

              # Make some changes to see different states
echo "new content" >> README.md       # modify a tracked file
echo "body { color: red; }" >> style.css  # modify another tracked file
echo "console.log('new');" > new-feature.js  # create a new file

# Full status output — three sections
git status

# Short status — compact one-line-per-file format
git status -s

# Stage one file and see how the status changes
git add README.md
git status -s
# README.md now shows 'M ' (staged) instead of ' M' (unstaged)

# Stage everything and check again
git add .
git status -s
# All files now show in the staged column

# Commit and verify clean tree
git commit -m "Add new feature and update styles"
git status
# "nothing to commit, working tree clean"

# See branch tracking information
git status -b
            

创建更改, 然后 use git 状态 and git 状态 -s 到 see 如何 files move 之间 untracked, unstaged, and staged 状态. 短格式是 worth memorizing — 它是 fastest 方式到 检查你的仓库.

动手试一试

  • Code Diff

    Compare versions — git 状态 tells 你什么更改, diff shows 如何.

  • File Hash Checksum

    Git uses 内容 hashes 到 detect 更改 — see hashing in 操作.

常见错误

错误写法

不运行 git 状态 and committing 错误 files

正确做法

总是运行 git 状态之前 git 提交到 see 精确地什么将 是 committed. Use git 状态 -s for 快速检查. 这个 prevents accidentally committing 调试 code or sensitive files.

错误写法

Ignoring untracked files 那个应该是 committed

正确做法

Untracked files (marked 使用 ??) 是简单到 miss. 运行 git 状态 and look for 'Untracked files' 小节. 如果 file 应该是 in 仓库, git 添加它; 如果不, 添加它 到 .gitignore.

相关速查表

练习 git 状态

5 个练习

练习 git 状态

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误