2

I have situation like this:

                                   H---I---J feature2
                                  /
                         E---F---G  feature1
                        /
           A---B---C---D  master

I want to keep my master up-to-date since it is changing very frequently while working on the features. My current way of work is as follows:

git checkout master
git pull
git checkout feature1
git rebase master
git checkout feature2
git rebase feature1

Is there any simpler way to do all these steps? I'm using latest sable git version. Also, I sometimes have situation like this with even 6 feature branches, so it would be great if any solution would be extensible for many feature branches on top of another.

2
  • Why do you want to these be in the top of each other? Git is bassically a tool for code versioning. In my team every ticket has a different branch and if it tested it will be merged back in the master branch. At home I use with two branches, master and develop, and if I end the developing of a feature/bugfix I merge it back to the master.
    – Feralheart
    Commented Nov 9, 2017 at 16:18
  • 2
    @Feralheart because the featureN depends on the changes on featureN-1. I'm doing a lot of work the same time and usually it ends like featureN-1 is on the review, when I'm already working on the featureN.
    – CutrNha
    Commented Nov 9, 2017 at 16:40

2 Answers 2

3

Actually, rebasing just feature2 would also rebase feature1, in that it would duplicate its commits and replay them onto master.

You could then reset feature1 to its new HEAD commit, located 'n' commits before feature2 HEAD.
And you can first count the number of commits of feature2 with:

git checkout feature2
git rev-list --count HEAD ^feature1 # memorize that in 'n'

Then rebase (no need to checkout master and pull):

git fetch
git rebase origin/master

                                           H'---I'---J' feature2
                                          /
                    master     E'---F'---G' 
                       |      /
           A---B---C---D--Y--Z  origin/master
                        \
                         E--F--F (feature1)

Finally reset feature1

git branch -f feature1 feature2~n

So, if we extend that scenario to 6 branches:

  • one rebase (even with 6 consecutive branches)
  • but 5 git rev-list (to count how far the other branch HEAD are located)
  • and 5 git branches reset.

In any case, that would need to be scripted in order to scale.

2
  • It looks like it would be much easier to script my "manual" solution as it seems more intuitive then doing reset to the new HEAD. I'll wait some time to see if someone has another idea and if not, I'll accept your answer:)
    – CutrNha
    Commented Nov 10, 2017 at 10:40
  • @CutrNha my solution might be less intuitive, but minimize the number of rebase, and with it, the number of conflicts.
    – VonC
    Commented Nov 10, 2017 at 10:41
2

As of Git 2.38.0, rebase has learned the option --update-refs and your task becomes as easy as:

git rebase --update-refs master feature2

This will rebase feature2 onto master and at the same time update refs that pointed to the original commits to point to the new, rebased commits instead.

1
  • This should be accepted answer. It rebases last "leaf" and all intermediate branches into a given trunk. Commented Nov 15, 2023 at 15:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.