Skip to contentSkip to conversation

Need some help with git rebase

DISCUSSION Sylvain RABOT 5 participants 7 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hello everybody,

I'm trying to backport a feature branch which is based on the master branch into another branch also based on master but older :

                        a--a--a--a--a--a--a--a--a feature
                       /

--x--x--x--x--x--x--x--x--x--x--x--x master
\
o--o--o--o--o--o--o 12.72.1

This is what I would like :

                        a--a--a--a--a--a--a--a--a feature
                       /

--x--x--x--x--x--x--x--x--x--x--x--x master
\
o--o--o--o--o--o--o--a--a--a--a--a--a--a--a--a 12.72.1

And this is what I get with git rebase --onto 12.72.1 master feature

o--o--o--o--o--o--o--a--a--a--a--a--a--a--a--a feature
/
--x--x--x--x--x--x--x--x--x--x--x--x master
\
o--o--o--o--o--o--o--a--a--a--a--a--a--a--a--a 12.72.1

Can you exlain to me what I need to do to get what I expect.

Best regards.

Signature · 1 line
Sylvain
#1 lore raw

So,

I tried again "git rebase --onto 12.72.1 master feature".

Situation :

                         a--a--a--a--a--a--a--a--a feature
                        /
--x--x--x--x--x--x--x--x--x--x--x--x master
   \
    o--o--o--o--o--o--o 12.72.1

Work flow :

$ git checkout 12.72.1
$ git rebase --onto 12.72.1 master feature

a lot of conflicts, resolving and git rebase --continue

And then, at the end of the rebase, without me doing anything, feature branch is checked out and it seems that its HEAD has been reset to the new 12.72.1 HEAD.

--x--x--x--x--x--x--x--x--x--x--x--x master

   \
    o--o--o--o--o--o--o--a--a--a--a--a--a--a--a--a 12.72.1 feature

Is that normal ? If it is, how do I do to avoid my feature branch to be reset at 12.72.1's HEAD ?

Any enlightenment is very welcome.

Regards.

Signature · 1 line
Sylvain Rabot <[email protected]>
#2 lore raw
Quoted 25 lines · Sylvain Rabot

So,

I tried again "git rebase --onto 12.72.1 master feature".

Situation :

                        a--a--a--a--a--a--a--a--a feature
                       /
--x--x--x--x--x--x--x--x--x--x--x--x master
  \
   o--o--o--o--o--o--o 12.72.1

Work flow :

$ git checkout 12.72.1
$ git rebase --onto 12.72.1 master feature

a lot of conflicts, resolving and git rebase --continue

And then, at the end of the rebase, without me doing anything, feature branch is checked out and it seems that its HEAD has been reset to the new 12.72.1 HEAD.

--x--x--x--x--x--x--x--x--x--x--x--x master

Quoted 8 lines
  \
   o--o--o--o--o--o--o--a--a--a--a--a--a--a--a--a 12.72.1 feature

Is that normal ? If it is, how do I do to avoid my feature branch to be
reset at 12.72.1's HEAD ?

Any enlightenment is very welcome.

Look at the "git-rebase" man page, particularly the order of the arguments, what they mean, and the usage examples of "--onto":

   $ git rebase --onto 12.72.1 master feature

Means, "replay these changes on top of 12.72.1", where "these changes" refers to commits on branch "feature" with upstream "master", which is what "git rebase" did for you.

If you actually want the "feature" branch to continue pointing at the old feature branch rather than your newly rebased one, you could just look up the old SHA1 for it and update it with:

   $ git branch -f feature abcd1234

Where "abcd1234" is the hash of the old "feature" HEAD.

But I don't really know why you'd want to do that. The purpose of "git rebase" isn't to copy or cherry-pick commits from one branch onto another, but to actually _move_ (or transplant, or replay, if you prefer) those commits.

Maybe I misunderstood your intentions though.

Cheers,
Wincent

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 11 lines · Sylvain Rabot

In fact I want to backport the commits of the feature branch into 12.72.1.

I used git rebase because the drawings of the man page looked like that I
wanted to do and it does except for the part it resets the head of my feature branch.

But as you said the good behavior would be to cherry pick each commit of the
feature branch and apply them into 12.72.1, right ?

Well rebasing is just a convenient way of cherry-picking a bunch of commits, so it's probably the right tool for the job.

But as you've seen, it has the effect of "moving" or "transplanting" the feature branch (replacing the old feature HEAD). If you really want the original feature branch HEAD to continue existing after the rebase you'll need to take some specific action to preserve it beforehand (creating a temporary branch before doing the rebase like Peter Baumann suggested) or restore it afterwards (using "git branch" like I suggested).

But before you actually do that, I'd make sure that you actually have a valid reason for keeping that branch around. Maybe wanting to "backport" those commits onto various different branches might be a valid reason. But it's worth thinking through because Git gives you various tools for supporting different workflows (merging, rebasing, cherry-picking) and they each have their use cases.

Cheers,
Wincent

#5 lore raw
In reply to Wincent Colaiuta
Quoted 11 lines · Wincent Colaiuta

Look at the "git-rebase" man page, particularly the order of the arguments, what they mean, and the usage examples of "--onto":

 $ git rebase --onto 12.72.1 master feature

Means, "replay these changes on top of 12.72.1", where "these changes" refers to commits on branch "feature" with upstream "master", which is what "git rebase" did for you.

If you actually want the "feature" branch to continue pointing at the old feature branch rather than your newly rebased one, you could just look up the old SHA1 for it and update it with:

 $ git branch -f feature abcd1234
Quoted 9 lines

Where "abcd1234" is the hash of the old "feature" HEAD.

But I don't really know why you'd want to do that. The purpose of "git rebase" isn't to copy or cherry-pick commits from one branch onto another, but to actually _move_ (or transplant, or replay, if you prefer) those commits.

Maybe I misunderstood your intentions though.

Cheers,
Wincent

In fact I want to backport the commits of the feature branch into 12.72.1. I used git rebase because the drawings of the man page looked like that I wanted to do and it does except for the part it resets the head of my feature branch.

But the good behavior would be to cherry pick each commit of the feature branch and apply them into 12.72.1, right ?

Thanks for your answer.

Regards.

--
Sylvain

#6 lore raw
Quoted 22 lines · Sylvain Rabot
Quoted 11 lines · Wincent Colaiuta

Look at the "git-rebase" man page, particularly the order of the arguments, what they mean, and the usage examples of "--onto":

 $ git rebase --onto 12.72.1 master feature

Means, "replay these changes on top of 12.72.1", where "these changes" refers to commits on branch "feature" with upstream "master", which is what "git rebase" did for you.

If you actually want the "feature" branch to continue pointing at the old feature branch rather than your newly rebased one, you could just look up the old SHA1 for it and update it with:

 $ git branch -f feature abcd1234
Quoted 9 lines

Where "abcd1234" is the hash of the old "feature" HEAD.

But I don't really know why you'd want to do that. The purpose of "git rebase" isn't to copy or cherry-pick commits from one branch onto another, but to actually _move_ (or transplant, or replay, if you prefer) those commits.

Maybe I misunderstood your intentions though.

Cheers,
Wincent

Quoted 8 lines

In fact I want to backport the commits of the feature branch into 12.72.1. I used git rebase because the drawings of the man page looked like that I wanted to do and it does except for the part it resets the head of my feature branch.

But the good behavior would be to cherry pick each commit of the feature branch and apply them into 12.72.1, right ?

$ git checkout -b rebased_feature feature
$ git rebase --onto 12.72.1 master rebased_feature

will create a temporary branch named "rebased_feature" pointing to the same commit as the branch "feature". In fact, this will rebase the commits on the feature branch not reachable from master onto your 12.72.1 branch *and* won't reset the feature branch. Instead the temporary branch named "rebased_feature" will be rebased ontop of 12.72.1.

I would still prefere the rebase over doing multiple cherry picks.

-Peter

#7 lore raw
In reply to Sylvain Rabot

Hi,

Quoted 10 lines · Sylvain Rabot

And then, at the end of the rebase, without me doing anything, feature branch is checked out and it seems that its HEAD has been reset to the new 12.72.1 HEAD.

--x--x--x--x--x--x--x--x--x--x--x--x master
  \
   o--o--o--o--o--o--o--a--a--a--a--a--a--a--a--a 12.72.1 feature

Is that normal ? If it is, how do I do to avoid my feature branch to be reset at 12.72.1's HEAD ?

reading the manual, I think this is normal.

According to the third paragraph of the description of git-rebase, the branch feature will be reset to 12.72.1.

You can just rename the branch back to 12.72.1, and retrieve the old tip of feature by examining the reflog.

Signature · 2 lines
Cheers,
Ray Chuan