# BUG: git subtree split - revert commit followed by a merge

2 messages from 2013-08-27 to 2013-08-28. Participants: Machiels, Brecht, Brecht Machiels.
Thread: https://gitlist.dev/t/34777

## Machiels, Brecht, 2013-08-27 13:09

Subject: BUG: git subtree split - revert commit followed by a merge
Message-ID: <E71F820BDC168F4B91A464A8FD8ABBD8389BD9C7@CH1PRD0310MB391.namprd03.prod.outlook.com>
URL: https://gitlist.dev/e/E71F820BDC168F4B91A464A8FD8ABBD8389BD9C7%40CH1PRD0310MB391.namprd03.prod.outlook.com

```
Hello:

I'm running into the problem described in this mailing list post:
	http://thread.gmane.org/gmane.comp.version-control.git/202645

 'git subtree split' fails to reconstruct the history when a revert commit is followed by a merge commit. I have slightly adjusted the test script provided by Fabien in his mailing list post:

    git init
    
    # create a directory that is going to be split
    mkdir doc
    echo "TEST" > doc/README
    git add doc
    # commit A
    git commit -a -m"first version"
    
    # create a branch with a new commit (Z)
    git checkout -b test
    echo "TEST" > doc/README1
    git add doc/README1
    git commit -a -m"added README1"
    git checkout master
    
    # modify the README file (commit B)
    echo "TEST_" > doc/README
    git commit -a -m"second version"
    
    # revert the change (commit C)
    echo "TEST" > doc/README
    git commit -a -m"revert second version"
    # or use git revert HEAD^
    
    # split
    git subtree split --prefix="doc" --branch=TARGET
    
    # add another commit (to a file *not* in the subtree dir)
    echo "BLA" > BLA
    git add BLA
    git commit -a -m"third version"
    
    # adding another commit to a file in the subtree dir will "fix" things
    #echo "MEH" > doc/MEH
    #git add doc
    #git commit -a -m"fourth version"

    # the log will show the 3 commits as expected (including B and C)
    GIT_PAGER= git log --oneline TARGET
    
    # merge the test branch
    git merge -m"merged test" test
    
    # attempt to re-split; this will fail
    git subtree split --prefix="doc" --branch=TARGET
    
    # see what history split generates
    git subtree split --prefix="doc" --branch=TARGET2

I have discovered that if the revert commit is followed by another commit that makes changes in the subtree directory, the split will work as expected (see "fourth version" above).

See also this related SO question where I ask for a workaround: http://stackoverflow.com/questions/18465867

Best regards,
Brecht

```

## Brecht Machiels, 2013-08-28 07:59

Subject: Re: BUG: git subtree split - revert commit followed by a merge
Message-ID: <op.w2ioheg99vfd47@bel1pc-0098.adcorp.kla-tencor.com>
URL: https://gitlist.dev/e/op.w2ioheg99vfd47%40bel1pc-0098.adcorp.kla-tencor.com
In-Reply-To: <E71F820BDC168F4B91A464A8FD8ABBD8389BD9C7@CH1PRD0310MB391.namprd03.prod.outlook.com>

```
Hi,

I think I understand what's going on. If there are no net changes in one  
of the two branches, the id of the merge commit will be the same as that  
of the last commit on the other branch (pass the -d option to 'git subtree  
split' to see the new commit id's of the split out commits). In that case,  
git-subtree will drop the merge commit. I believe this is done because for  
regular commits, this means that the commit does not make changes to files  
in the subtree directory. I'm not sure I'm right about this, as I would  
expect the merge commit's hash to depend on the hashes of its parent  
commits.

I was thinking the solution would be to simply adjust the copy_or_skip  
function to always copy merge commits. However, this may result in the  
produced commit history to differ from before (unnecessary merge commits  
were previously left out), leading to other problems.

A solution might be to check for the presence of such commit +  
revert-commit branches. This can be done by checking for each parent of a  
merge commit if it is the ancestor of any of the other parents (using git  
merge-base --is-ancestor).

Best regards,
Brecht

On Tue, 27 Aug 2013 15:09:30 +0200, Machiels, Brecht  
<Brecht.Machiels@kla-tencor.com> wrote:

> Hello:
>
> I'm running into the problem described in this mailing list post:
> 	http://thread.gmane.org/gmane.comp.version-control.git/202645
>
>  'git subtree split' fails to reconstruct the history when a revert  
> commit is followed by a merge commit. I have slightly adjusted the test  
> script provided by Fabien in his mailing list post:
>
>     git init
>    # create a directory that is going to be split
>     mkdir doc
>     echo "TEST" > doc/README
>     git add doc
>     # commit A
>     git commit -a -m"first version"
>    # create a branch with a new commit (Z)
>     git checkout -b test
>     echo "TEST" > doc/README1
>     git add doc/README1
>     git commit -a -m"added README1"
>     git checkout master
>    # modify the README file (commit B)
>     echo "TEST_" > doc/README
>     git commit -a -m"second version"
>    # revert the change (commit C)
>     echo "TEST" > doc/README
>     git commit -a -m"revert second version"
>     # or use git revert HEAD^
>    # split
>     git subtree split --prefix="doc" --branch=TARGET
>    # add another commit (to a file *not* in the subtree dir)
>     echo "BLA" > BLA
>     git add BLA
>     git commit -a -m"third version"
>    # adding another commit to a file in the subtree dir will "fix" things
>     #echo "MEH" > doc/MEH
>     #git add doc
>     #git commit -a -m"fourth version"
>
>     # the log will show the 3 commits as expected (including B and C)
>     GIT_PAGER= git log --oneline TARGET
>    # merge the test branch
>     git merge -m"merged test" test
>    # attempt to re-split; this will fail
>     git subtree split --prefix="doc" --branch=TARGET
>    # see what history split generates
>     git subtree split --prefix="doc" --branch=TARGET2
>
> I have discovered that if the revert commit is followed by another  
> commit that makes changes in the subtree directory, the split will work  
> as expected (see "fourth version" above).
>
> See also this related SO question where I ask for a workaround:  
> http://stackoverflow.com/questions/18465867

```
