gitgit-rebase

Rebasing branch from later branch back onto master directly


After updating my local master, I routinely rebase my local branches.

However, this time, I forgot to checkout master back between rebases and rebased one local non-master branch (branch_2 in my example) on top of the other (branch_1). I use IDEA's "Checkout and rebase onto..." feature, it's a convenient shortcut.

How do I now "re-rebase" the mis-rebased branch (branch_2) onto master? Technically, it's already on master (even though there's another branch in between), so rebasing onto master won't work.

* branch_2
*
* branch_1
*
* master
*

Solution

  • Simply use

    git rebase --onto master branch_1 branch_2
    

    i.e., recreate on master all commits from branch_1 to branch_2 – or more precisely, all commits which are contained in branch_2, excluding those which are contained in branch_1 – and then update the branch_2 pointer.

    An alternative interpretation is: change the base of branch_2 from branch_1 to master.

    IMO the most comprehensive explanations for how --onto works are given in What is the difference between `git rebase master` and `git rebase --onto master`?