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


当前回答

Git重置<commit_hash>(获取<commit_hash>使用Git log——oneline)

Git恢复。将所有更改的文件恢复到yout目标提交的版本

Git push origin master——force强制推到远程主分支。但如果有人和你在同一分支工作,在使用力推时要小心

其他回答

这是我的方法:

假设分支名为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 push origin +<short_commit_sha>^:<branch_name>

重置对我来说很困难: 谢谢@Mo D Genensis和@vibs2006


git reset --hard 'your last working commit hash'

git clean -f -d

git push -f

一种不保留“撤销”痕迹的解决方案。

注意:如果有人已经抽取了您的更改,则不要这样做 (我只会在个人回购时使用这个方法。)

run:

git reset <previous label or sha1>

这将在本地重新签出所有更新(因此git状态将列出所有更新的文件)

然后你“做你的工作”并重新提交你的更改(注意:这一步是可选的)

git commit -am "blabla"

此时,您的本地树与远程树不同

git push -f <remote-name> <branch-name>

将强制远程分支接受此推送并删除前一个推送(指定remote-name和branch-name不是强制的,但建议避免更新带有update标志的所有分支)。

!! 注意一些标签可能仍然指向删除提交!!how-to-delete-a-remote-tag

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

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