Git development
 help / color / mirror / Atom feed
From: "Kristoffer Haugsbakk" <kristofferhaugsbakk@fastmail.com>
To: git@vger.kernel.org, gItGitgadget <gitgitgadget@gmail.com>
Cc: "Julia Evans" <julia@jvns.ca>
Subject: Re: [PATCH] doc: checkout: rewrite detached HEAD state explanation
Date: Wed, 07 Oct 2026 21:02:30 +0200	[thread overview]
Message-ID: <cb482e6d-0c76-4f34-b72c-03b5234d4f37@app.fastmail.com> (raw)
In-Reply-To: <pull.2250.git.1791384721919.gitgitgadget@gmail.com>

> Re: [PATCH] doc: checkout: rewrite detached HEAD state explanation

This change adds a standalone guide as well. The subject “just” make it
sound like you are rewriting the text where it is.

On Wed, Oct 7, 2026, at 16:52, Julia Evans via GitGitGadget wrote:
> From: Julia Evans <julia@jvns.ca>
>
> The current explanation has the following issues:
>
> - Says detached HEAD state is useful but doesn't explain why
> - Takes many paragraphs before explaining what detached HEAD state is
> - It's common for users to accidentally end up in detached HEAD state,
>   but it doesn't explain why that might happen
> - One of the UI improvements in `git switch` is to make it harder
>   to detach accidentally, but that isn't advertised.
>   See 7968bef06b (switch: only allow explicit detached HEAD, 2019-03-29)
> - Too many confusing diagrams
>
> Write a new explanation addressing these issues, and put it in a
> standalone guide so that we can easily reference it from advice
> ("see `git help detachedhead`").
> The result is a shorter guide that covers more material.
>
> The framing that "Git considers commits that aren't on a branch/other
> reference to be garbage" is taken from Steve Klabnik's tutorial
> https://steveklabnik.github.io/jujutsu-tutorial/branching-merging-and-conflicts/anonymous-branches.html.
> It's funny and it's consistent with the way Git uses the term
> "garbage collection".
>
> Co-authored-by: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
> Signed-off-by: Julia Evans <julia@jvns.ca>
> ---
>     doc: checkout: rewrite detached HEAD state explanation
>
>     Often when rewriting these explanations I go through a process where I
>     ask users' feedback on the old explanation. Here I didn't do that
>     basically because that process takes a long time and I'm working on a
>     bunch of other time consuming docs projects so I did a quick rewrite
>     based on my previous experience explaining detached HEAD state to folks.
>
>     I thought this could be a nice quick docs win on a topic which many
>     users find quite confusing. If the changes here are too controversial I
>     can drop it for now.
>
>     Also if folks object to making a separate git help detachedhead guide
>     I'm happy to drop that too. Personally I'm excited about the idea of
>     being able to reference the guides in our advice (which is one of our
>     best tools for getting users info about how to use Git!), but it's not
>     possible to let users jump to a subsection, so this is sort of a hack
>     around that.
>
> Published-As:
> https://github.com/gitgitgadget/git/releases/tag/pr-2250%2Fjvns%2Fdetached-head-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git
> pr-2250/jvns/detached-head-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/2250
>
>  Documentation/detached-head.adoc   |  43 ++++++++++
>  Documentation/git-checkout.adoc    | 129 +----------------------------
>  Documentation/gitdetachedhead.adoc |  14 ++++

Why in one word instead of hyphenating `detached-head`?
(gitdetached-head(7))

>  advice.c                           |   1 +
>  command-list.txt                   |   1 +

This is missing meson/Makefile changes in order to make
gitdetachedhead(7).

>  5 files changed, 60 insertions(+), 128 deletions(-)
>  create mode 100644 Documentation/detached-head.adoc
>  create mode 100644 Documentation/gitdetachedhead.adoc
>
> diff --git a/Documentation/detached-head.adoc
> b/Documentation/detached-head.adoc
> new file mode 100644
> index 0000000000..1e00712124
> --- /dev/null
> +++ b/Documentation/detached-head.adoc
> @@ -0,0 +1,43 @@
> +`HEAD` is where Git stores your current branch. `HEAD` can either be:

I see that this is similar to gitdatamodel(7). Except it adds “, if
there is a current branch”:

    HEAD is where Git stores your current branch, if there is a current
    branch. HEAD can either be: [...]

But a potential problem with that is:

A: What is HEAD?
B: It stores the current branch, if there is a current branch.
A: Oh so it is *empty* if there is no current branch?
B: No. Then it stores a commit.

This is of course revealed later here. But I wonder if starting with
current-branch can set someone up for a double take.

One could go in the other direction: you are almost always on a
commit. For most people the exception will be the unborn branch state
after `git init`. That commit is stored in the thing called `HEAD`. And
if you are on branch it also points to that branch. (It doesn’t point at
two things at the same time but instead at the branch, which is in turn
points at the commit for the branch, but conceptually it’s the same
difference).

> +
> +1. A branch, which is your current branch.
> +2. A commit ID, when you don't have a current branch.
> +   This is called "detached HEAD state".
> +
> +It can sometimes be useful for `HEAD` to be a commit ID.
> +For example, it lets you look at an old version of your code
> +(with `git checkout COMMIT_ID`).

Maybe go straight to `git switch --detached COMMIT_ID` here.

It is very useful to describe detached `HEAD` as something that you can
fall into by accident. It is very much perenially needed. And this
paragraph explains a legitimate, intentional use.

But a dedicated guide is also an opportunity to not only acknowledge hey
there is a use here but to explain actively why it is
convenient. Because one thing is “detached `HEAD`” as a conceptual,
scary blind alley where my work might get garbage collected. But only
thinking in terms of branches and tags can lead to a dead end of only
thinking of Git as:

1. The state of our collective work right now (“main”)
2. Our important waypoints (tags)

So before you lead them out of the maw of the garbage incinerator, you
can help level up their use of Git by explaining why checking out
commits freely is useful with an example of why they would do that.

1. Finding buggy commits manually (this would probably end with a
   mention of git-bisect(1))
2. Testing that each commit on a branch (forked from `main`) passes the
   test suite
3. Browsing an interesting commit manually instead of pointing `git
   grep` at the commit (or something)
4. Just being on `origin/main` instead of having a `main`. I never
   update “main”. So why would I go to the trouble of keeping it up to
   date? I can just “be on” `origin/main` (detached on whatever that
   points to).

   See also StackOverflow questions about how to update hundreds of
   local branches with git-pull(1) or something. Why? They do not have
   hundreds of local branches that they are authoring. They just want
   those branches by other people to be updated. But that is not
   necessary for anything. You just need the remote-tracking
   branches. Including if you want to check them out (on detached
   `HEAD`).

This conceptual dead end (of no “detached `HEAD`) could manifest itself
like this:

• “I must always be on a branch”
• “Therefore I can only check out old commits by first creating a
  branch”
• “Every interesting commit must have a tag for posteriety so we can
  name it” (you just need the hash, even abbreviated)
• “We must tag this commit for the build so we can name the build” (you
  can just use the hash or use part-of-the-hash with git-describe(1))

> +
> +The only problem is that if you create new commits while in detached
> +HEAD state, those commits won't be on a branch. This makes those new
> +commits much harder to find later. Also, Git considers commits that
> +aren't on any branch (or a tag or other reference) to be garbage.
> +Git will eventually permanently delete those "garbage" commits during
> +garbage collection.

Maybe this is where you should mention git-reflog(1) (and git-log(1)
maybe (see later)).

Maybe this about garbage collection could be tempered with a mention of
the however-many default days for reflog expiry. It’s such a long time
that if any of my commits get garbage collected... I probably didn’t
need them.

> +
> +There are 3 main ways you can end up in detached HEAD state
> +unintentionally:

I would say “typical” instead of “main”. As in these things are the
typical gateways to this state (unintentionally).

> +
> +1. `git checkout COMMIT_ID`, where `COMMIT_ID` is a commit ID
> +2. `git checkout v1.3`, where v1.3 is a tag name
> +3. `git checkout origin/main`, where `origin/main` is
> +   a remote-tracking branch
> +
> +Checking out a tag puts you in detached HEAD state because `HEAD` can
> +only be a branch or a commit, not a tag or any other reference.
> +So `git checkout TAG` will set HEAD to the commit for that tag.

Nice.

> +
> +The easiest way to avoid accidentally ending up in detached HEAD state
> +is to use linkgit:git-switch[1] instead of linkgit:git-checkout[1] to
> +switch branches. `git switch` won't let you detach unless you
> explicitly
> +pass the `--detach` argument.
> +
> +To get back onto a branch, you can:
> +
> +1. Switch to the branch you want to be on, with `git switch BRANCHNAME`.
> +2. Create a new branch at the current commit, with `git switch -c BRANCHNAME`.
> +   You might want to do this if you've created new commits, so that you can
> +   find the commit later and so that it won't be garbage collected.
> +
> +If you create commits in detached HEAD state that aren't on a branch,
> +you can find them later using linkgit:git-reflog[1].

Okay, but it’s more immediate and obvious to use `git log`. Say you
created three commits by accident while not on a branch. Then they will
be immediately um logged.

git-reflog(1) is excellent too of course, mostly because you are likely
to find all of them if you did a lot of weird checkouts in one session.

> diff --git a/Documentation/git-checkout.adoc
> b/Documentation/git-checkout.adoc
> index 2aefea0228..4e9e94e24d 100644
> --- a/Documentation/git-checkout.adoc
> +++ b/Documentation/git-checkout.adoc
> @@ -376,135 +376,8 @@ For more details, see the 'pathspec' entry in
> [snip]

I didn’t read the rest here.

      parent reply	other threads:[~2026-10-07 19:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-07 14:52 [PATCH] doc: checkout: rewrite detached HEAD state explanation Julia Evans via GitGitGadget
2026-10-07 16:43 ` Junio C Hamano
2026-10-07 19:02 ` Kristoffer Haugsbakk [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cb482e6d-0c76-4f34-b72c-03b5234d4f37@app.fastmail.com \
    --to=kristofferhaugsbakk@fastmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=julia@jvns.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox