2

假设我有一个带有两个分支的 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..."and的顺序echo "This should also..."不是 Git 可以弄清楚的,所以这里会有一些手动决策。但我想知道如何以最“Git”的方式做到这一点,并且涉及来自像 Tower 这样的程序的最少数量的错误消息。

4

3 回答 3

1

所以按照我的理解,您有 2 个更改custom,其中只有一个需要合并到main中,而其中的一个更改main需要合并到custom. 您可以通过以下方式解决您的问题:

  • 将自定义倒回到 2 个更改之前
  • custom添加您要合并的一项更改main
  • 隐藏第二个更改
  • 将一个提交从customintomain合并,解决合并冲突
  • 将更改合并maincustom
  • 将隐藏的更改弹出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”将为提交准备一个块,“n”将跳过它。如果“s”不起作用这很神奇,点击“e”并手动编辑大块。 更多信息

一旦上演,

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

这很丑……但它有效。

于 2014-08-08T06:38:16.130 回答
0

你可以做一个

git checkout -p master -- print.php

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

阅读更多..

于 2014-08-07T19:04:36.517 回答
0

如果我正确理解您的问题,您应该能够简单地执行以下操作:

git checkout custom
git merge master

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

于 2014-08-07T19:20:01.790 回答