Search code examples
gitgit-rebasesquashgit-squash

Git: Squash commits older than conflicting merge


I would like to squash some commits that are older than a "conflicting" merge. E.g.:

main branch:  A--B--C--D--G--H
dev branch:    \-E--F----/

H is my current head, G is the conflicting merge (so a merge in which I resolved conflicts manually) and I would like to squash C and D - but if I try to do that, I'm getting rebase conflicts which are due to G having been a conflicting merge.

Can I squash C and D anyways and if yes, how? I would be interested in a solution in which I do not have to merge something manually again.

Additional info: git rebase -i B does not even work when leaving all commits on "pick" - which should do nothing, right?


Solution

  • Don't use rebase for this, rebase can squash for you but it's built for much heavier lifting than ancestry-only rewrites like this, it does a load of (slow) prep work to enable any alterations you like along the way.

    git replace --graft G B F   # (put usable references to those commits here of course)
    git filter-branch
    

    and you've achieved everything the rebase would, vastly quicker.