Git:如何“撤消”合并

发布于 2024-10-07 10:25:42 字数 573 浏览 0 评论 0原文

情况: 从 A 的 Master 开始,我在 B 进行分支并进行了一些更改,然后将该分支合并回 (C)。在进行更多更改后,我在 D 处,但发现我需要部署代码而不需要分支中发生的更改。如果我不手工合并它就好了。

A_______C___D
 \     /
  \_B_/

首先,我想知道我应该从这里做什么来部署代码,就好像合并从未发生过一样。注意:在分支中编辑的相同文件都没有在主版本中编辑过。

其次......

我没有时间找出处理这个问题的最佳方法,所以我删除了分支添加的文件,并手动恢复了分支中所做的一些编辑,然后提交了部署结果(<代码>F)

A_______C___D___F
 \     /
  \_B_/

我希望能够继续开发分支并将来自主分支的任何更改合并到其中以使其保持最新状态,但如果我这样做,我所做的就是创建 F 将被合并并导致文件被删除并恢复编辑。处理这个问题的最佳方法是什么?

The situation:
Starting with the Master at A I branched and made some changes at B then merged that branch back in (C). After making some more changes I was at D but found I needed to deploy the code without the changes that happened in the branch. If I hand't merged it that would have been fine.

A_______C___D
 \     /
  \_B_/

Firstly I'd like to know what I should have done from here to deploy the code as if the merge never happened. Note: none of the same files that were edited in the branch have been edited in the master.

Secondly...

I didn't have time to work out the best method to deal with this, so I deleted the files which the branch added, and manually reverted a few edits made in the branch then commited the result for deployment (F)

A_______C___D___F
 \     /
  \_B_/

I want to be able to keep developing the branch and merge any changes from the master into it to keep it up to date, but if I do that the stuff I did to create F will be merged in and cause the files to be deleted and the edits to be reverted. What is the best way to deal with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2024-10-14 10:25:42

您可以使用 rebase 一步完成此操作:

git rebase --onto A C D

我刚刚对此进行了测试,并得到了适当的结果:

$ edit test.txt
$ git add .
$ git commit -mA
$ git checkout -b the_branch
$ edit test.txt
$ git commit -a -mB
$ git checkout master
$ git merge master the_branch --no-ff
$ edit test.txt
$ git commit -a -mD

从这里您将看到您所描述的情况。
然后:

$ git rebase --onto <SHA1-for-A> <SHA1-for-C> master

将 C(排除)的提交变基到 master,再到 A。
我需要解决一些冲突,因为我在 B 和 D 中的相同位置进行了修改,但我认为您不会。

   _D'
  /
 /
A_______C___D
 \     /
  \_B_/

关于 git rebase --onto 的文档,这或多或少是您的情况:
http://git-scm.com/docs/git-rebase


如果您有:

A_______C___D___F
 \     /
  \_B_/

,那么您现在就拥有了:

   _D'___F'_(master)
  /
 /
A_______C___D___F
 \     /
  \_B_/(the_branch)

从这里开始,将 master 中的更改合并到分支中很容易。完全放弃提交F'

$ git checkout master # if you were not here already
$ git branch old_fix  # if you want to be able to return to F' later
$ git reset --hard <SHA1-to-D'>

在执行上述命令之后,您将执行以下操作:

     (master)
    /
   _D'___F'_(old_fix)
  /
 /
A_______C___D___F
 \     /
  \_B_/(the_branch)

将 master 的更新合并到 the_branch:

$ git checkout the_branch
$ git merge master

... 并修复冲突。

You can use rebase to do that in one step:

git rebase --onto A C D

I just tested this, with appropriate results:

$ edit test.txt
$ git add .
$ git commit -mA
$ git checkout -b the_branch
$ edit test.txt
$ git commit -a -mB
$ git checkout master
$ git merge master the_branch --no-ff
$ edit test.txt
$ git commit -a -mD

From here you have the situation you described.
Then:

$ git rebase --onto <SHA1-for-A> <SHA1-for-C> master

rebases commits from C (excluded) to master, onto A.
I needed to fix some conflicts since I modified at the same places in B and D, but I think you won't.

   _D'
  /
 /
A_______C___D
 \     /
  \_B_/

Doc about git rebase --onto, which is more or less your situation:
http://git-scm.com/docs/git-rebase


If you had:

A_______C___D___F
 \     /
  \_B_/

, then you have now:

   _D'___F'_(master)
  /
 /
A_______C___D___F
 \     /
  \_B_/(the_branch)

From here, merging the changes in master into the branch is easy. Discard the commit F' altogether.

$ git checkout master # if you were not here already
$ git branch old_fix  # if you want to be able to return to F' later
$ git reset --hard <SHA1-to-D'>

After the commands above you have:

     (master)
    /
   _D'___F'_(old_fix)
  /
 /
A_______C___D___F
 \     /
  \_B_/(the_branch)

To merge updates of master into the_branch:

$ git checkout the_branch
$ git merge master

... and fix the conflicts.

还在原地等你 2024-10-14 10:25:42

显而易见的解决方案是重置为 A,手动重新应用所有补丁并解决冲突(您不会遇到这种情况)。

或者,您可以只 git revert patch B 但这将创建一个新的提交。

虽然Gauthier的答案更好。

The obvious solution is to reset to A, reapply all patches manually and resolve conflicts (which you won't have).

Alternatively you can just git revert patch B but that will create a new commit.

Although Gauthier's answer is better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文