374
votes

I have two (private) feature branches that I'm working on.

a -- b -- c                  <-- Master
     \     \
      \     d -- e           <-- Branch1
       \
        f -- g               <-- Branch2

After working on these branches a little while I've discovered that I need the changes from Branch2 in Branch1. I'd like to rebase the changes in Branch2 onto Branch1. I'd like to end up with the following:

a -- b -- c                  <-- Master
           \
            d -- e -- f -- g <-- Branch1

I'm pretty sure I need to rebase the second branch onto the first, but I'm not entirely sure about the correct syntax and which branch I should have checked out.

Will this command produce the desired result?

(Branch1)$ git rebase --onto Branch1 Branch2
4
To answer your question, I would create a test repository, create the commit structure you showed and try the command you showed. But I think you can do that yourself, so I am not going to do it :)Daniel Hilgarth
Thanks. I was so bent on getting this right the first time that it didn't occur to me that I could easily test this myself :-)Arjen
I thought so, that's why I posted that comment :) Everytime I do something I am not sure it will do what I think it does, I create a test repository and perform my tests there. Or, I create a copy of my real repository and perform the tests on the copy.Daniel Hilgarth
Note: Git 2.0 will introduce a shortcut for this kind of rebase: git rebase -. see my answer belowVonC
Minor note: The answers here give branch2 as the result. The OP wanted branch1. Or I missed something?Josef.B

4 Answers

427
votes
  1. Switch to Branch2

    git checkout Branch2
    
  2. Apply the current (Branch2) changes on top of the Branch1 changes, staying in Branch2:

    git rebase Branch1
    

Which would leave you with the desired result in Branch2:

a -- b -- c                      <-- Master
           \
            d -- e               <-- Branch1
           \
            d -- e -- f' -- g'   <-- Branch2

You can delete Branch1.

65
votes

Note: if you were on Branch1, you will with Git 2.0 (Q2 2014) be able to type:

git checkout Branch2
git rebase -

See commit 4f40740 by Brian Gesiak modocache:

rebase: allow "-" short-hand for the previous branch

Teach rebase the same shorthand as checkout and merge to name the branch to rebase the current branch on; that is, that "-" means "the branch we were previously on".

12
votes

I know you've already accepted the answer, but if you want to do exactly what you asked for (add Branch2's commits on top of Branch1 while staying on Branch1) you can do this:

git checkout Branch1
git cherry-pick master..Branch2

You'll end up with this.

a -- b -- c                      <-- Master
           \
            d -- e -- f' -- g'   <-- Branch1 (current)
           \
            f -- g               <-- Branch2

This will cherry-pick each of the commits on Branch2 in order onto Branch1. Then you can delete Branch2.

7
votes

I know you asked to Rebase, but I'd Cherry-Pick the commits I wanted to move from Branch2 to Branch1 instead. That way, I wouldn't need to care about when which branch was created from master, and I'd have more control over the merging.

a -- b -- c                  <-- Master
     \     \
      \     d -- e -- f -- g <-- Branch1 (Cherry-Pick f & g)
       \
        f -- g               <-- Branch2