Oliver
Oliver

Reputation: 29621

git rebase says nothing to do

I have the following commits, and master and 2 other branches (b1 and b2):

m1 - a1 - b1
^    ^    ^ 
|    |    branch b
|    branch a
master

I want to rebase b branch so its parent is master:

 / - a1
m1
 \ - b1

If I do git rebase master or git rebase origin/master, I get Current branch b is up to date. That seems wrong but I think it is related to the fact that b in some ways is already rooted on master. It also seems wrong that I would have to use the --onto.

Upvotes: 3

Views: 426

Answers (3)

Oliver
Oliver

Reputation: 29621

So not long after I posted this I understood the problem: the command

git rebase master 

takes all the commits from the current branch, all the way back until the first commit that branches from 2nd argument ie master, and rebases them onto master HEAD.

To clarify why this didn't work, let's add more commits to my original diagram:

                      /- b1 - b2
            /- a1 - a2
m1 - m2 - m3

where commits that start with "m" are on master branch (so master HEAD is at m3), those starting with "a" are on the a branch (so a HEAD is commit a2), and similarly for "b" (so b HEAD is b2). My goal was to end up with this:

            /- a1 - a2
m1 - m2 - m3
            \- b1 - b2

and the commands I tried are basically equivalent to

git pull
git checkout b
git rebase master

Problem is that "until the first commit that branches from master" includes the commits on branch a, so git will try to rebase a1 - a2 - b1 - b2 onto HEAD of master which is m3. This sequence of commits is already rooted at m3, so git says correctly that there is nothing to do.

I had to do instead git rebase --onto master a which means "rebase current branch (which is b) onto master, but only after the HEAD commit of branch a", ie "rebase b1 - b2 onto m3".

Upvotes: 0

eftshift0
eftshift0

Reputation: 30337

It can be done in a single shot like this:

git rebase --onto master a b

You are telling to rebase on top of master all revisions that make up branch b but discarding revisions that are part of the history of a... in other words, bring over revision b1 on top of master (creating b1'), set b to point to b1'.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

Honestly in this case you could just cherry-pick the b1 commit onto a new branch coming from master:

# from b
git reset --hard master
git cherry-pick b1

If you wanted to use a rebase option, then use --onto:

# from b
git rebase --onto m1 a1

The above says to rebase the b branch such that the commit whose parent is the a1 commit (just the b1 commit) should now sit on a new base, specifically commit m1.

Upvotes: 3

Related Questions