1. 概述

Git 是一个强大的版本控制系统,它将项目状态分为三个主要区域:

  • 工作区(Working Tree)
  • 暂存区(Staging Tree)
  • 提交历史(Commit Tree)

通常的操作流程是:在工作区修改文件 → 将修改添加到暂存区 → 最后提交到历史记录。但在某些情况下,我们可能希望取消暂存(unstage)某些文件,而不是直接提交。

本文将介绍几种在 Git 中取消暂存文件的方法,并对比它们之间的区别。我们将重点关注以下命令:

  • git restore --staged
  • git reset HEAD
  • git rm --cached

这些命令虽然都能实现“取消暂存”的效果,但其行为和副作用却大不相同。


2. 标准方法:git restore --staged

这是目前推荐的标准方式,用于将文件从暂存区移回工作区,保留其修改内容。

✅ 示例

$ echo $(date) >> file
$ git add file
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file

执行取消暂存:

$ git restore --staged file

此时再查看状态:

$ git status
On branch master
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:   file

no changes added to commit (use "git add" and/or "git commit -a")

🔍 特点

  • ✅ 只取消暂存,不删除文件内容
  • ✅ 命令语义清晰,推荐使用
  • ❌ 必须指定文件路径

3. 替代方法:git reset HEAD

git reset 是一个功能强大的命令,常用于回退提交或暂存内容。使用 git reset HEAD <file> 也可以实现取消暂存的效果。

✅ 示例

$ git reset HEAD file
Unstaged changes after reset:
M       file

查看状态:

$ git status
On branch master
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:   file

no changes added to commit (use "git add" and/or "git commit -a")

🔍 特点

  • ✅ 支持不指定文件,批量取消暂存
  • ❌ 语法不如 restore 直观
  • ⚠️ 使用不当可能误删提交(如 --hard

4. 危险操作:git rm --cached

虽然这个命令主要用于删除文件,但如果加上 --cached 参数,它也可以用于取消暂存,同时保留文件在工作区。

✅ 示例

$ git rm --cached file

查看状态:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    file

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file

🔍 特点

  • ❌ 文件会变成“未追踪”状态(untracked)
  • ❌ 有潜在副作用,可能造成混乱
  • ✅ 适用于想从版本库中移除但保留在本地的情况(如 .log 文件)

5. 总结

方法 是否保留内容 是否改变文件状态 推荐使用场景
git restore --staged 取消暂存标准操作
git reset HEAD 快速取消暂存多个文件
git rm --cached 从版本库中移除文件但保留本地文件

✅ 建议

  • 优先使用 git restore --staged,语义清晰且安全
  • 如果需要批量取消暂存,可使用 git reset HEAD
  • git rm --cached 要谨慎使用,避免误操作导致文件状态混乱

扩展阅读


原始标题:Methods to Unstage a File in Git