Search code examples
gitrebasegit-rebase

How to rebase after squashing commits in the original branch?


I have

A--B--C master
       \
        D branch0

Then I squash B and C into B'. How do I rebase branch0 such that it looks like this:

A--B' master
    \
     D branch0

Solution

  • Use the --onto argument to git rebase, which changes the baseline that git replays work on.

    git checkout branch0
    

    At this state you should still see C in your git history.

    git rebase --onto B' C
    

    This translates to: Take all commits in my current branch since commit C (In your case that's just D) and play back on top of B'

    You can also use the optional <branch> argument to do both the checkout and rebase at once:

    git rebase --onto B' C branch0