git octopus合并'默默'不合并一些分支

时间:2013-09-10 08:47:58

标签: git merge octopus

我有一些意想不到的结果似乎是基于执行章鱼合并时给予git merge的分支的顺序。

以下脚本将复制我的情况

#!/usr/bin/env bash

rm -rf TEMP

# create area to work in
mkdir TEMP
cd TEMP/
git init

# work on master
echo "hello world" > file
git add file 
git commit -m "initial commit"

# create branch which is at the same point as master
git branch hasnt_moved

# create another branch 1 commit ahead
git checkout -b feature_branch_with_work
echo "some interesting work" > other_file
git add other_file 
git commit -m "work on feature branch"

git checkout master

# always should also have some work ahead of master
git checkout -b always
echo "some work on always" > other_other_file
git add other_other_file 
git commit -m "work on always branch"

# back to master
git checkout master

# now try a merge
#git merge --no-edit --strategy=octopus always hasnt_moved feature_branch_with_work

# however if I had ran this then it would be merged in
#git merge --no-edit --strategy=octopus hasnt_moved always feature_branch_with_work

# I would also expect this to give the same resutls
#git merge --no-edit --strategy=octopus always feature_branch_with_work hasnt_moved

这是运行上面脚本后我的git树的状态 branches before any merge

你可以在末尾看到3个注释掉的行,下面是每一行和在树上运行该命令得到的图形(通过gitk --all生成),在运行每个命令后你可以'返回'到通过git reset --hard hasnt_moved

开始

以下2个案例符合预期(注意分支参数的排序)

git merge --no-edit --strategy=octopus hasnt_moved always feature_branch_with_work

expected results form merge

git merge --no-edit --strategy=octopus always feature_branch_with_work hasnt_moved

expected results form merge

这最后一种情况会产生意想不到的结果,因为always分支不包含在最终合并的主分支中(注意分支参数的排序)。

git merge --no-edit --strategy=octopus always hasnt_moved feature_branch_with_work

actual results from merge

如果我逐个运行合并,我会得到预期的结果:

git merge --no-edit --strategy=octopus always
git merge --no-edit --strategy=octopus hastn_moved
git merge --no-edit --strategy=octopus feature_branch_with_work

manual results given expected results

我想知道为什么给octopus merge的提交顺序有时会导致always分支不能合并到master。

1 个答案:

答案 0 :(得分:0)

看起来你绊倒了遗留语法。事实证明,git merge曾经有过以下形式:

git merge <msg> HEAD <commit>...

由于遗留原因,它仍然受到支持。你的合并命令:

git merge --no-edit always hasnt_moved feature_branch_with_work

意外地绊倒了它。在这种情况下,always被视为消息,hasnt_moved与HEAD处于同一版本。因此,git将此命令视为:

git merge --no-edit -m "always" feature_branch_with_work

我不确定git的前一种语法,但在我看来,如果它希望看到实际的单词HEAD,那么在放弃这种语法之前git应该更严格一些。但是,我怀疑是这样的。

您可以通过明确提供带有-m参数的消息来解决此问题:

git merge --no-edit -m "Merge always, hasnt_moved, and feature_branch_with_work"
    \ always hasnt_moved feature_branch_with_work

然后章鱼合并按预期发生。

顺便说一句,您可以在git-merge man page上看到git merge的旧语法。