How do I rebase a chain of local git branches?

2022: I described last August in “Git interactive rebase: how to move other branches (refs) automatically?” a new rebase option --update-ref (Git 2.38, Q3 2022):

Automatically force-update any branches that point to commits that are being rebased.
Any branches that are checked out in a worktree are not updated in this way.

              branch1   branch2
                 |         |
            A----B----C----D
           /
o----o----o----o
               |
             main
git switch branch2
git rebase --update-refs main
                    branch1     branch2
                       |           |
                 A'----B'----C'----D'
                /
o----o----o----o
               |
             main

2013: One-line:

git rebase --onto branch1 branch1tmp branch2

That supposes to make a branch1tmp on branch1 before rebasing branch1.

git checkout branch1
git branch branch1tmp
git rebase master
git rebase --onto branch1 branch1tmp branch2

That being said, check what ORIG_HEAD references.
From git rebase man page:

ORIG_HEAD is set to point at the tip of the branch before the reset.

So check if this would work (and scale better):

git checkout branch1
git rebase master
git rebase --onto branch1 ORIG_HEAD branch2
git rebase --onto branch2 ORIG_HEAD branch3
...

Leave a Comment