git rebase"--preserve-merges --onto"不保留合并

Rav*_*ole 8 git rebase

使用git v1.7.1我正在尝试同时使用--preserve-merges--onto功能进行rebase .最终结果似乎没有合并提交,因此看起来是线性的.我宁愿保留合并提交,原因与人们经常使用的相同--preserve-merges(更容易看到逻辑上属于单独功能并在其自己的分支中开发的提交组).

我的主分支(rebase的目的地)很无聊:

A-B-C

我想要的功能分支有一个已合并到其中的子功能分支.喜欢:

     X - Y
   /      \
V-W ------ Z
Run Code Online (Sandbox Code Playgroud)

其中Z是合并提交,它是要从中获取的要素分支的头部,并且X和Y位于子要素分支上.

我正在使用: git rebase --preserve-merges --onto C V Z

我想最终得到:

         X - Y
        /     \
A-B-C-W ------ Z
Run Code Online (Sandbox Code Playgroud)

但相反,我得到:

A-B-C-W-X-Y
Run Code Online (Sandbox Code Playgroud)

由于Z是一个无冲突的合并,代码的最终状态是正确的,但历史并不像我想的那样具有表现力.

有没有办法得到我想要的东西?

编辑地址@Bombe:我写了一个bash脚本来构建我的例子.在我的系统(RHEL 6.2 with git 1.7.1)上,这证明了我的问题.

#! /bin/bash
# start a new empty repo
git init
# make some commits on the master branch
git checkout master
touch A.txt; git add A.txt; git commit -m "add A.txt"; git tag Atag
touch B.txt; git add B.txt; git commit -m "add B.txt"; git tag Btag
touch C.txt; git add C.txt; git commit -m "add C.txt"; git tag Ctag
# now build the feature branch
# start at Btag (more or less arbitrary; point is it's before C)
git checkout Btag
git checkout -b feature
touch V.txt; git add V.txt; git commit -m "add V.txt"; git tag Vtag
touch W.txt; git add W.txt; git commit -m "add W.txt"; git tag Wtag
# now a subfeature
git checkout -b subfeature
touch X.txt; git add X.txt; git commit -m "add X.txt"; git tag Xtag
touch Y.txt; git add Y.txt; git commit -m "add Y.txt"; git tag Ytag
# merge the subfeature into the feature
# preserves branch history with --no-ff
git checkout feature
git merge --no-ff subfeature
# the merge commit is our Z
git tag Ztag
# one more commit so that merge isn't the tip (for better illustration of Z missing later)
touch postZ.txt; git add postZ.txt; git commit -m "add postZ.txt"; git tag postZtag
# now do the rebase
git rebase --preserve-merges --onto Ctag Vtag
# optionally move the master branch forward to the top of feature branch
git checkout master
git merge feature
Run Code Online (Sandbox Code Playgroud)

在变调之前,我得到:

        X-Y
       /   \
    V-W-----Z-postZ
   / 
A-B-C
Run Code Online (Sandbox Code Playgroud)

在变形后,我得到:

        X-Y
       /   \
    V-W-----Z-postZ
   / 
A-B-C-W'-X'-Y'-postZ'
Run Code Online (Sandbox Code Playgroud)

注意Y'和postZ'之间缺少Z'.

Rav*_*ole 12

非常感谢Bombe和一位离线朋友指出这对某些人有用.有了这个灵感,我现在能够回答我自己的问题.

简短的回答:1.7.5.2之前的git版本会表现出这种不良行为.

答案很长:在git自己的源代码库中,2011年4月28日提交c192f9c865dbdae48c0400d717581d34cd315fb8明确解决了这个问题.

引用提交消息(作者Andrew Wong):

git-rebase--interactive.sh: preserve-merges fails on merges created with no-ff

'git rebase' uses 'git merge' to preserve merges (-p).  This preserves
the original merge commit correctly, except when the original merge
commit was created by 'git merge --no-ff'.  In this case, 'git rebase'
will fail to preserve the merge, because during 'git rebase', 'git
merge' will simply fast-forward and skip the commit.  For example:

               B
              / \
             A---M
            /
    ---o---O---P---Q

If we try to rebase M onto P, we lose the merge commit and this happens:

                 A---B
                /
    ---o---O---P---Q

To correct this, we simply do a "no fast-forward" on all merge commits
when rebasing.  Since by the time we decided to do a 'git merge' inside
'git rebase', it means there was a merge originally, so 'git merge'
should always create a merge commit regardless of what the merge
branches look like. This way, when rebase M onto P from the above
example, we get:

                   B
                  / \
                 A---M
                /
    ---o---O---P---Q
Run Code Online (Sandbox Code Playgroud)

解决方案:获取新版本的git,如果需要,可以从源代码构建.

顺便说一下,我曾经想过git bisect这个.很棒的工具.