git 通过拉取请求撤消合并?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6481575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:15:35  来源:igfitidea点击:

Undo a merge by pull request?

gitgithubmergepull-request

提问by Will

Someone accepted a pull request which they shouldn't have. Now we have a bunch of broken code merged in. How do you undo a pull request? I was just going to revert the changes to the commit just before the merge, but I noticed that it merged in a bunch of commits. So now there are all these commits from this person from days before the merge. How do you undo this?

有人接受了他们不应该接受的拉取请求。现在我们合并了一堆损坏的代码。你如何撤消拉取请求?我只是打算在合并之前将更改还原到提交,但我注意到它合并了一堆提交。所以现在有这个人在合并前几天的所有这些提交。你如何撤消这个?

采纳答案by errordeveloper

There is a better answerto this problem, though I could just break this down step-by-step.

这个问题有一个更好的答案,尽管我可以一步一步地分解它。

You will need to fetch and checkout the latest upstream changes like so, e.g.:

您需要像这样获取并签出最新的上游更改,例如:

git fetch upstream
git checkout upstream/master -b revert/john/foo_and_bar

Taking a look at the commit log, you should find something similar to this:

查看提交日志,您应该会发现类似以下内容:

commit b76a5f1f5d3b323679e466a1a1d5f93c8828b269
Merge: 9271e6e a507888
Author: Tim Tom <[email protected]>
Date:   Mon Apr 29 06:12:38 2013 -0700

    Merge pull request #123 from john/foo_and_bar

    Add foo and bar

commit a507888e9fcc9e08b658c0b25414d1aeb1eef45e
Author: John Doe <[email protected]>
Date:   Mon Apr 29 12:13:29 2013 +0000

    Add bar

commit 470ee0f407198057d5cb1d6427bb8371eab6157e
Author: John Doe <[email protected]>
Date:   Mon Apr 29 10:29:10 2013 +0000

    Add foo
commit b76a5f1f5d3b323679e466a1a1d5f93c8828b269
Merge: 9271e6e a507888
Author: Tim Tom <[email protected]>
Date:   Mon Apr 29 06:12:38 2013 -0700

    Merge pull request #123 from john/foo_and_bar

    Add foo and bar

commit a507888e9fcc9e08b658c0b25414d1aeb1eef45e
Author: John Doe <[email protected]>
Date:   Mon Apr 29 12:13:29 2013 +0000

    Add bar

commit 470ee0f407198057d5cb1d6427bb8371eab6157e
Author: John Doe <[email protected]>
Date:   Mon Apr 29 10:29:10 2013 +0000

    Add foo

Now you want to revert the entire pull request with the ability to unrevert later. To do so, you will need to take the ID of the merge commit.

现在您想要恢复整个拉取请求,并具有稍后取消恢复的能力。为此,您需要获取合并提交的 ID 。

In the above example the merge commitis the top one where it says "Merged pull request #123...".

在上面的例子中,合并提交是最上面的,它说“合并拉取请求 #123...”

Do this to revert the both changes ("Add bar"and "Add foo") and you will end up with in one commit reverting the entire pull request which you can unrevert later on and keep the history of changes clean:

执行此操作以还原两个更改("Add bar""Add foo"),您最终将在一次提交中还原整个拉取请求,您可以稍后取消还原并保持更改历史记录干净:

git revert -m 1 b76a5f1f5d3b323679e466a1a1d5f93c8828b269

回答by Pa?lo Ebermann

Look at your commit graph (with gitk or a similar program). You will see commits from the pull request, and you will see your own commits, and a merge commit (if it was not a fast-forward merge). You just have to find the last of your own commits before the merge, and reset the branch to this commit.

查看您的提交图(使用 gitk 或类似程序)。您将看到来自拉取请求的提交,您将看到自己的提交和合并提交(如果它不是快进合并)。你只需要在合并之前找到你自己的最后一次提交,并将分支重置为这个提交。

(If you have the branch's reflog, it should be even easier to find the commit before the merge.)

(如果你有分支的 reflog,在合并之前找到提交应该更容易。)



(Edit after more information in comments:)

(在评论中的更多信息后编辑:)

Okay, lets look at the graph:

好的,让我们看一下图表:

screenshot 1

截图 1

I assume the last (rightmost) commit was your wrong merge by pull request, which merged the blue line seen here. Your last goodcommit would be the one before on the black line, here marked in red:

我假设最后一次(最右侧)提交是您通过 pull request进行的错误合并,它合并了此处看到的蓝线。你最后一次好的提交将是黑线上的前一次,这里用红色标记:

enter image description here

在此处输入图片说明

Reset to this commit, and you should be fine.

重置到这个提交,你应该没问题。

This means, in your local working copy do this (after making sure you have no more uncommitted stuff, for example by git stash):

这意味着,在您的本地工作副本中执行此操作(在确保您没有更多未提交的内容之后,例如通过 git stash):

git checkout master
git reset --hard 7a62674ba3df0853c63539175197a16122a739ef
gitk 

Now confirm that you are really on the commit I marked there, and you will see none of the pulled stuff in its ancestry.

现在确认您确实在我在那里标记的提交上,并且您将看不到其祖先中的任何拉取内容。

git push -f origin master

(if your github remote is named origin- else change the name).

(如果您的 github 遥控器已命名origin- 否则更改名称)。

Now everything should look right on github, too. The commits will still be in your repository, but not reachable by any branch, thus should not do any harm there. (And they will be still on RogerPaladin's repository, of course.)

现在一切都应该在 github 上看起来也正确。提交仍将在您的存储库中,但任何分支都无法访问,因此不应在那里造成任何伤害。(当然,它们仍将保留在 RogerPaladin 的存储库中。)

(There might be a Github specific web-only way of doing the same thing, but I'm not too familiar with Github and its pull request managing system.)

(可能有一种 Github 特定的 web-only 方式来做同样的事情,但我对 Github 及其拉取请求管理系统不太熟悉。)

Notethat if anyone else already might have pulled your master with the wrong commit, they then have the same problem as you currently have, and can't really contribute back. before resetting to your new master version.

请注意,如果其他人可能已经以错误的提交拉取了您的主人,那么他们就会遇到与您目前相同的问题,并且无法真正做出回馈。在重置为新的主版本之前。

If it is likely that this happened, or you simply want to avoid any problems, use the git revertcommand instead of git reset, to revert the changes with a new commit, instead of setting back to an older one. (Some people think you should never do reset with published branches.) See other answers to this question on how to do this.

如果这很可能发生,或者您只是想避免任何问题,请使用git revert命令而不是git reset, 以通过新提交还原更改,而不是设置回旧提交。(有些人认为您永远不应该对已发布的分支进行重置。)请参阅此问题的其他答案,了解如何执行此操作。

For the future:

为将来:

If you want only some of the commits of RogerPaladin's branch, consider using cherry-pickinstead of merge. Or communicate to RogerPaladin to move them to a separate branch and send a new pull request.

如果您只想要 RogerPaladin 分支的一些提交,请考虑使用cherry-pick而不是merge. 或者与 RogerPaladin 通信以将它们移动到单独的分支并发送新的拉取请求。

回答by samthebest

If the pull was the last thing he did then

如果拉是他做的最后一件事,那么

git reset --hard HEAD~1

回答by VonC

Starting June 24th, 2014, you can try cancel a PR easily (See "Reverting a pull request") with:

从 2014 年 6 月 24 日开始,您可以尝试通过以下方式轻松取消 PR(请参阅“恢复拉取请求”):

Introducing the Revert Button

介绍还原按钮

you can easily revert a pull request on GitHub by clicking Revert:

您可以通过单击 Revert 在 GitHub 上轻松地恢复拉取请求:

https://camo.githubusercontent.com/0d3350caf2bb1cba53123ffeafc00ca702b1b164/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6c696e6b2e706e67

https://camo.githubusercontent.com/0d3350caf2bb1cba53123ffeafc00ca702b1b164/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6c696e6b2e706e67

You'll be prompted to create a new pull request with the reverted changes:

系统将提示您使用已还原的更改创建新的拉取请求:

https://camo.githubusercontent.com/973efae3cc2764fc1353885a6a45b9a518d9b78b/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6e65772d70722e706e67

https://camo.githubusercontent.com/973efae3cc2764fc1353885a6a45b9a518d9b78b/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6e65772d70722e706e67

It remains to be tested though if that revert uses -mor not (for reverting merges as well)

尽管该恢复是否使用-m(也用于恢复合并),但仍有待测试

But Adil H Razaadds in the comments (Dec. 2019):

Adil H Raza在评论中补充道(2019 年 12 月):

That is the expected behavior, it created a new branch and you can create a PR from this new branch to your master.
This way in the future you can un-revert the revert if need be, it is safest option and not directly changing your master.

这是预期的行为,它创建了一个新分支,您可以从这个新分支创建一个 PR 到您的master.
这样,将来您可以在需要时取消还原还原,这是最安全的选择,而不是直接更改您的master.



Warning: Korayempoints out in the commentsthat:

警告Korayem指出在评论中说:

After a revert, let's say you did some further changes on Git branch and created a new PR from same source/destination branch.
You will find the PR showing only new changes, but nothing of what was there before reverting.

恢复后,假设您在 Git 分支上做了一些进一步的更改,并从相同的源/目标分支创建了一个新的 PR。
您会发现 PR 仅显示新的更改,但不显示还原之前的内容

Korayem refers us to "Github: Changes ignored after revert (git cherry-pick, git rebase)" for more.

Korayem 将我们推荐给“ Github:在还原 ( git cherry-pick, git rebase)后忽略的更改”了解更多信息。

回答by FluffySamurai

To undo a github pull request with commits throughout that you do not want to delete, you have to run a:

要撤消包含您不想删除的整个提交的 github 拉取请求,您必须运行:

git reset --hard --merge <commit hash>

git reset --hard --merge <commit hash>

with the commit hash being the commit PRIOR to merging the pull request. This will remove all commits from the pull request without influencing any commits within the history.

提交哈希是合并拉取请求之前的提交。这将从拉取请求中删除所有提交,而不会影响历史记录中的任何提交。

A good way to find this is to go to the now closed pull request and finding this field:

找到它的一个好方法是转到现已关闭的拉取请求并找到此字段:

Pull Request ImagePull Request Image

拉取请求图像拉取请求图像

After you run the git reset, run a:

运行后git reset,运行:

git push origin --force <branch name>

git push origin --force <branch name>

This should revert the branch back before the pull request WITHOUT affecting any commits in the branch peppered into the commit history between commits from the pull request.

这应该在拉取请求之前恢复分支,而不会影响分支中的任何提交,这些提交会在拉取请求的提交之间插入提交历史记录。

EDIT:

编辑:

If you were to click the revert button on the pull request, this creates an additional commit on the branch. It DOES NOT uncommit or unmerge. This means that if you were to hit the revert button, you cannot open a new pull request to re-add all of this code.

如果您要单击拉取请求上的还原按钮,则会在分支上创建一个额外的提交。它不会取消提交或取消合并。这意味着,如果您要点击还原按钮,则无法打开新的拉取请求来重新添加所有这些代码。

回答by Mike Marshall

I use this place all the time, thanks.

我一直在使用这个地方,谢谢。

I was searching for how to undo a pull request and got here.

我正在寻找如何撤消拉取请求并来到这里。

I was about to just git reset --hardto "a long time ago" and do a fast forward back to where I was before doing the pull request.

我正要git reset --hard“很久以前”并快进回到我在执行拉取请求之前的位置。

Besides looking here, I also asked my co-worker what he would do, and he had a typically good answer: using the example output in the first answer above:

除了看这里,我还问我的同事他会做什么,他有一个典型的好答案:使用上面第一个答案中的示例输出:

git reset --hard 9271e6e

As with most things in Git, if you're doing it some way that's not easy, you're probably doing it wrong.

与 Git 中的大多数事情一样,如果您以某种并不容易的方式进行操作,那么您可能做错了。