Skip to contentSkip to conversation

Recovering Committed Changes in a Detached Head?

DISCUSSION Daly Gutierrez 9 participants 10 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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:

Quoted 1 line

git checkout 3a5bb38a83c00f7acab573f0ec836577143200aa

2) Modified file and committed the changes in the detached branch.

Quoted 1 line

git log

commit 92aa5381b9f7229523dba42aa94735c30f173451
Author: Daly Gutierrez <[email protected]>
Date: Sat Oct 8 16:20:11 2011 -0400

Committing this in the Detached Head

3) For curiosity,

Quoted 1 line

git branch

* (no branch)
New_Branch
Second_New_Branch
master

4) Changed to 'New_Branch' branch... I no longer see the detached branch...

Quoted 1 line

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?

#1 lore raw

Hi,

Quoted 24 lines · Daly Gutierrez

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:

Quoted 1 line

git checkout 3a5bb38a83c00f7acab573f0ec836577143200aa

2) Modified file and committed the changes in the detached branch.

Quoted 1 line

git log

commit 92aa5381b9f7229523dba42aa94735c30f173451
Author: Daly Gutierrez <[email protected]>
Date: Sat Oct 8 16:20:11 2011 -0400

Committing this in the Detached Head

3) For curiosity,

Quoted 1 line

git branch

* (no branch)
New_Branch
Second_New_Branch
master

4) Changed to 'New_Branch' branch... I no longer see the detached branch...

Quoted 7 lines
Quoted 1 line

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

#2 lore raw
Quoted 11 lines

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}: ...
...

Quoted 7 lines

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.

While rflog is cool, I can't help but think that git could be even more helpful for these scenarios.

First, maybe git could create refs for these automatically, perhaps with a name like orphans/1? Maybe these refs would only be visible via git branch --orphans.

-Martin
Employee of Qualcomm Innovation Center,Inc. which is a member of Code Aurora Forum

#3 lore raw
Quoted 1 line · Martin Fick
Quoted 1 line

git reflog to the rescue.

<snip>

Quoted 11 lines
Quoted 7 lines

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.

While rflog is cool, I can't help but think that git could be even more helpful for these scenarios.

First, maybe git could create refs for these automatically, perhaps with a name like orphans/1? Maybe these refs would only be visible via git branch --orphans.

Creating these "orphan" refs would require the equivalent of (part of) git-fsck; I can't imagine that could be imposed without significant overhead on too many operations. I think you'd be better off wrapping git-fsck in a script that can create these branches.

--Joel

#4 lore raw
Quoted 14 lines · Joel C. Salomon
Quoted 1 line · Martin Fick
Quoted 1 line

git reflog to the rescue.

<snip>

Quoted 9 lines
Quoted 7 lines

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.

While rflog is cool, I can't help but think that git could be even

more helpful for these scenarios.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

First, maybe git could create refs for these automatically, perhaps

with a name like orphans/1? Maybe these refs would only be visible via git branch --orphans.

Creating these "orphan" refs would require the equivalent of (part of) git-fsck; I can't imagine that could be imposed without significant overhead on too many operations. I think you'd be better off wrapping git-fsck in a script that can create these branches.

Is there another way to create orphan refs than by doing a checkout and leaving the orphan behind? I guess a rebase or other branch rewinding operations, but shouldn't it already know on those that that if it alters anything, it will create an orphan? Git already checks for to see if it is leaving orphan refs behind on a checkout, why not just give the orphan a branch name at those points (not sure what to do if the check is bypassed with -q, just punt I guess)?

-Martin

Employee of Qualcomm Innovation Center,Inc. which is a member of Code Aurora Forum

#5 lore raw
In reply to Martin Fick
Quoted 3 lines · Martin Fick

While rflog is cool,...

First, maybe git could create refs for these automatically, perhaps with a name like orphans/1? Maybe these refs would only be visible via git branch --orphans.

Instead of spelling them orphans/$n, you already have @{$n}.

#6 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 3 lines

First, maybe git could create refs for these automatically, perhaps with a name like orphans/1? Maybe these refs would only be visible via git branch --orphans.

Instead of spelling them orphans/$n, you already have @{$n}.

Hmm, shouldn't that be "HEAD@{$n}" (as the reflog output suggests)?

[Well, I dunno, I'm generally kind of confused by the @{...} notation, but I just tried it out, and just @{$n} seems to refer to the current branch, which presumably won't include the orphaned bit once one it's been orphaned...]

Thanks,

-Miles

Signature · 1 line
`The suburb is an obsolete and contradictory form of human settlement'
#7 lore raw
In reply to Martin Fick
Quoted 14 lines · Martin Fick
Quoted 14 lines

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

Quoted 8 lines
Quoted 4 lines

 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.

While rflog is cool, I can't help but think that git could be even more helpful for these scenarios.

First, maybe git could create refs for these automatically, perhaps with a name like orphans/1?  Maybe these refs would only be visible via git branch --orphans.

If I remember correctly, we don't have private ref namespace, any refs created automatically this way could be pushed out by accident.

Signature · 1 line
Duy
#8 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Nguyen Thai Ngoc Duy

Nguyen> On Sun, Oct 9, 2011 at 9:00 AM, Martin Fick <[email protected]> wrote:

>> While rflog is cool, I can't help but think that git could be >> even more helpful for these scenarios.

>> First, maybe git could create refs for these automatically, >> perhaps with a name like orphans/1?  Maybe these refs would only >> be visible via git branch --orphans.

Nguyen> If I remember correctly, we don't have private ref Nguyen> namespace, any refs created automatically this way could be Nguyen> pushed out by accident.

A way to avoid this could be to deal with this like git-stash works, instead of relying on the branch machinery.

But anyway, I think there is a trade-off between keeping some references to some hypothetical future use and preventing the garbage collector to reclaim useless objects...

Signature · 5 lines
  Ronan KERYELL                      |\/  Phone:  +1 408 844 HPC0
  HPC Project, Inc.                  |/)  Cell:   +33 613 143 766
  5201 Great America Parkway #3241   K    [email protected]
  Santa Clara, CA 95054              |\   skype:keryell
  USA                                | \  http://hpc-project.com
#9 lore raw
In reply to Martin Fick
Quoted 14 lines · Martin Fick
Quoted 14 lines

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

Quoted 6 lines
Quoted 4 lines

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.

While rflog is cool, I can't help but think that git could be even more helpful for these scenarios.

Git has been showing a big scary warning when entering detached HEAD, and now has another helpful one when leaving a commit orphan:

$ git checkout HEAD^
Note: checking out 'HEAD^'.

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name

HEAD is now at adcbd2f... foo

$ date > foo.txt; git add foo.txt; git commit -am "commited on detached HEAD"
[detached HEAD 9e9c4ef] commited on detached HEAD
 1 files changed, 1 insertions(+), 1 deletions(-)
$ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

9e9c4ef commited on detached HEAD

If you want to keep them by creating a new branch, this may be a good time to do so with:

git branch new_branch_name 9e9c4efeca049ca559541595c9ca4a3380dee523

Switched to branch 'master'

(since 8e2dc6ac06ae90, Junio C Hamano, Fri Feb 18 16:04:47 2011, commit:
give final warning when reattaching HEAD to leave commits behind)

I think these warnings are (scary and big) enough.

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/