threads / discuss / 22037

Need some help with git rebase

Subject: Need some help with git rebase

## tl;dr

7 messages between Dec 28, 2009 and Dec 31, 2009.

replies: 6people: 5as markdown or json

Sylvain RABOT· Dec 28, 2009, 13:33 UTC · lore
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.
-- 
Sylvain
Sylvain Rabot· Dec 30, 2009, 22:24 UTC · re: Sylvain RABOT · lore

Re: Need some help with git rebase

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.
-- 
Sylvain Rabot <sylvain@abstraction.fr>
Wincent Colaiuta· Dec 30, 2009, 22:46 UTC · re: Sylvain Rabot · lore

Re: Need some help with git rebase

El 30/12/2009, a las 23:24, Sylvain Rabot escribió:
Show 33 quoted lines
> 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.

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

Sylvain Rabot· Dec 31, 2009, 09:02 UTC · re: Wincent Colaiuta · lore

Re: Need some help with git rebase

On Wed, Dec 30, 2009 at 23:46, Wincent Colaiuta <win@wincent.com> wrote:
Show 20 quoted lines
>
> 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
>

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

Peter Baumann· Dec 31, 2009, 09:32 UTC · re: Sylvain Rabot · lore

Re: Need some help with git rebase

On Thu, Dec 31, 2009 at 10:02:12AM +0100, Sylvain Rabot wrote:
Show 30 quoted lines
> On Wed, Dec 30, 2009 at 23:46, Wincent Colaiuta <win@wincent.com> wrote:
> >
> > 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
> >
> 
> 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
Tay Ray Chuan· Dec 31, 2009, 05:01 UTC · re: Sylvain Rabot · lore

Re: Need some help with git rebase

Hi,
On Thu, Dec 31, 2009 at 6:24 AM, Sylvain Rabot <sylvain@abstraction.fr> wrote:
Show 10 quoted lines
> 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.

-- 
Cheers,
Ray Chuan
Wincent Colaiuta· Dec 31, 2009, 11:06 UTC · lore

Re: Need some help with git rebase

El 31/12/2009, a las 00:02, Sylvain Rabot escribió:
Show 11 quoted 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 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

← back to recent threads