threads / discuss / 26711

Correcting forgetting to make a new branch

Subject: Correcting forgetting to make a new branch

## tl;dr

5 messages between Mar 11, 2011 and Mar 11, 2011.

replies: 4people: 4as markdown or json

Howard Miller· Mar 11, 2011, 15:31 UTC · lore

The scenario is that I have done work on a branch and made a number of commits. What I wanted to do was to start a new branch before I made these commits. Unfortunatel, I got distracted and forgot that I hadn't done it.

Is there are neat way to create the branch and 'move' the commits over?
TYIA
Alexey Feldgendler· Mar 11, 2011, 15:46 UTC · re: Howard Miller · lore

Re: Correcting forgetting to make a new branch

On Fri, 11 Mar 2011 16:31:18 +0100, Howard Miller <howard@e-learndesign.co.uk> wrote:

Show 6 quoted lines
> The scenario is that I have done work on a branch and made a number of
> commits. What I wanted to do was to start a new branch before I made
> these commits. Unfortunatel, I got distracted and forgot that I hadn't
> done it.
>
> Is there are neat way to create the branch and 'move' the commits over?
Just create a new branch right where you are, it will be correct.

However, the branch that was active before when you should have branched will have been unintentionally updated. To fix that, you'll need to move the old branch a few commits back:

git branch -f <old-branch> <where-it-was-before>
-- 
Alexey Feldgendler
Software Developer, Desktop Team, Opera Software ASA
[ICQ: 115226275] http://my.opera.com/feldgendler/
Matthieu Moy· Mar 11, 2011, 15:50 UTC · re: Alexey Feldgendler · lore

Re: Correcting forgetting to make a new branch

"Alexey Feldgendler" <alexeyf@opera.com> writes:
Show 5 quoted lines
> However, the branch that was active before when you should have
> branched will have been unintentionally updated. To fix that, you'll
> need to move the old branch a few commits back:
>
> git branch -f <old-branch> <where-it-was-before>

This is correct, but for completeness: don't do that if you've already pushed your changes. If you pushed them and people already pulled them, you can hardly do better than appologize to your co-workers that you shouldn't have done that, and possibly create a new commit on top of the branch reverting what you did.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Howard Miller· Mar 11, 2011, 15:53 UTC · re: Matthieu Moy · lore

Re: Correcting forgetting to make a new branch

Show 13 quoted lines
> "Alexey Feldgendler" <alexeyf@opera.com> writes:
>
>> However, the branch that was active before when you should have
>> branched will have been unintentionally updated. To fix that, you'll
>> need to move the old branch a few commits back:
>>
>> git branch -f <old-branch> <where-it-was-before>
>
> This is correct, but for completeness: don't do that if you've already
> pushed your changes. If you pushed them and people already pulled them,
> you can hardly do better than appologize to your co-workers that you
> shouldn't have done that, and possibly create a new commit on top of the
> branch reverting what you did.
I hadn't (luckily), so that worked fine. Thanks for your help - both of you :)
Michael J Gruber· Mar 11, 2011, 15:49 UTC · re: Howard Miller · lore

Re: Correcting forgetting to make a new branch

Howard Miller venit, vidit, dixit 11.03.2011 16:31:
Show 6 quoted lines
> The scenario is that I have done work on a branch and made a number of
> commits. What I wanted to do was to start a new branch before I made
> these commits. Unfortunatel, I got distracted and forgot that I hadn't
> done it.
> 
> Is there are neat way to create the branch and 'move' the commits over?

It depends on which commit you wanted the new branch to start with. If you have

...-*-*-*-X-*-*-*-*-A

on branch A and X is the commit where you meant to create a new branch B starting at X you can simply

git checkout A git branch B git reset --hard X

or simply

git branch -m A B git branch -f A X

If, on the other hand, you wanted to start B from a different commit Y, you will need to rebase:

git branch B A git branch -f A X git rebase --onto Y A B

After backing up, of course ;)
Michael

← back to recent threads