简体   繁体   English

如何将合并提交还原为之后的新提交?

[英]How to revert a merge commit with a newer commit after it?

I already know how to revert a merge commit including selecting parents.我已经知道如何恢复合并提交,包括选择父母。
But now I'm facing a different situation where there is a new commit after the merge commit.但是现在我面临一种不同的情况,即在合并提交之后有一个新的提交。

Looking at:看着:

在此处输入图像描述

You can see that after merge commit "2", there is a new commit "1".可以看到在合并提交“2”之后,有一个新的提交“1”。
All I want is to revert the merging of "3" to the branch master.我想要的只是将“3”的合并恢复到分支主控。 ( I know that "2" is a merge commit). (我知道“2”是一个合并提交)。

Question:问题:

How can I revert the merge commit with the red arrow?如何使用红色箭头还原合并提交? I've numbered the relative commits so it would be easier to ref in an answer.我已经对相对提交进行了编号,因此更容易在答案中引用。

I tend to use Tower as my git client here you can see me directly reverting an older commit .我倾向于使用 Tower 作为我的 git 客户端,在这里你可以看到我直接还原了一个较旧的提交

Without rewriting history:不重写历史:

First revert the thing you want to get rid of locally:首先在本地恢复你想要摆脱的东西:

 git revert sha-of-2 -m X

Where X is the parent number (likely 1 in this case).其中 X 是父编号(在这种情况下可能为 1)。 See here for details .请参阅此处了解详细信息

Resolve any merge issues that come from that.解决由此产生的任何合并问题。 Resolving these potential conflicts will make sure that 1 is applied correctly without having to revert it first.解决这些潜在冲突将确保正确应用1而不必先恢复它。 then commit and push.然后提交并推送。

The result will be:结果将是:

-- 4 -- 2 -- 1 -- '2 <- master
        /
-- 3 --/

where '2 is the inverse of 2 .其中'22的倒数。

With rewriting history:重写历史:

WARNING : force pushing to master after rewriting history may have severe negative impact on other users of the same branch.警告:重写历史记录后强制推送到 master 可能会对同一分支的其他用户产生严重的负面影响。

Do an interactive rebase:做一个交互式变基:

 git rebase -i sha-of-2

drop the merge commit, keep commit 1, then force push master.删除合并提交,保留提交 1,然后强制推送 master。

The result will be:结果将是:

-- 4 -- '1 <- master

-- 3

Where '1 is a new commit with the original changes of 1 plus any conflicts you had resolved.其中'1是新提交,原始更改为1加上您已解决的任何冲突。

Rewriting history using a new branch使用新分支重写历史

WARNING : force pushing to master after rewriting history may have severe negative impact on other users of the same branch.警告:重写历史记录后强制推送到 master 可能会对同一分支的其他用户产生严重的负面影响。

This may be the easier to understand.这可能更容易理解。 It does the exact same thing as the rebase example able, but instead of applying rebase-fairy-dust, you do the hard work yourself.它与 rebase 示例完全一样,但不是应用 rebase-fairy-dust,而是您自己做艰苦的工作。

Instead of rebasing you could re-do these changes yourself.您可以自己重新进行这些更改,而不是重新设置基准。 Reset to 4 , create a new branch, cherry pick 1 , reset master to '1 and force push.重置为4 ,创建一个新分支,cherry pick 1 ,将 master 重置为'1并强制推送。

     /- '1 <- master
    /
-- 4 -- 2 -- 1
        /
-- 3 --/

git reset --hard sha-of-4
git checkout -B newmaster
git cherry-pick sha-of-1
# fix conflicts if any
git checkout master
git reset master --hard newmaster
git push --force

Where '1 is the cherry-picked version of 1 .其中'11的精选版本。

At the command line it should be as simple as git revert 2 , though I know sometimes reverting merges can be difficult.在命令行中,它应该像git revert 2一样简单,尽管我知道有时恢复合并可能很困难。

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

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