仅合并从一个git分支到另一个git分支的某些更改

时间:2014-08-07 18:48:16

标签: git git-tower

假设我有一个包含两个分支的git存储库 - maincustom。假设我在custom工作,并对文件print.php进行了两处更改:

/* begin custom only change */
echo "I'm only printed in the custom branch version";
/* end custom only change */

echo "I want this printed in both branches";

同时说,我已经提取了一个新的main分支副本,它对print.php进行了以下更改:

echo "This should also be printed in both branches";

我应该怎样做 - 使用命令行或使用像Tower这样的git程序(甚至是制作自定义脚本的建议) - 以获得以下结果文件?

主分支中的

print.php

...
echo "I want this printed in both branches";
echo "This should also be printed in both branches";
...

自定义分支中的print.php

...
/* begin custom only change */
echo "I'm only printed in the custom branch version";
/* end custom only change */

echo "I want this printed in both branches";
echo "This should also be printed in both branches";
...

显然,echo "I want this..."echo "This should also..."的顺序不是Git可以解决的问题,所以这里会做一些手动决策。但我想知道如何在大多数" Git"可能的方式,涉及来自Tower等程序的最少数量的错误消息。

3 个答案:

答案 0 :(得分:1)

所以我理解它的方式,custom中有2项更改,其中只有一项需要合并到main,而main中的一项更改需要合并进入custom。您可以通过这种方式解决问题:

  • 在2次更改之前将自定义撤回到
  • 将一个更改添加到您要合并到custom
  • main
  • 存储第二次更改
  • custom中的一个提交合并到main,解决合并冲突
  • main的更改合并到custom
  • custom中弹出隐藏的更改,然后将其提交

如果您已经在自定义中提交了2个更改,则您需要先执行git reset --soft [hash-of-the-commit-before-new-changes](您可以使用git log找到正确提交的哈希值)。否则,请按以下步骤操作。

您可以在交互式git add模式中逐行提交:

git add -p print.php

在此模式下,首先自行暂存此更改:

echo "I want this printed in both branches";

(一旦进入交互模式,如果你点击" s" git会尝试将提交帅哥分成更小的帅哥。" y"将会提交一个大块用于提交," 34; n"将跳过它。如果" s"没有工作它的魔法,点击" e"并手动编辑大块。{{3 }})

一旦上演,

git commit
git stash            # stash unstaged, uncommitted change in custom for later

然后,

git checkout main    # get in main branch
git merge custom     # merge custom, you'll probably get a merge conflict

如果/当您遇到合并冲突时:

git mergetool

我能告诉git保留文件的两个版本中的行,并选择向左或向右显示在文件中。

git commit           # commit this change to main
git checkout custom  # get in custom branch
git merge main       # merge main

然后,

git stash pop        # pop that other change to custom out of the stash
git add print.php    # stage it for commit
git commit           # commit that second change to custom

它很难看......但它确实有效。

答案 1 :(得分:0)

你可以做一个

git checkout -p master -- print.php

来自您的自定义分支,您可以修补文件的某些部分。

Read more..

答案 2 :(得分:0)

如果我理解你的问题,你应该能够做到:

git checkout custom
git merge master

这实际上会创建一个自动合并。我在https://gist.github.com/jordansamuels/8cb8ade97fdaf232d26c做了一个回购(有一个小错字)。