简体   繁体   English

撤消已推送到远程仓库的提交

[英]Undoing commits that have been pushed to remote repo

We made some changes to a branch, committed and merged them to master, then pushed to the remote. 我们对分支进行了一些更改,将其提交并合并到master,然后推送到远程。 Subsequently we made more commits to master and pushed those up, related to the merge. 随后,我们进行了更多的提交来掌握并推高了与合并有关的提交。

As it turns out, that merge and the subsequent modifications were the wrong way to go about solving our problem, and we'd like to revert to just before the merge. 事实证明,合并和随后的修改是解决问题的错误方法,我们希望恢复到合并之前。 In looking at the SO Git for beginners guide I see the reset command, but for changes that have already been pushed is that good enough, calling reset and then pushing? 在查看SO Git初学者指南时,我看到了reset命令,但是对于已经推送的更改就足够了,调用reset然后推送?

It's not our case here, but suppose that after we had merged, and done some fix-up commits/pushed afterward, there were other, unrelated pushes to master, what's a good way of only undoing our changes and not the unrelated ones? 这不是我们的情况,但是假设我们合并后进行了一些修正提交/推送,那么还有其他不相关的推送要掌握,有什么好方法可以只撤销我们的更改而不撤销不相关的更改?

There are two approaches here. 这里有两种方法。 The first is to reset your local branch, as if these changes never existed, and then force update the remote branch: 首先是reset您的本地分支,就像这些更改不存在一样,然后强制更新远程分支:

git rest <hash>
git push https://git.... --force

The main problem with this approach is the difficulty it creates for anyone else working with your remote. 这种方法的主要问题是它给使用遥控器的其他人带来的困难。

The more "elegant" solution, IMHO, is to revert the change - ie, acknowledge a mistake has been made, and rectify it, preserving history. 恕我直言,更“优雅”的解决方案是还原更改-即确认已犯了一个错误,并纠正它,保留历史记录。

You can use git revert <hash> to create an additional commit that undoes the problematic commit, and then just git push it like every other commit. 您可以使用git revert <hash>创建一个其他提交来撤消有问题的提交,然后像其他所有提交一样git push The advantage is that you can always amend the commit message, and explain exactly why the original solution was faulty, and why it had to be undone. 优点是您可以随时修改提交消息,并确切说明原始解决方案有问题的原因以及为什么必须撤消它。

git reset --hard <tag/branch/commit id>

在这里查看更多信息

The question was already asked here 这个问题已经在这里提出了

Rewriting git history with a private remote repository 用私有远程存储库重写git历史记录

If other people have performed git pull on the repository, the most advised approach is to revert the commits search for it, there is many questions like that with many answer and find the one the most appropriate for you. 如果其他人在存储库上执行了git pull,那么建议的方法是还原对它的提交搜索,类似的问题很多,答案很多,找到最适合您的问题。

To avoid these problems in the future, always use an temporary "topic" branch until you are satisfied with the results. 为了避免将来出现这些问题,请始终使用一个临时的“主题”分支,直到您对结果满意为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM