假设我有一个包含两个分支的git存储库 - main
和custom
。假设我在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等程序的最少数量的错误消息。
答案 0 :(得分:1)
所以我理解它的方式,custom
中有2项更改,其中只有一项需要合并到main
,而main
中的一项更改需要合并进入custom
。您可以通过这种方式解决问题:
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)
答案 2 :(得分:0)
如果我理解你的问题,你应该能够做到:
git checkout custom
git merge master
这实际上会创建一个自动合并。我在https://gist.github.com/jordansamuels/8cb8ade97fdaf232d26c做了一个回购(有一个小错字)。