Leonardo Alves Machado
Leonardo Alves Machado

Reputation: 2837

git rebase to a different branch

I have the following situation:

Master ---X---X---X
               \
   Alpha branch A1---A2---A3---A4---A5---A6
                      \              \
                       \  beta branch B1---B2
                        \
              My Branch M1---M2---M3

I want to move my branch to "B2", ending up like this:

Master ---X---X---X
               \
   Alpha branch A1---A2---A3---A4---A5---A6
                                     \
                          beta branch B1---B2
                                            \                             
                                  My Branch M1---M2---M3

To do that, should I make, in my branch, a git rebase beta or should I add the option --onto like this: git rebase --onto beta alpha My? I had read the documentation, but I still didn't figure out the difference...

Upvotes: 6

Views: 18564

Answers (2)

pishpish
pishpish

Reputation: 2614

Here's four ways to do it:

  • git rebase <branch>

    Reset checked out branch to <branch>, apply differences (between previous HEAD and the common ancestor) and commit.

    git checkout my
    git rebase beta
    
  • git rebase <branch1> <branch2>

    Reset <branch2> to <branch1>, apply differences (between previous <branch2> and the common ancestor) and commit.

    git rebase beta my
    
  • git rebase --onto <branch1> <branch2>

    Reset checked out branch to <branch1>, apply differences (between previous HEAD and <branch2>) and commit.

    git checkout my
    git rebase --onto beta A2
    
  • git rebase --onto <branch1> <branch2> <branch3>

    Reset <branch2> to <branch1>, apply differences (between previous HEAD and <branch2>) and commit.

    git rebase --onto beta A2 my
    

Upvotes: 9

eftshift0
eftshift0

Reputation: 30297

If you are on top of your branch, just saying git rebase beta or git rebase B2 will do. The difference is that by using --onto you can control more tricky situations..... like the opposite: want to move beta branch on top of my branch. If you are on top of beta and run git rebase my-branch you would end up with M1--M2--M3--A3'--A4'--A5'--B1'--B2' (because from the point where the branches diverged (A2) revisions A3 to A5 (both inclusive) would also have to be included in a simple rebase. In that case you would have to run: git rebase --onto my-branch alpha-branch beta-branch. Hope that clears it up

Upvotes: 2

Related Questions