git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Commit to wrong branch. How to fix?

From
Jakub Narebski <jnareb@gmail.com>
Date
Sep 4, 2009, 18:51 UTC
Message-ID
<m33a72lf0h.fsf@localhost.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
Previous: Howard Miller
Message 7 of 7 in “Commit to wrong branch. How to fix?”
  1. Howard MillerSep 4, 2009
  2. Michael J GruberSep 4, 2009
  3. Erik Faye-LundSep 4, 2009
  4. Michael J GruberSep 5, 2009
  5. Howard MillerSep 4, 2009
  6. Howard MillerSep 4, 2009
  7. Jakub NarebskiSep 4, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.