Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebasing a branch with sub-branches all at once

Tags:

git

rebase

I often have sub-branches on a branch that I want to rebase onto the mainline. Consider this:

* (Mainline)
*
*
| * (topicA_Branch3)
| *
| *
| * (topicA_Branch2)
| *
| *
| * (topicA_Branch1)
| *
| *
|/
*
*

I want to move all three of these topicA branches onto mainline. Currently, I know two ways to do this:

  1. While on topicA_Branch3, execute the command, git rebase Mainline.

    a. At this point, I would have to delete topicA_Branch1 and 2 and manually re-create the branches on the correct commits on the now rebased topicA_Branch3.

  2. Another way would be to do three separate commands:

    a. While on topicA_Branch1, do git rebase Mainline.

    b. git rebase --onto topicABranch1 <topicA_Branch1-old-SHA> topicABranch2

    c. git rebase --onto topicABranch2 <topicA_Branch2-old-SHA> topicABranch3

    d. This is kind of cumbersome...

Is there a command that I want that will rebase a branch and bring it's sub-branches with it?

To be clear, I want to end up with this:

* (topicA_Branch3)
*
*
* (topicA_Branch2)
*
*
* (topicA_Branch1)
*
*
* (Mainline)
*
*
*
*
like image 875
boltup_im_coding Avatar asked Mar 31 '14 19:03

boltup_im_coding


People also ask

What is the golden rule of rebasing?

The Golden Rule of Rebasing reads: “Never rebase while you're on a public branch.” This way, no one else will be pushing other changes, and no commits that aren't in your local repo will exist on the remote branch.

Can you rebase more than once?

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.

When should you avoid rebasing a branch?

1 Answer. Show activity on this post. Case 1: We should not do Rebase on branch that is public, i.e. if you are not alone working on that branch and branch exists locally as well as remotely rebasing is not a good choice on such branches and it can cause bubble commits.

Does git rebase change both branches?

Unlike a merge, which merges two branches in one go, rebasing applies the changes from one branch one by one.


1 Answers

What about that:

 a. While on `topicA_Branch1`, do `git rebase Mainline`.

 b. `git rebase --onto topicABranch1 topicABranch1@{1} topicABranch2`

 c. `git rebase --onto topicABranch2 topicABranch2@{1} topicABranch3` 

 d. ...

This might easily be automated. The syntax topicABranch1@{1} means "the last known state of topicABranch1 before the rebase"

like image 103
user2987828 Avatar answered Nov 24 '22 20:11

user2987828