Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebase a branch that has child branches

Tags:

git

My company's policy is that any pull request must be done with a rebased branch. I have a local Dev branch and this has 'task' branches:

remote master --------
     \   
     Dev ------------------
            \-----task1  \
                          \---task2

When master gets updated, I rebase Dev on master. What is a optimal way to manage branches task1 and task2. They are still being worked on. Rebasing each task branch seems like the only way I can see.

like image 348
Josef.B Avatar asked Apr 30 '17 22:04

Josef.B


2 Answers

Try first do rebase dev while preserving merges:

git checkout Dev
git rebase --preserve-merges master

That should keep task1 and 2 correctly rebased as well.

See "What exactly does git's “rebase --preserve-merges” do (and why?)"

like image 106
VonC Avatar answered Nov 07 '22 21:11

VonC


If you have a lot of task branches, then the best I can think of is to temporarily octopus merge them with Dev into a temp branch and then rebase temp onto master with --preserve-merges. Then reset the branch labels to the proper places.

$ git checkout -b temp Dev && git merge task1 task2
$ git rebase master --preserve-merges
$ git checkout Dev   && git reset --hard temp^
$ git checkout task1 && git reset --hard temp^2
$ git checkout task2 && git reset --hard temp^3
$ git branch -D temp

It's still a lot of commands, but:

  1. Since the tree is inherently preserved, it saves you from having to manually reconstruct the graph, i.e. less human error from having to graft each task branch onto the proper corresponding commit of the rebased Dev branch. It's just a matter of resetting the Dev and task branch markers onto the correct parent commit, which corresponds to the order they were used/named in the octopus merge.

  2. If you have to do this kind of thing a lot, this should be straightforward to script and use as an alias.

like image 24
Bert F Avatar answered Nov 07 '22 19:11

Bert F