# Correcting forgetting to make a new branch

5 messages from 2011-03-11 to 2011-03-11. Participants: Howard Miller, Alexey Feldgendler, Matthieu Moy, Michael J Gruber.
Thread: https://gitlist.dev/t/26711

## Howard Miller, 2011-03-11 15:31

Subject: Correcting forgetting to make a new branch
Message-ID: <AANLkTi=fp=-pwi7Mj9TptP22mtP51bzq5UwTLRx9LnNB@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTi%3Dfp%3D-pwi7Mj9TptP22mtP51bzq5UwTLRx9LnNB%40mail.gmail.com

```
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, 2011-03-11 15:46

Subject: Re: Correcting forgetting to make a new branch
Message-ID: <op.vr6rgcqs56e9f9@xman.eng.oslo.osa>
URL: https://gitlist.dev/e/op.vr6rgcqs56e9f9%40xman.eng.oslo.osa
In-Reply-To: <AANLkTi=fp=-pwi7Mj9TptP22mtP51bzq5UwTLRx9LnNB@mail.gmail.com>

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

> 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/

```

## Michael J Gruber, 2011-03-11 15:49

Subject: Re: Correcting forgetting to make a new branch
Message-ID: <4D7A4483.5080209@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4D7A4483.5080209%40drmicha.warpmail.net
In-Reply-To: <AANLkTi=fp=-pwi7Mj9TptP22mtP51bzq5UwTLRx9LnNB@mail.gmail.com>

```
Howard Miller venit, vidit, dixit 11.03.2011 16:31:
> 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

```

## Matthieu Moy, 2011-03-11 15:50

Subject: Re: Correcting forgetting to make a new branch
Message-ID: <vpqk4g57j1h.fsf@bauges.imag.fr>
URL: https://gitlist.dev/e/vpqk4g57j1h.fsf%40bauges.imag.fr
In-Reply-To: <op.vr6rgcqs56e9f9@xman.eng.oslo.osa>

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

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

```

## Howard Miller, 2011-03-11 15:53

Subject: Re: Correcting forgetting to make a new branch
Message-ID: <AANLkTikjonZxcHk3a4HvEhiDfcBqwgpqzFP2F=8tF7qv@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTikjonZxcHk3a4HvEhiDfcBqwgpqzFP2F%3D8tF7qv%40mail.gmail.com
In-Reply-To: <vpqk4g57j1h.fsf@bauges.imag.fr>

```
> "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 :)

```
