# Recovering from a "detached from" HEAD

4 messages from 2019-01-07 to 2019-01-08. Participants: Alyssa Ross, Junio C Hamano.
Thread: https://gitlist.dev/t/50170

## Alyssa Ross, 2019-01-07 16:17

Subject: Recovering from a "detached from" HEAD
Message-ID: <20190107161748.pyhgpewymdgjmgoh@x220>
URL: https://gitlist.dev/e/20190107161748.pyhgpewymdgjmgoh%40x220

```
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, 2019-01-07 19:43

Subject: Re: Recovering from a "detached from" HEAD
Message-ID: <xmqq8szw8d8o.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq8szw8d8o.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20190107161748.pyhgpewymdgjmgoh@x220>

```
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, 2019-01-08 14:19

Subject: Re: Recovering from a "detached from" HEAD
Message-ID: <20190108141927.a4q5fqug3ymkh5v3@x220>
URL: https://gitlist.dev/e/20190108141927.a4q5fqug3ymkh5v3%40x220
In-Reply-To: <xmqq8szw8d8o.fsf@gitster-ct.c.googlers.com>

```
> 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, 2019-01-08 14:44

Subject: Re: Recovering from a "detached from" HEAD
Message-ID: <xmqq36q36wfw.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq36q36wfw.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20190108141927.a4q5fqug3ymkh5v3@x220>

```
Alyssa Ross <hi@alyssa.is> writes:

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

```
