我在远程存储库中有一个项目,与本地存储库(开发)和服务器存储库(prod)同步。我一直在做一些已经推送到远程和从服务器上拉出来的承诺更改。现在,我想撤销这些更改。因此,我可以在更改之前签出到提交,并提交新更改,但我猜再次将它们推到远程会有问题。我该怎么做,有什么建议吗?


当前回答

这是我的方法:

假设分支名为develop。

# Checkout a new temp branch based on one history commit
# (command to get history commit hashes: git log --oneline)
git checkout <last_known_good_commit_hash>

# Delete the original develop branch 
git branch -D develop
# Create a new develop branch based on the temp branch
git checkout -b develop

# Force update the remote with the new branch
git push -f origin develop

其他回答

如果你按照下面给出的步骤通过Git命令行,你可以将Git本地和远程提交还原(或者你也可以称之为DELETE)。

运行以下命令查看要还原的提交id

git log --oneline --decorate --graph

你会得到如下截图

如果你也检查远程(通过Web接口),那么你可以看到这将是相同的如下所示

根据截图,目前你的提交id是e110322,但你想在本地和远程都恢复到030bbf6。

执行以下步骤删除/恢复本地+远程提交


首先本地恢复提交id 030bbf6

git reset --hard 030bbf6

紧随其后的是

git clean -f -d

这两个命令强制重置以提交阶段030bbf6,如下图所示

现在如果你跑了 git状态,然后你会看到你在远程分支中是TWO Commits BEHIND,如下所示

运行以下命令更新索引(如果有更新的话)。建议您要求所有开发人员不要接受主远程分支上的任何拉请求。

git fetch --all

一旦你完成了它,然后你需要在分支前面使用+符号强制推送这个提交,如下所示。我这里用的是主分支,你可以替换成任何分支

代码

git push -u origin +master

现在如果你看到远程的web界面,那么提交也应该被还原。

你可以通过以下方法恢复单个提交:

git revert <commit_hash>

这将创建一个新的提交,该提交将恢复您指定的提交的更改。请注意,它只恢复特定的提交,而不恢复之后的提交。如果你想要还原一系列的提交,你可以这样做:

git revert <oldest_commit_hash>..<latest_commit_hash>

它将还原<oldest_commit_hash>之后直到<latest_commit_hash>的所有提交。在某些版本的git上,它还会恢复<oldest_commit_hash>,因此请仔细检查该提交是否被恢复。您总是可以使用g reset—hard HEAD~来删除最新的恢复提交(它恢复了最早的提交)。

要找出提交的哈希值,可以使用git log。

有关git revert命令的更多信息,请参阅git-revert手册页。此外,查看这个答案,了解关于恢复提交的更多信息。

这是我的方法:

假设分支名为develop。

# Checkout a new temp branch based on one history commit
# (command to get history commit hashes: git log --oneline)
git checkout <last_known_good_commit_hash>

# Delete the original develop branch 
git branch -D develop
# Create a new develop branch based on the temp branch
git checkout -b develop

# Force update the remote with the new branch
git push -f origin develop

这将删除您推送的提交

git reset --hard 'xxxxx'

git clean -f -d

git push -f

做个备份以防万一

评论中出现了两个注释:

'xxxxx'是你想保留的最后一次提交的哈希码。 清洁是危险的。它删除文件,不受版本控制!

另一种不需要恢复(undo的痕迹)的方法:

如果其他人已经推送了其他提交,就不要这样做

创建你的分支的备份,在你的分支my-branch。因此,如果出现错误,您可以重新启动进程而不会损失任何已完成的工作。

git checkout -b my-branch-temp

回到你的分行去。

git checkout my-branch

重置,放弃你的最后一次提交(撤销它):

git reset --hard HEAD^

移除远程上的分支(例如origin远程)。

git push origin :my-branch

将分支重新推到远程(没有不必要的提交)。

git push origin my-branch

完成了!

我希望这对你有所帮助!;)