Git 取消暂存:一步步教你操作 – wiki基地

Git 取消暂存:一步步教你操作

在使用 Git 进行版本控制时,”暂存区” (staging area 或 index) 是一个非常重要的概念。它位于你的工作目录和版本历史之间,允许你在提交 (commit) 前选择性地准备你的更改。然而,有时你可能会不小心暂存了错误的文件,或者在提交前改变了主意,想要将文件从暂存区移回工作目录。这就是 “取消暂存” (unstage) 操作的用武之地。

本文将详细介绍 Git 中取消暂存的几种方法,并提供一步步的操作指南。


什么是暂存区 (Staging Area)?

在深入了解如何取消暂存之前,我们先快速回顾一下暂存区的概念。

当你对工作目录中的文件进行修改后,这些修改并不会立即成为下一次提交的一部分。你需要使用 git add <file> 命令将这些修改添加到暂存区。暂存区就像一个“待提交清单”,里面包含了你希望包含在下一个快照(提交)中的所有更改。

操作流程概览:

  1. 修改文件: 在工作目录中编辑文件。
  2. 添加到暂存区: git add <file> 将修改的文件加入暂存区。
  3. 提交更改: git commit -m "提交信息" 将暂存区的更改永久保存到版本历史中。

取消暂存,就是将步骤 2 中添加到暂存区的更改,重新移回到步骤 1 的工作目录状态(但修改本身依然存在于工作目录中)。


何时需要取消暂存?

你可能需要取消暂存的情况包括:

  • 误操作: 不小心 git add . 将所有文件都暂存了,但只想提交部分文件。
  • 改变主意: 暂存了一些更改,但后来决定这些更改不应该属于当前提交。
  • 分离提交: 你在同一个文件中做了多个不相关的修改,希望将它们拆分成两次独立的提交。

如何取消暂存文件?

Git 提供了两种主要命令来取消暂存文件:git restore --staged (推荐,现代用法) 和 git reset HEAD (传统用法)。

方法一:使用 git restore --staged (推荐)

git restore 命令是 Git 2.23 版本引入的,旨在更清晰地分离“恢复文件内容”和“恢复暂存区状态”这两种操作。它更直观地表达了“从暂存区恢复到工作目录”的意图。

1. 取消暂存单个文件

如果你只想取消暂存一个特定的文件,使用以下命令:

bash
git restore --staged <文件名>

示例:

假设你修改了 index.htmlstyle.css,然后执行了 git add .。现在你决定不想在本次提交中包含 style.css

“`bash

1. 修改文件并添加到暂存区

echo “New content” > index.html
echo “New styles” > style.css
git add .

2. 查看当前状态(style.css 已暂存)

git status

On branch main

Changes to be committed:

(use “git restore –staged …” to unstage)

new file: index.html

new file: style.css

3. 取消暂存 style.css

git restore –staged style.css

4. 再次查看状态

git status

On branch main

Changes to be committed:

(use “git restore –staged …” to unstage)

new file: index.html

Changes not staged for commit:

(use “git add …” to update what will be committed)

(use “git restore …” to discard changes in working directory)

modified: style.css

“`

可以看到,style.css 已经从 “Changes to be committed” 区域移到了 “Changes not staged for commit” 区域,这意味着它已成功取消暂存。

2. 取消暂存所有文件

如果你想取消暂存所有已暂存的更改:

bash
git restore --staged .

注意:这里的 . 代表当前目录下的所有文件。

示例:

“`bash

1. 暂存所有修改

git add .

2. 取消暂存所有文件

git restore –staged .

3. 再次查看状态

git status

On branch main

Changes not staged for commit:

(use “git add …” to update what will be committed)

(use “git restore …” to discard changes in working directory)

modified: index.html

modified: style.css

“`

方法二:使用 git reset HEAD (传统用法)

git restore 出现之前,git reset HEAD <file> 是取消暂存的标准命令。它仍然有效,并且在很多教程和老项目中你可能会看到它。

git reset 是一个功能强大的命令,用于撤销提交、修改 HEAD 指针等。当与 HEAD 结合使用时,它会将暂存区重置为 HEAD 指向的最新提交的状态,但不会修改你的工作目录。

1. 取消暂存单个文件

bash
git reset HEAD <文件名>

或,在现代 Git 中,git status 可能会提示你使用更简洁的形式:

bash
git reset <文件名>

示例:

继续上面的例子,取消暂存 style.css

“`bash

1. 修改文件并添加到暂存区

echo “New content” > index.html
echo “New styles” > style.css
git add .

2. 查看当前状态

git status

On branch main

Changes to be committed:

(use “git restore –staged …” to unstage)

new file: index.html

new file: style.css

3. 取消暂存 style.css

git reset HEAD style.css

或 git reset style.css

Unstaged changes after reset:

M style.css

4. 再次查看状态

git status

On branch main

Changes to be committed:

(use “git restore –staged …” to unstage)

new file: index.html

Changes not staged for commit:

(use “git add …” to update what will be committed)

(use “git restore …” to discard changes in working directory)

modified: style.css

“`

效果与 git restore --staged 相同。

2. 取消暂存所有文件

bash
git reset HEAD .

或更常见、更简洁的形式:

bash
git reset

git reset 不带文件名参数时,它默认作用于所有已暂存的文件。

示例:

“`bash

1. 暂存所有修改

git add .

2. 取消暂存所有文件

git reset

或 git reset HEAD .

Unstaged changes after reset:

M index.html

M style.css

3. 再次查看状态

git status

On branch main

Changes not staged for commit:

(use “git add …” to update what will be committed)

(use “git restore …” to discard changes in working directory)

modified: index.html

modified: style.css

“`


重要提示:取消暂存不会丢失你的修改!

无论是 git restore --staged 还是 git reset HEAD,它们都只影响暂存区。你的工作目录中的文件内容不会受到影响。这意味着,取消暂存后,你对文件的所有修改仍然存在,只是它们不再处于“待提交”的状态。

如果你想丢弃工作目录中的修改,使其恢复到上次提交或暂存时的状态,你需要使用 git restore <file> (不带 --staged) 或 git checkout -- <file> 命令。这是一个完全不同的操作,请务必区分。

  • git restore --staged <file>: 将文件从暂存区移回工作目录(保留修改)。
  • git restore <file>: 丢弃工作目录中的修改,使文件恢复到暂存区或上一次提交时的状态。

总结

取消暂存是 Git 日常工作流中一个非常实用的技能,它允许你精确控制每一次提交的内容。

  • 现代推荐: 使用 git restore --staged <文件名>git restore --staged .
  • 传统但仍然有效: 使用 git reset HEAD <文件名>git reset

理解暂存区和取消暂存操作,能帮助你更好地组织提交,保持提交历史的整洁和意义明确。在进行任何提交之前,养成使用 git status 检查暂存区内容的习惯,并在必要时利用这些命令进行调整,将大大提升你的 Git 使用效率。

滚动至顶部