Search code examples
gitgithubrebasecherry-pick

Git rebase fails because: Commit xxxx is a merge but no -m option was given


I had a feature branch:

feature

with lets say 10 commits

then some time ago I started doing experiments on it, but wanted to preserve the current functionality just in case, so I started a new branch:

feature-experiment

and then did another 10 commits

today I decided to merge feature-experiment into feature and then I deleted feature-experiment. There were some merge conflicts which I resolved.

then, my 20 commits which were all using the same name and ending in WIP (work in progress), were very ugly, so I decided to

git rebase -p -i HEAD~22

I changed pick to s to squash them all into the oldest commit for this feature, but I had some merge conflicts (the same as before). I resolved them and then

git add -A && git commit

git rebase --continue

but now I get the following error:

error: Commit asd123qsd is a merge but no -m     option was given.
fatal: cherry-pick failed
Could not pick asd123qsd 

This is the last commit (the merge one)

I tried again, but this time I didnt change this particular commit's pick to s, but got the same error.

How can I carry out this dreadful rebase?

I was thinking, as a possible solution, I could I modify the last commit to add -m to it, but how do I do that and what do I do with this -m command? Are there any other options


Solution

  • The problem is likely here:

    git rebase -p -i HEAD~22

    The -p option is described in the git documentation as the short version of --preserve-merges:

    -p

    --preserve-merges

    Recreate merge commits instead of flattening the history by replaying commits a merge commit introduces. Merge conflict resolutions or manual amendments to merge commits are not preserved.

    This uses the --interactive machinery internally, but combining it with the --interactive option explicitly is generally not a good idea unless you know what you are doing (see BUGS below).

    Since you're trying to squash commits here, preserving merge commits is almost certainly not what you want. Additionally, the warning about combining the --preserve-merges flag with the --interactive (-i) flag is likely relevant here.

    Honestly though, using a rebase to do a squash like this may be introducing a lot more complexity than what you need here. If all you want is to squash all of the changes from this branch into a single commit, you can do that like this:

    git checkout feature
    git reset --soft <base of feature branch>
    

    At this point all the changes from your feature branch should be staged, and the feature branch should be at the base commit. Now you can simply run git commit to create a new commit containing all the changes from your feature branch, which is basically equivalent to a squash.