当我从另一个分支中挑选提交,然后尝试合并它。如果已应用更多更改,则会发生合并冲突。
一个很好的例子,如果我遵循'gitflow'的方式来做git(master,develop,feature / bugfix,hotfix)。这是步骤:
bugfix
合并到develop
develop
中bugfix
并将它作为hotfix
应用到master
master
合并回develop
,我会发生合并冲突。bugfix
合并提交,然后合并master
(从而创建这个“中间”合并),然后合并这个“中间”合并到develop
一切正常,没有任何冲突。如果之后没有发生任何变化,git会如何计算出樱桃选择。但如果添加更多更改,那就是git的世界末日。
这是我的问题的ascii模式C'是挑选的C:
(键:✔=没有冲突,✘=冲突)
+-------------------->✘ <--+
| |
| |
| ✔<--------+ |
| ^ | |
| | +-+--+
| | | M2 +<-----+
| | +-+--+ |
| + ^ +-+-+
| +----------->✔<--------+ | | D |
| | | | +-+-+
+-+-++ +-+--+ ^
| Ma +<----+ +---->+ M1 +------+
+-+--+ | | +-^--+
^ +-+--+ +-+-+ |
| | C' | | C | |
| +-+--+ +-+-+ |
| ^ ^ |
| | | |
| +-+--+ +-+-+ |
| | B' | | B | |
| +-+--+ +-+-+ |
| ^ ^ |
+-+--+ | | |
| A +-----+---------+-------+
+-+--+ | | |
| | | |
| | | |
+ + + +
Master Cherry- Bugfix Develop
Picked
Hotfix
这是我创建这种情况的脚本:
git init
echo -e "foo\nbar\nbaz\nqux\nquux" > file
git add file
git commit -m "Initial commit"
git checkout -b develop
git branch second-feature
git checkout -b first-feature
echo -e "foo1\nbar\nbaz\nqux\nquux" > file
git commit -m "First feature" file
git checkout develop
git merge --no-ff -m "Merge first feature" first-feature
git checkout second-feature
echo -e "foo\nbar\nbaz2\nqux\nquux" > file
git commit -m "Second feature" file
git checkout develop
git merge --no-ff -m "Merge second feature" second-feature
git tag before-bugfix
git checkout -b bugfix
echo -e "foo1\nbar\nbaz2\nqux\nquux1" > file
git commit -m "Bugfix (part I)" file
echo -e "foo1\nbar\nbaz2\nqux\nquux2" > file
git commit -m "Bugfix (part II)" file
git checkout develop
git merge --no-ff -m "Merge bugfix" bugfix
git checkout -b third-feature
echo -e "foo1\nbar\baz2\nqux\nquux3" > file
git commit -m "Third feature" file
git checkout develop
git merge --no-ff -m "Third feature" third-feature
git checkout master
git cherry-pick -x before-bugfix..bugfix
如果我这样做,我得到:
$ git checkout develop
$ git merge --no-ff master
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
如果我这样做,我得到:
$ git checkout -b new-master bugfix
$ git merge --no-ff -m "In-between Merge" master
Merge made by the 'recursive' strategy.
$ git merge --no-ff -m "Merge master" new-master
Already up-to-date!
Merge made by the 'recursive' strategy.
有没有办法在一次合并中解决这个问题而没有任何冲突?
我尝试了不同的合并策略,但它没有用。我试图将master和bugfix同时合并到开发中,我仍然会遇到冲突。
为了做你期望的事情,git必须跟踪合并分支的整个历史记录,以识别等效的提交并跳过它。它不会这样做,它只使用历史上的3个提交:父母和“合并基础” - 父母的共同祖先。
你可以这样指导,如果你愿意的话:
git checkout -b master_with_develop master
develop
develop
develop
并合并master_with_develop
- 它将是快进的(您也可以将其标记为--no-ff
以创建显式合并提交)您可以检查git-imerge工具,它自动尝试执行上述步骤