Skip to content

三层工作区模型 ​

git 的"反直觉"有一半源于它有三个区——大部分人用了三年还没意识到自己改的文件分别在哪个区。理解这个模型之后,你会突然明白为什么 git add 之后又改了文件需要再 add 一次、为什么 git diff 和 git diff --cached 不一样、为什么 git checkout -- file 能"撤销" 而 git reset 也能"撤销"——它们撤销的是不同区之间的状态。

一句话先记住:git 三区是 working / staging / repo,文件在每个区都有一份当前快照。git add 把文件从 working 拷到 staging;git commit 把 staging 拷到 repo;git checkout 把 repo 刷回 working。所有"我以为我撤销了但好像没撤销"的事故,都是搞错了哪个区到哪个区。


一、三个区到底是什么 ​

┌──────────────┐  git add    ┌──────────────┐  git commit  ┌──────────────┐
│   working    │ ──────────→ │   staging    │ ──────────→  │   repo       │
│  (工作区)    │             │  (暂存区)    │              │  (本地仓库)   │
│              │ ←────────── │              │ ←─────────── │              │
└──────────────┘  checkout   └──────────────┘  reset       └──────────────┘
   你眼睛看到的            "下次要 commit 的内容"           commit 历史(.git)
   随便改、IDE 同步           git 跟踪的快照                 不可变、永久
区物理位置你直接操作?
working dir项目目录里(除了 .git)是,你的 IDE / vim 改的就是这里
staging area / index.git/index(二进制文件)不直接看,通过 git add / git rm 间接操作
repo / object store.git/objects/(blob/tree/commit 对象)不直接看,通过 commit / reset 改指针

关键认知:.git/index 不是你的源代码——它是一份"git 知道的、即将进 commit 的状态清单"。你 git add foo.txt,index 里就多了 foo.txt 当下内容的哈希。


二、git status 是看"差异",不是看"内容" ​

bash
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   src/a.ts          # ← staging vs repo 的差异

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
        modified:   src/b.ts          # ← working vs staging 的差异

Untracked files:
        new.ts                        # ← working 里 git 没见过的

这三段对应三种"差异":

段比的是怎么解决
Changes to be committedstaging vs repogit commit 提交,或 git restore --staged 撤销 add
Changes not stagedworking vs staginggit add 进暂存,或 git restore 抛弃工作区改动
Untrackedworking 里 git 不知道的git add 让 git 开始跟踪,或加进 .gitignore

看不懂 git status 的人,git 永远是黑魔法。status 是 git 给你的全息状态图,一秒看清三区分别在干嘛。


三、关键演示:同一个文件可以同时存在三种状态 ​

最反直觉的事——一个文件可以同时出现在两段 status 里。

bash
echo "v1" > a.txt
git add a.txt
echo "v2" >> a.txt        # 又改了一次,没 add
git status

输出会是:

Changes to be committed:
        new file:   a.txt        ← staging 里是 "v1"
Changes not staged for commit:
        modified:   a.txt        ← working 里是 "v1\nv2",和 staging 不一样

这件事推论极其重要:

  • 如果你这时 git commit,只有 v1 进了 repo,v2 还在工作区
  • 想把 v2 也提交,要再 git add a.txt 把 staging 刷新

这就是为什么"add 之后又改了要再 add 一次"——因为 staging 是当时那一刻 working 的快照,后续 working 的改动不会自动同步到 staging。staging 不是"指针",是"独立的副本"。


四、git diff 三剑客 ​

理解三区之后,git diff 的几种用法对应不同区之间的对比:

bash
git diff                  # working vs staging  (我改了但还没 add 的)
git diff --cached         # staging vs repo     (我 add 了准备 commit 的)
git diff --staged         # 同上(--cached 的别名)
git diff HEAD             # working vs repo     (合起来:所有未提交的改动)
git diff <commit1> <commit2>   # 任意两个 commit 之间
git diff <branch1> <branch2>   # 两条分支之间

排错套路:

我想看用哪条
我下一次 commit 会包含啥git diff --cached
我 add 完之后又改了什么git diff
我离上次 commit 一共改了多少git diff HEAD
这个 PR 改了什么git diff main..feature

不会用 git diff --cached 的人 commit 经常翻车——你以为提交了 A 但 add 之后又改了 A,实际进 commit 的是改之前的 A。commit 前用 git diff --cached 过一眼,事故概率减半。


五、四种文件状态的转换 ​

git 把每个文件的状态分成四种,转换关系:

                 git add
   Untracked ──────────→ Staged ────────┐
                                          │ git commit
                                          ↓
   Modified ←──────── Unmodified ← (commit 完后)
        │                  ↑
        │   git checkout   │
        └──────────────────┘
状态说明
Untrackedgit 不认识这个文件
Unmodified工作区和 repo / staging 三方一致
Modified工作区改了,但 staging 没跟上
Stagedstaging 改了,准备 commit

新文件第一次进 git 历史要走完整流程:Untracked → git add → Staged → git commit → Unmodified。


六、撤销操作:每个区都能撤 ​

撤销在哪一层,用的命令不同:

bash
# 撤销 working 区的修改(扔掉,回到 staging 的内容)
git restore <file>                  # git 2.23+
git checkout -- <file>              # 老命令,等价

# 撤销 staging(把 add 撤回去,working 区不变)
git restore --staged <file>
git reset HEAD <file>               # 老命令,等价

# 撤销 repo 最后一次 commit(commit 撤,改动留在 staging)
git reset --soft HEAD^

# 撤销 repo 最后一次 commit(commit 撤,改动留在 working)
git reset --mixed HEAD^             # 默认,可省略 --mixed

# 撤销 repo 最后一次 commit(commit 撤,改动也扔掉)⚠️
git reset --hard HEAD^

reset 三种模式的差别:

模式移动 HEAD改 staging改 working
--soft✅❌❌
--mixed(默认)✅✅❌
--hard✅✅✅ ⚠️ 工作区也回退

--hard 是 git 里最危险的操作之一——它会扔掉工作区未保存的改动。每次打 --hard 之前先深呼吸,确认你真的不要这些改动了。reflog 救得回 commit,救不回从未 commit 过的工作区改动。


七、git add -p:精细化的暂存 ​

只用 git add file 就把整个文件加到 staging 是粗放的。真实工程场景:你改了一个文件里 5 处,只想把其中 3 处提交,另 2 处属于另一个逻辑改动留下次提交。

git add -p(--patch)就是干这件事的:

bash
git add -p src/a.ts

git 会把这个文件的改动切成若干 hunk(改动块),每块问你:

@@ -10,7 +10,8 @@ function foo() {
   const a = 1;
+  const b = 2;
   ...
Stage this hunk [y,n,q,a,d,s,e,?]?
按键意思
y是,加进 staging
n不加
ssplit,这块再切小一点
e进编辑器手工挑行
q退出
a这文件后面所有都加
d这文件后面都不加

不会 git add -p 等于不会 git 的精细 commit。好的 commit 历史是一个个有逻辑边界的小提交,而不是 "WIP, save before lunch"。


八、查看 staging 里到底有什么 ​

调试 staging 不一致问题:

bash
git ls-files -s              # staging 里所有文件 + 它们的 blob 哈希
git ls-files -s | grep a.ts  # 看 a.ts 在 staging 的哈希
git cat-file -p <哈希>        # 把那个 blob 内容打印出来

git cat-file -p HEAD:a.ts:看 HEAD 这个 commit 里 a.ts 的内容。

这几个命令是 git 底层 API,99% 时间用不到。但当你怀疑"staging 跟我想的不一样"时,这是唯一能直接看的办法。


九、踩坑提醒 ​

  1. 不看 status 直接 commit——经常漏 add、或 add 错文件
  2. 以为 git add 之后改文件会自动同步 staging——不会,要再 add 一次
  3. commit 前不 git diff --cached——把临时 console.log 也提交进去
  4. 乱用 reset --hard——工作区改动直接没,reflog 救不回
  5. 跟踪了不该跟踪的文件(.env / node_modules)——后面要 git rm --cached 才能清,历史还留着
  6. 不会 git add -p——commit 颗粒度永远是"一整次工作的乱炖"
  7. git checkout file 和 git checkout branch 搞混——前者是从 repo 刷文件回 working,后者是切分支;新版用 restore / switch 区分清楚

下一篇:04-add与commit基础.md,讲 commit 的最佳实践、原子提交、commit message 的写法、commit --amend 的正确用途和滥用方式。