如何在 Git 中撤消提交

使用 Git 时,有时您可能想要撤消/还原您最近所做的提交。

在 git 中撤消提交的最简单方法是使用 revert 选项。

git revert <COMMIT-NAME>

这将撤消最近的提交。

实际上,有两种方法可以实现这一点。

  • git revert:恢复 git 存储库的先前状态,并将更改反映在 git log
  • git reset: 让你刚刚所做的提交看起来从一开始就不存在(本质上是删除提交)

还原现有提交

当你想撤消 Git 中的提交但又希望它反映在 Git 日志中时——历史文档在你需要它之​​前感觉没有必要——你应该使用 git revert 命令。

下面是语法 git revert 命令:

git revert <COMMIT-NAME>

提交名称可以是您用来指代提交的任何名称。 它可以是提交的 SHA1 总和(你在提交时得到它)、标签、参考名称……任何唯一标识提交的东西。

让我展示一个 example.

让我在 git 中初始化一个文件。

$ git commit -m "init commit"
[master (root-commit) b1adf72] init commit
 1 file changed, 3 insertions(+)
 create mode 100644 README.md

$ echo "a new line in readme" >> README.md

首先,我 cat README 以验证其内容。

$ cat README.md
# Heading

A readme is useless if it is empty. But this readme is even more useless because it wastes the reader's time.
a new line in readme

然后我通过使用将它添加到暂存中 git add . 我做出了一个承诺’一个将永远存在的承诺’。

$ git commit -m 'a commit that will live on for ever'
[master b731901] a commit that will live on for ever
 1 file changed, 1 insertion(+)

提交该提交给了我对该提交的引用(它是 master b731901)。 现在,我通过运行恢复到上一次提交的状态 git revert 并提到该提交。

$ git revert master b731901
[master 01c9be7] Revert "a commit that will live on for ever"
 1 file changed, 1 deletion(-)

为了确保提交反映在 Git 的日志中,我运行了 git log. 瞧,它就在那里。

$ git log
commit 01c9be75eff7b5ae48c6c35bbb7c63ac1ebc3fcd (HEAD -> master)
Author: Pratham Patel <[email protected]>
Date:   Mon Feb 28 21:29:36 2022 +0530

    Revert "a commit that will live on for ever"

    This reverts commit b731901deaa30851832c07b7cb7ed535b68d473d.

commit b731901deaa30851832c07b7cb7ed535b68d473d
Author: Pratham Patel <[email protected]>
Date:   Mon Feb 28 21:29:12 2022 +0530

    a commit that will live on for ever

commit b1adf72e535921ff966ff78f062943b717e78a08
Author: Pratham Patel <[email protected]>
Date:   Mon Feb 28 21:20:43 2022 +0530

    init commit

但是,README 文件就像从未修改过一样。 它反映了它在 Git 中的“init commit”提交时所处的状态。

$ cat README.md
# Heading

A readme is useless if it is empty. But this readme is even more useless because it wastes the reader's time.

删除提交的存在

如果您想让您错误创建的提交看起来从一开始就不存在,请使用 git reset.

提交是 Git 存储库的快照。 Git 有一个名为 HEAD 的引用变量 – 当您使用以下命令检查日志时,您可能已经看到了 git log.

这个变量 HEAD 用于指向您正在处理的分支的最新提交。

让我在 git 中启动文件更改。

$ git commit -m "init commit"
[master (root-commit) b1adf72] init commit
 1 file changed, 3 insertions(+)
 create mode 100644 README.md

$ echo "a new line in readme" >> README.md

$ cat README.md
# Heading

A readme is useless if it is empty. But this readme is even more useless because it wastes the reader's time.
a new line in readme

让我们提交这个更改:

$ git add .

$ git commit -m "a commit that i will regret later on"
[master fb58caf] a commit that i will regret later on
 1 file changed, 1 insertion(+)

现在,我将通过使用 git reset 命令将其完全删除来撤消此提交。

$ git reset --soft HEAD~1

让我们看一下 git 提交历史。

$ git log
commit b1adf72e535921ff966ff78f062943b717e78a08 (HEAD -> master)
Author: Pratham Patel <[email protected]>
Date:   Mon Feb 28 21:20:43 2022 +0530

带有“我以后会后悔的提交”消息的提交去了哪里?

它被完全删除了,因为我移到了 HEAD 指向的倒数第二个引用(表示为 HEAD~1)。

值得庆幸的是,我们在“README.md”文件中完成的所有工作都不会受到删除提交的影响。

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

--soft 标志确保运行时唯一修改的内容 git reset command 是 git 保存的日志。

如果您还想将 git 存储库的内容恢复到旧状态,请使用 --hard 旗帜, 慎用.

结论

要在 git 提交的上下文中进行撤消操作,我们使用 git revert 这是一个在日志中反映您的 git 存储库的实际历史记录的操作。 然而, git reset 完全从历史中删除提交。

git revert 是两者中更安全的选择,只有在情况需要时才应避免使用。