threads / discuss / 50170

Recovering from a "detached from" HEAD

Subject: Recovering from a "detached from" HEAD

## tl;dr

4 messages between Jan 7, 2019 and Jan 8, 2019.

replies: 3people: 2as markdown or json

Alyssa Ross· Jan 7, 2019, 16:17 UTC · lore

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?

Junio C Hamano· Jan 7, 2019, 19:43 UTC · re: Alyssa Ross · lore

Re: Recovering from a "detached from" HEAD

Alyssa Ross <hi@alyssa.is> writes:
> 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.

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 9745ede235

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.

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.

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

Alyssa Ross· Jan 8, 2019, 14:19 UTC · re: Junio C Hamano · lore

Re: Recovering from a "detached from" HEAD

Show 15 quoted 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.
>
> 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.

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.

Junio C Hamano· Jan 8, 2019, 14:44 UTC · re: Alyssa Ross · lore

Re: Recovering from a "detached from" HEAD

Alyssa Ross <hi@alyssa.is> writes:
Show 19 quoted 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.
>>
>> 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.
>
> 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.

← back to recent threads