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


当前回答

你可以这样做

git push origin +<short_commit_sha>^:<branch_name>

其他回答

这将删除您推送的提交

git reset --hard 'xxxxx'

git clean -f -d

git push -f

做个备份以防万一

评论中出现了两个注释:

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

这是我的方法:

假设分支名为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 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手册页。此外,查看这个答案,了解关于恢复提交的更多信息。

你可以这样做

git push origin +<short_commit_sha>^:<branch_name>
git revert HEAD -m 1

在上面的代码行。"最后一个参数表示"

1 -还原一次提交。 2 -返回最近两次提交。 N -返回上一个N 提交。

该命令后需要按一下才能对远程生效。您还有其他选项,例如指定要恢复的提交范围。这是选项之一。


稍后使用git commit -am "COMMIT_MESSAGE" 然后是git push或git push -f