349

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
Nir O.
  • 1,322
  • 1
  • 16
  • 25
Arjen
  • 4,183
  • 3
  • 15
  • 19
  • 15
    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 Feb 15 '13 at 11:19
  • 5
    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 Feb 15 '13 at 11:27
  • 5
    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 Feb 15 '13 at 11:29
  • Note: Git 2.0 will introduce a shortcut for this kind of rebase: `git rebase -`. see [my answer below](http://stackoverflow.com/a/22830458/6309) – VonC Apr 03 '14 at 07:17
  • 6
    Minor note: The answers here give branch2 as the result. The OP wanted branch1. Or I missed something? – Josef.B Apr 30 '17 at 22:12

3 Answers3

399
  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.

Rafael
  • 6,646
  • 13
  • 29
  • 43
sasikt
  • 4,466
  • 1
  • 15
  • 15
  • Thanks! When deleting the branch after rebasing I get a message that the branch is not fully merged. I assume I can safely ignore this message and force the delete? – Arjen Feb 15 '13 at 11:28
  • It should not show any error if you have rebased properly. Make sure that all your commits in the branch1 which is to be deleted is present in branch2 and delete branch1 – sasikt Feb 15 '13 at 11:38
  • In case Branch1 changes, you will get into conflicts. Before merging them by hand try "git rebase --skip". If you merge those changes by hand by fully accepting all changes made in the Branch1, you would encounter on "git rebase --continue" the message "No changes - did you forget to use 'git add'?". In such case, also issue "git rebase --skip" – Maksymilian Wojakowski Oct 21 '14 at 11:46
  • 18
    didn't he want to have all changes in Branch1? – tomasz_kusmierczyk Mar 23 '18 at 13:45
  • 9
    This seems like the opposite of what he wanted, no? – 1252748 Jul 11 '18 at 19:49
  • 3
    Indeed, @tomasz_kusmierczyk and @1252748, and I got confused myself, too. But then I realized that performing `git rebase` while staying in Branch1 will rewrite Branch1 history to have Branch1's changes on top of those copied from Branch2. That will result in the following commit order, `a - b - f - g - c' - d' - e'`. – eel ghEEz Oct 22 '18 at 19:35
  • 3
    @tomasz_kusmierczyk and 1252748, this is not the opposite of what he want, this is EXACTLY what he wanted. Branch names don't matter, you can always change them. – a3y3 Feb 18 '20 at 22:59
63

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".

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • 37
    nice, but also a bit dangerous. sometimes verbosity wins. but then again, I also like Java... (-: – sthzg Jul 27 '17 at 09:37
3

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
Craigo
  • 2,630
  • 22
  • 18