Recovering from a "detached from" HEAD
If I detach my head, then use git subtree add to generate a commit, the output from git status changes from "detached at SHA" to "detached from SHA". The sha doesn't change, but HEAD has updated.
I don't understand why git status doesn't tell me that I'm "detached at" the sha of the new commit, but not only that, it seems to be extremely difficult to get git status to reflect my actual HEAD. git checkout HEAD doesn't change it, git checkout NEWSHA doesn't work.
So my question is, what's going on here? Is this intentional behaviour, or a bug? How should I get my working tree back to a normal state?
Quoted 3 lines · Alyssa Ross
If I detach my head, then use
git subtree addto generate a commit, the output fromgit statuschanges from "detached at SHA" to "detached from SHA". The sha doesn't change, but HEAD has updated.
This is expected, and there is nothing to "recover from". Use of "git subtree add" should be irrelevant (iow, you should get the same behaviour no matter _how_ you build new commits on the unnamed branch).
$ git checkout --detach
$ git status -uno
HEAD detached at 9745ede235
$ git commit --allow-empty -m empty
[detached HEAD bc9a31f2df] empty
$ git status -uno
HEAD detached from 9745ede235The commit the message shows is meant to indicate where your unnamed branch diverged at named branches. Immediately after moving to the unnamed branch by detaching the HEAD, the message says "at"; the HEAD is pointing directly at the tip of the then-current branch and that is where the tip of the unnamed branch is. You can tell from that message that you will not lose any commit if you were to check out a named branch from that state.
After you make a commit on the unnamed branch, you have something to lose if you were to check out a named branch from that state, as the detached HEAD is the _only_ thing these new commits you built on top of the fork point. Upon seeing "HEAD detached from 9745ede235", you could do "git log 9745ede235.." and see what you would end up losing if you were to switch to another branch without saving them first to a named branch.
Quoted 2 lines
So my question is, what's going on here? Is this intentional behaviour, or a bug? How should I get my working tree back to a normal state?
There is nothing abnormal in this state while you are working on an unnamed branch.
Quoted 8 lines
The commit the message shows is meant to indicate where your unnamed branch diverged at named branches. Immediately after moving to the unnamed branch by detaching the HEAD, the message says "at"; the HEAD is pointing directly at the tip of the then-current branch and that is where the tip of the unnamed branch is. You can tell from that message that you will not lose any commit if you were to check out a named branch from that state.
Quoted 7 lines
After you make a commit on the unnamed branch, you have something to lose if you were to check out a named branch from that state, as the detached HEAD is the _only_ thing these new commits you built on top of the fork point. Upon seeing "HEAD detached from
9745ede235", you could do "git log9745ede235.." and see what you would end up losing if you were to switch to another branch without saving them first to a named branch.
Thank you for the detailed explanation. I was for some reason under the impression that "git status" would show the current HEAD, rather than where it was detached. Why I thought that, I don't know.
Quoted 16 lines · Alyssa Ross
Quoted 8 lines
The commit the message shows is meant to indicate where your unnamed branch diverged at named branches. Immediately after moving to the unnamed branch by detaching the HEAD, the message says "at"; the HEAD is pointing directly at the tip of the then-current branch and that is where the tip of the unnamed branch is. You can tell from that message that you will not lose any commit if you were to check out a named branch from that state.
Quoted 7 lines
After you make a commit on the unnamed branch, you have something to lose if you were to check out a named branch from that state, as the detached HEAD is the _only_ thing these new commits you built on top of the fork point. Upon seeing "HEAD detached from
9745ede235", you could do "git log9745ede235.." and see what you would end up losing if you were to switch to another branch without saving them first to a named branch.
Quoted 3 lines
Thank you for the detailed explanation. I was for some reason under the impression that "git status" would show the current HEAD, rather than where it was detached. Why I thought that, I don't know.
I wrote not from the documentation but from what I remember about the discussion that led to final phrasing of the message, and I strongly suspect that your impression is the sign that our docs are insufficient. If you can spot where we can improve on, that would be great.
Thanks.