Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rebase multiple branches at once?

I currently work on a project in which SVN is used as a repository. Locally, I do several "experiments" with the code that I don't want to commit to the repo. Therefore I use git locally on top of the SVN working directory and use multiple branches for different "experiments". So it looks e.g. like this.

C0 ---- C7 (master)  \   \---- C1 ---- C2 ---- C4 (exp1)    \     \       \     \     \       \---- C3 (exp2)      \     \       \     \---- C5 (exp3)        \         \---- C6 (exp4) 

On branch master I want to have the untainted SVN repo, i.e. C0 is SVN Repo Revision x and C7 is SVN Repo Revision x + n.

Is it somehow easily possible to rebase all the exp branches onto C7 at once in such a way that the tree will look like the following diagram?

C0 ---- C7 (master)          \           \---- C1' ---- C2' ---- C4' (exp1)            \     \         \             \     \         \---- C3' (exp2)              \     \               \     \---- C5' (exp3)                \                 \---- C6' (exp4) 

I want to use rebase since I don't want to modify the state of my local SVN Checkout and want to have my changes built on top of the SVN revisions.

I'm also interested in solutions that work from within SourceTree without having to use the shell.

like image 949
moktor Avatar asked Apr 30 '14 11:04

moktor


People also ask

Can I rebase multiple branches?

Git merge and rebase serve the same purpose. They are designed to integrate changes from one or multiple branches into one. Although the final goal is the same, those two methods achieve it in different ways, and it's helpful to know the difference. Let's start with the following graph to understand the differences.

Can I rebase multiple times?

Yes, you can rebase more than once. After rebasing, you get a fresh set of commits. These commits are exactly like all other commits and hold no record of having been rebased. The main thing you need to be careful for is the possibility of rebase conflicts.

Is merge better than rebase?

But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch. The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge .


1 Answers

You can't get this in one step. At best you move each branch one by one; this would be the sequence:

git rebase --onto master exp1 git rebase --onto c2' c2 exp2 git rebase --onto c1' c1 exp3 git rebase --onto master exp4 

The key is to rebase from each branch point in the old tree (e.g. c2 in the above) on to the new tree (e.g. c2' in the above).

like image 184
GoZoner Avatar answered Oct 03 '22 05:10

GoZoner