Git 教程 - 撤销修改

Jinku Hu 2020年6月25日 2018年7月19日
Git 教程 - 撤销修改

在本教程中,我们将来介绍如何从暂存区撤销修改。

从暂存区撤销修改

在你将某些文件修改添加到临时区域后,有可能会遇到一种情况,就是你发现修改并不完全正确,你想继续对文件进行更多修改,所以你不希望将此更改提交到版本库,这时候你需要从暂存区中撤销该文件的修改。

如果你用 git status 查看的话,能够看到文件更改已在暂存区中,并且它还提示可以使用 git reset HEAD <file>... 命令来将修改从暂存区撤销。

我们来看下工作区状态,

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	renamed:    test1_rename.txt -> move/test1.txt

我们希望撤销这次重命名的修改,所以我们用下面的命令,

$ git reset HEAD
Unstaged changes after reset
D test1_rename.txtgi

然后,我们再来查看工作区的状态,它就变成了,

$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    test1_rename.txt

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

	move/

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

你可以看出来,重命名的修改已经被撤销,工作区返回到了重命名之前的状态。

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn