# Commit to wrong branch. How to fix?

7 messages from 2009-09-04 to 2009-09-05. Participants: Howard Miller, Michael J Gruber, Erik Faye-Lund, Jakub Narebski.
Thread: https://gitlist.dev/t/20853

## Howard Miller, 2009-09-04 15:54

Subject: Commit to wrong branch. How to fix?
Message-ID: <88A0A7C2-7DCB-45A3-B196-BED2A8AC9405@googlemail.com>
URL: https://gitlist.dev/e/88A0A7C2-7DCB-45A3-B196-BED2A8AC9405%40googlemail.com

```
I commited to the wrong branch and I can't figure out what to do. To  
make matters worse I then did 'git reset HEAD^' which has made things  
much worse. It didn't remove the commit and now I can't change  
branches. I'm utterly confused. Any help much appreciated!

Moral - use git status liberally and read it carefully before doing  
anything. A 'git undo" command would be great is someone is feeling  
generous :-) 

```

## Michael J Gruber, 2009-09-04 16:19

Subject: Re: Commit to wrong branch. How to fix?
Message-ID: <4AA13DF4.4050604@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4AA13DF4.4050604%40drmicha.warpmail.net
In-Reply-To: <88A0A7C2-7DCB-45A3-B196-BED2A8AC9405@googlemail.com>

```
Howard Miller venit, vidit, dixit 04.09.2009 17:54:
> I commited to the wrong branch and I can't figure out what to do. To  
> make matters worse I then did 'git reset HEAD^' which has made things  
> much worse. It didn't remove the commit and now I can't change  
> branches. I'm utterly confused. Any help much appreciated!
> 
> Moral - use git status liberally and read it carefully before doing  
> anything. A 'git undo" command would be great is someone is feeling  
> generous :-) 

Whatever happens, don't panic ;)

Let's say "geesh" is the branch on which you committed by mistake, and
which you have reset.

git reflog geesh

which show you what has happened to that branch lately. In particular,
it will list the "lost" commit. (Most probably it is the same as geesh@{1}.)

git tag sigh sha1ofthatcommit

will assign the tag "sigh", so that it won't get lost by doing "git gc"
or such. Now you can lean back!

Next step is committing "sigh" to the right branch. Depends on how you
arrived at that commit. Did you commit the complete tree you wanted, or
did you apply a change to geesh which you rather had wanted applied to
some other branch?

Michael

```

## Erik Faye-Lund, 2009-09-04 17:11

Subject: Re: Commit to wrong branch. How to fix?
Message-ID: <40aa078e0909041011s7639cccbqa758f17a56c61863@mail.gmail.com>
URL: https://gitlist.dev/e/40aa078e0909041011s7639cccbqa758f17a56c61863%40mail.gmail.com
In-Reply-To: <4AA13DF4.4050604@drmicha.warpmail.net>

```
On Fri, Sep 4, 2009 at 6:19 PM, Michael J
Gruber<git@drmicha.warpmail.net> wrote:
> Let's say "geesh" is the branch on which you committed by mistake, and
> which you have reset.
>
> git reflog geesh

That should be "git reflog show geesh", no?

-- 
Erik "kusma" Faye-Lund
kusmabite@gmail.com
(+47) 986 59 656

```

## Howard Miller, 2009-09-04 18:01

Subject: Re: Commit to wrong branch. How to fix?
Message-ID: <26ae428a0909041101h509ffa31odf3502228517043a@mail.gmail.com>
URL: https://gitlist.dev/e/26ae428a0909041101h509ffa31odf3502228517043a%40mail.gmail.com
In-Reply-To: <4AA13DF4.4050604@drmicha.warpmail.net>

```
Hi Michael,

Thanks for your help.

I had modified a few files and then done a 'git commit -a'. It was
only after this that I did a status and realised that I had switched
to a different branch (and forgotten). Unfortunately the branch I
switched to tracks a remote that is my stable release so I'm a bit
precious about it. If I forget again and push it, I'm in trouble :-)
Yes, I have done that before!!

Howard

2009/9/4 Michael J Gruber <git@drmicha.warpmail.net>:
> Howard Miller venit, vidit, dixit 04.09.2009 17:54:
>> I commited to the wrong branch and I can't figure out what to do. To
>> make matters worse I then did 'git reset HEAD^' which has made things
>> much worse. It didn't remove the commit and now I can't change
>> branches. I'm utterly confused. Any help much appreciated!
>>
>> Moral - use git status liberally and read it carefully before doing
>> anything. A 'git undo" command would be great is someone is feeling
>> generous :-)
>
> Whatever happens, don't panic ;)
>
> Let's say "geesh" is the branch on which you committed by mistake, and
> which you have reset.
>
> git reflog geesh
>
> which show you what has happened to that branch lately. In particular,
> it will list the "lost" commit. (Most probably it is the same as geesh@{1}.)
>
> git tag sigh sha1ofthatcommit
>
> will assign the tag "sigh", so that it won't get lost by doing "git gc"
> or such. Now you can lean back!
>
> Next step is committing "sigh" to the right branch. Depends on how you
> arrived at that commit. Did you commit the complete tree you wanted, or
> did you apply a change to geesh which you rather had wanted applied to
> some other branch?
>
> Michael
>

```

## Howard Miller, 2009-09-04 18:03

Subject: Re: Commit to wrong branch. How to fix?
Message-ID: <26ae428a0909041103p4ecba8efvff6223f902e14f1a@mail.gmail.com>
URL: https://gitlist.dev/e/26ae428a0909041103p4ecba8efvff6223f902e14f1a%40mail.gmail.com
In-Reply-To: <4AA13DF4.4050604@drmicha.warpmail.net>

```
Sorry... meant to add. When I do a 'git log' on the current (wrong
branch) the commit I made is still at the top of the list. I must
admit I don't understand what 'reflog' is (more reading) - not heard
of that before. I did ONE commit and ONE reset and then decided not to
touch it again :-)

Howard

2009/9/4 Michael J Gruber <git@drmicha.warpmail.net>:
> Howard Miller venit, vidit, dixit 04.09.2009 17:54:
>> I commited to the wrong branch and I can't figure out what to do. To
>> make matters worse I then did 'git reset HEAD^' which has made things
>> much worse. It didn't remove the commit and now I can't change
>> branches. I'm utterly confused. Any help much appreciated!
>>
>> Moral - use git status liberally and read it carefully before doing
>> anything. A 'git undo" command would be great is someone is feeling
>> generous :-)
>
> Whatever happens, don't panic ;)
>
> Let's say "geesh" is the branch on which you committed by mistake, and
> which you have reset.
>
> git reflog geesh
>
> which show you what has happened to that branch lately. In particular,
> it will list the "lost" commit. (Most probably it is the same as geesh@{1}.)
>
> git tag sigh sha1ofthatcommit
>
> will assign the tag "sigh", so that it won't get lost by doing "git gc"
> or such. Now you can lean back!
>
> Next step is committing "sigh" to the right branch. Depends on how you
> arrived at that commit. Did you commit the complete tree you wanted, or
> did you apply a change to geesh which you rather had wanted applied to
> some other branch?
>
> Michael
>

```

## Jakub Narebski, 2009-09-04 18:51

Subject: Re: Commit to wrong branch. How to fix?
Message-ID: <m33a72lf0h.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m33a72lf0h.fsf%40localhost.localdomain
In-Reply-To: <26ae428a0909041103p4ecba8efvff6223f902e14f1a@mail.gmail.com>

```
Howard Miller <howardsmiller@googlemail.com> writes:

> I must admit I don't understand what 'reflog' is (more reading) - not heard
> of that before.

I'll try to explain it with some ASCII-art.

Let's say that you had the following situation:

  ...---A---B---C           <-- foo  <--- HEAD

Current branch is named 'foo', and three last commits on it are named
A, B, C.

Now you create new commit (I assume that you comitted unwanted
changes; the recipe would be different (much simpler) if you have
realized that you are on wrong branch[1] before committing)

[1] git aware shell prompt, countaing branch name, could help there

  ...---A---B---C---X           <-- foo  <--- HEAD

Reflog records that commit in 'foo' reflog and in HEAD reflog

  foo@{0}: X


You have realized that you are on wrong branch, and you did 
"git reset --hard HEAD^" (too early)

  ...---A---B---C               <-- foo  <--- HEAD
                 \
                  \-X

Reflog records that fact (it records where tip of branch was)

  foo@{0}: C
  foo@{1}: X


Then if you want to create new branch 'bar' with X, you would do

 $ git checkout -b bar foo@{1}

If you wanted to have this commit on some other existing branch, let's
call it 'baz', you would do instead (I think):

 $ git checkout baz
 $ git cherry-pick foo@{1}

HTH (hope that helps).
-- 
Jakub Narebski
Poland
ShadeHawk on #git

```

## Michael J Gruber, 2009-09-05 12:51

Subject: Re: Commit to wrong branch. How to fix?
Message-ID: <4AA25ED5.1020700@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4AA25ED5.1020700%40drmicha.warpmail.net
In-Reply-To: <40aa078e0909041011s7639cccbqa758f17a56c61863@mail.gmail.com>

```
Erik Faye-Lund venit, vidit, dixit 04.09.2009 19:11:
> On Fri, Sep 4, 2009 at 6:19 PM, Michael J
> Gruber<git@drmicha.warpmail.net> wrote:
>> Let's say "geesh" is the branch on which you committed by mistake, and
>> which you have reset.
>>
>> git reflog geesh
> 
> That should be "git reflog show geesh", no?
> 

Yes, sorry. "show" is default, but only without a branch arg :|

Michael

```
