撤消远程 Git 仓库中的最后一次提交

John Wachira 2022年7月12日
撤消远程 Git 仓库中的最后一次提交

本文将讨论从远程 Git 仓库中删除最后一次提交。如果当前提交不符合我们的期望,Git 可以很容易地回滚到之前的提交。

让我们看看如何做到这一点。

撤消远程 Git 仓库中的最后一次提交

让我们模拟一种情况,我们必须在远程仓库中回滚一次提交。

我们的仓库 Delftscopetech 有一个文件 README.md。我们将进行更改、提交并将它们推送到远程仓库。

推送

我们已将更改推送到我们的远程仓库。这是我们的远程仓库。

远程版本库

在我们想要撤消此提交的情况下,我们将如何处理?

我们将运行 git log 命令来显示我们 repo 中所有提交的列表。使用 --oneline 选项来简化输出。

$ git log --oneline

git log oneline

下一步是重置 HEAD,以便参考位于 Sixth Update。我们将运行 git reset 命令并传递我们的 Sixth UpdateCommit ID,如下所示。

$ git reset --hard 27bd68b
HEAD is now at 27bd68b Sixth Update

如果我们要运行 git log 命令,我们会发现 Updated README.md File 提交丢失了。我们已经从本地仓库中删除了这个提交,唯一剩下的就是将更改推送到我们的远程仓库,如下所示。

我们将需要运行强制推送,因为远程仓库提前了一次提交。我们运行下面的命令。

$ git push -f

git push -f

我们的远程仓库已更新。让我们确认一下。

更新的远程仓库

差不多就是这样。在撤消错误提交之前,确保其他开发人员没有从远程获取。

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

相关文章 - Git Reset

相关文章 - Git Commit