git rebase interactive: squash merge commits together

后端 未结 4 1118
灰色年华
灰色年华 2020-12-12 13:53

I wanted to have a simple solution to squash two merge commits together during an interactive rebase.

My repository looks like:

   X --- Y ---------          


        
相关标签:
4条回答
  • 2020-12-12 14:14

    I came to this topic wanting to squash a single merge commit; so my answer is not that useful to the original question.

                   X
                    \   
                     \  
    a --- b --- c --- M1 (subtree merge)
    

    What I wanted was to rebase the M1 merge and squash everything as a single commit on top of b.

    a --- b --- S (include the changes from c, X and M1)
    

    I tried all kinds of different combinations but this is what worked:

    git checkout -b rebase b (checkout a working branch at point b)
    git merge --squash M1
    

    This will apply the changes into the index where they can be committed git commit

    0 讨论(0)
  • 2020-12-12 14:16

    None of the mentioned methods works for me with a recent git version. In my case the following did the trick:

    git reset --soft Y
    git reset --hard $(git commit-tree $(git write-tree) -p HEAD -p stable < commit_msg)
    

    You'll have to write the commit message to the file commit_msg first, though.

    0 讨论(0)
  • 2020-12-12 14:33

    if you haven't published the last two merge commits, you could do a reset and a simple merge.

    git reset --hard Y
    git merge stable
    
    0 讨论(0)
  • 2020-12-12 14:35

    This is an old topic, but I just ran across it while looking for similar information.

    A trick similar to the one described in Subtree octopus merge is a really good solution to this type of problem:

    git checkout my-feature
    git reset --soft Y
    git rev-parse f > .git/MERGE_HEAD
    git commit
    

    That will take the index as it exists at the tip of my-feature, and use it to create a new commit off of Y, with 'f' as a second parent. The result is the same as if you'd never performed M1, but gone straight to performing M2.

    0 讨论(0)
提交回复
热议问题