How can I rebase and preserve merges and also do squash/fixup

Viewed 71

When tasked with cleaning up a long running feature branch I find there are many commits in the history that needs to be squashed/fixed-up or reworded. I do want to keep the merges as they may have complicated conflict resolutions that I cannot do.

Imagine the following commits:

master: P0 - P1 - P2 - P3 --------------
         \    \                          \
feature:  A - M1 - B - C1 - C2 - C3 - D - M2 - E

Where Ms are merges from the same branch (master)2.

And I want to fixup C2 and C3 into C1 (and say reword C1).

When I do: git rebase -i -r master I see all commits, and the merge, and I mark them with f and r as desired.

When this rebase is executed, and I reach the merge commit M2, git will complain with a conflict. I assume because it cannot apply the merge since commits were rewritten as a consequence of the fixups.

Is there a way to make git do such a merge without conflict? Given that I have not changed any lines of content will an ours or theirs always be correct in that case, and how would I do that in the middle of running this rebase.

1 Answers

If in general terms, what you want to do is squash all your changes into a single one and rebase on top of the branch you started from.... I do it without all the rebase hassle like this:

git checkout my-branch
git merge the-target-branch
# finish that merge, doesn't matter the comment, it will go away
# when the merge is finished
git reset --soft the-target-branch
git commit -m "my squashed feature"

And you rebased/squashed without even noticing

Related