Rebase and move parent branches along

Issue

TLDR I want the parent branches (in a linear branch) to move along with the new commits during a rebase.


Initial setup: multiple feature branches (on top of each other) each one waiting to be merged into master

A--B--C--D <-master
   \
    E--F--G--H--I--J--K--L
       ^     ^     ^     ^
       |     |     |     feature_d
       |     |     feature_c
       |     feature_b
       feature_a

A review is made, a new commit M is added to feature_a, then it is merged into master:

A--B--C--D--N <-master
   \       /
    E--F--M <- feature_a
       \
        G--H--I--J--K--L
           ^     ^     ^
           |     |     feature_d
           |     feature_c
           feature_b

It’s here where I want to do the rebase.

Desired result: rebase feature_d to master and move all parent branches along:

A--B--C--D--N <-master
   \       / \
    E--F--M   G'--H'--I'--J'--K'--L'
                  ^       ^       ^
                  |       |       feature_d
                  |       feature_c
                  feature_b

The way I know how to do this is “manual”, i.e. normal rebase of feature_d followed by moving each branch pointer:

git checkout feature_d
git rebase master
git branch -f feature_b H'
git branch -f feature_c J'

This requires manually searching and referencing the new commits by their sha. It involves extra attention and is error prone. I am hoping for an automated process, something like this:

git checkout feature_d
git rebase master --magic-option-move-branches-to-new-commits

Solution

This solution it’s not one magic command, but it it does avoid manual naming of commits. Rebase each branch on top of it’s parent branch, starting with the oldest one:

git checkout feature_b
git rebase master

git checkout feature_c
git rebase feature_b

git checkout feature_d
git rebase feature_c

I wasn’t expected this to work. Here is the explanation why it works: Git rebase skipping identical commits

Answered By – bolov

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published