简体   繁体   English

如何撤消已推送到远程服务器的 git 中的多个提交?

[英]How to undo multiple commits in git that have been pushed to a remote server?

I am not sure how to fix a situation with commits that should not have been merged into the master branch.我不确定如何解决不应该合并到master分支的提交的情况。 I am not well versed in git and have been mostly using the Visual Studio controls for it.我不太精通 git 并且大部分时间都在使用 Visual Studio 控件。

A while ago I merged a bunch of feature branches into develop branch.前段时间把一堆feature分支合并成了develop分支。 However, I was not supposed to do that yet.但是,我不应该那样做。 Recently, a bug in the application needed fixing.最近,应用程序中的一个错误需要修复。 So, I branched out of develop , fixed it, and merged back into develop .所以,我从develop分支出来,修复它,然后合并回develop Then, I merged develop into master so that I can release the bug fixes.然后,我将develop合并到master中,以便我可以发布错误修复。

After doing that, I realized I had forgotten about those feature branches that aren't to be released yet and now they are in master along with the bug fixes.这样做之后,我意识到我忘记了那些尚未发布的功能分支,现在它们与错误修复一起在master中。 It's around 200 commits that need to be reverted, so I don't want to do them one by one.大约有 200 个提交需要还原,所以我不想一一进行。 Once I revert them, I'd eventually need to undo the reverts as well so I can release those features.一旦我还原它们,我最终也需要撤消还原,以便我可以发布这些功能。

I first tried to undo changes up to a specific commit by doing Reset > Delete Changes (--hard) , but since the changes are pushed to the remote repository, they just reappear as incoming changes.我首先尝试通过执行Reset > Delete Changes (--hard)来撤消对特定提交的更改,但是由于更改被推送到远程存储库,它们只是作为传入更改重新出现。

Then I looked up a way to revert multiple commits and tried reverting a range:然后我查找了一种方法来恢复多个提交并尝试恢复一个范围:

git revert --no-edit <first_bad_commit-id>..<last_bad_commit-id>

But I am getting the error:但我收到错误:

error: commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx is a merge but no -m option was given.
fatal: revert failed

Found this post about what is -m and how to use it, but everywhere I read I just can't comprehend how to use -m and why can't I revert merges.找到了这篇关于什么是-m以及如何使用它的帖子,但无论我读到什么,我都无法理解如何使用-m以及为什么我不能恢复合并。 This is probably due to my minimal interaction with git command-line.这可能是由于我与 git 命令行的交互最少。

So, I have a range containing 200 commits I don't need that are sandwiched between commits I need.因此,我有一个包含 200 个我不需要的提交的范围,它们夹在我需要的提交之间。 How do I undo those 200 commits?我如何撤消这 200 次提交?

I'd also be satisfied with a solution of undoing all the latest commits up to a specific commit.我也对撤消所有最新提交到特定提交的解决方案感到满意。 It would be quick to re-add the code for bug fixes.重新添加错误修复代码会很快。

Are the commits consecutive?提交是连续的吗? If so, you can try using the method described by @timmouskhelichvili on the hackernoon website:https://hackernoon.com/how-to-delete-commits-from-remote-in-git , where you reset the head back to one or more commits.如果是这样,您可以尝试使用 hackernoon 网站上@timmouskhelichvili 描述的方法:https://hackernoon.com/how-to-delete-commits-from-remote-in-git ,您将 head 重置为 1或更多的承诺。 That is: git reset --hard HEAD~x (for x consecutive commits and can be changed depending on the number of commits to be removed).即:git reset --hard HEAD~x(针对 x 次连续提交,可以根据要删除的提交数进行更改)。

Otherwise, you can use the method that I used which is to remove the commits up to a specific hash value: git reset --hard [hash_value] (where the hash value can be found on the remote repository or local git logs).否则,您可以使用我使用的方法,即删除特定 hash 值之前的提交:git reset --hard [hash_value](其中 hash 值可以在远程存储库或本地 git 日志中找到)。

If the commits are not consecutive: you will need to use an interactive rebase, but I have not used that and will recommend you go check outhttps://hackernoon.com/how-to-delete-commits-from-remote-in-git for a better explanation.如果提交不是连续的:您将需要使用交互式变基,但我没有使用过,并会推荐您 go 查看https://hackernoon.com/how-to-delete-commits-from-remote-in -git以获得更好的解释。

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

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