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

Re: Recovering Committed Changes in a Detached Head?

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Oct 8, 2011, 21:37 UTC
Message-ID
<20111008213741.GA24409@goldbirke>
In-Reply-To
<1318107488.5865.46.camel@R0b0ty>
Hi,
On Sat, Oct 08, 2011 at 04:58:07PM -0400, Daly Gutierrez wrote:
Show 31 quoted lines
> Is this possible after changing from the Detached Head branch to an
> existing branch?  How about if I don't remember the commit SHA1 ID?
> 
> What I did, to the best of my knowledge:
> 1) Checked out a previous version:
> > git checkout 3a5bb38a83c00f7acab573f0ec836577143200aa
> 
> 2) Modified file and committed the changes in the detached branch.
> > git log
>  commit 92aa5381b9f7229523dba42aa94735c30f173451
>  Author: Daly Gutierrez <Daly.Gutierrez@gmail.com>
>  Date:   Sat Oct 8 16:20:11 2011 -0400
> 
>     Committing this in the Detached Head
> 
> 3) For curiosity,
> > git branch
> * (no branch)
>   New_Branch
>   Second_New_Branch
>   master
> 
> 4) Changed to 'New_Branch' branch...  I no longer see the detached
> branch...
>  > git branch
> * New_Branch
>   Second_New_Branch
>   master
> 
> 5) Want to access the file with the changes I made in the Detached
> branch, but don't know how...  PLEASE HELP?

git reflog to the rescue. For your example above it will output something like this:

  deadbeef HEAD@{0}: checkout: moving from 92aa5381b9f7229523dba42aa94735c30f173451 to New_Branch
  92aa5381 HEAD@{1}: commit: Committing this in the Detached Head
  3a5bb38a HEAD@{2}: checkout: moving from master to 3a5bb38a83c00f7acab573f0ec836577143200aa
  deafbabe HEAD@{3}: ...
  ...

There you see the first line of the commit message from your "lost" commit, and you can do

  git checkout -b lost_detached_head 92aa5381

and you get a branch pointing to that commit you made while on detached head, and you can work with it as usual.

Best, Gábor

Previous: Daly GutierrezNext: Martin Fick
Message 2 of 10 in “Recovering Committed Changes in a Detached Head?”
  1. Daly GutierrezOct 8, 2011
  2. SZEDER GáborOct 8, 2011
  3. Martin FickOct 8, 2011
  4. Joel C. SalomonOct 9, 2011
  5. Martin FickOct 9, 2011
  6. Nguyen Thai Ngoc DuyOct 9, 2011
  7. Ronan KeryellOct 9, 2011
  8. Matthieu MoyOct 9, 2011
  9. Junio C HamanoOct 9, 2011
  10. Miles BaderOct 10, 2011

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.