I think we may be nearing (or at?) the final version.
On Mon, Nov 16, 2026 at 10:33 AM Julia Evans via GitGitGadget
<gitg…@gmail.com> wrote:
>
> From: Julia Evans <jul…@jvns.ca>
>
> Add a new guide (`git help detached-head`) to help users who
> accidentally end up in detached HEAD state without understanding why.
> Make it dual-purpose so that it can also be used as the DETACHED HEAD
> section in `git-checkout.adoc`. The result is a shorter guide that
> covers more material than the previous section.
>
> This fixes the following issues with the current DETACHED HEAD section:
>
> - 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
>
> To help users who end up in detached HEAD state by accident, explain
> the following, making all the explanations dual-purpose so they also
> work in the context of `git-checkout.adoc`.
>
> 1. What detached HEAD state is
> 2. What the risks of detached HEAD state are, to help show folks that
> the risks are pretty limited and there's no need to be afraid
> 3. How you might end up there accidentally (or on purpose)
> 4. How to avoid entering detached HEAD state accidentally by using
> `git switch`
> 5. The options for getting out of detached HEAD state
>
> 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 <hel…@marieflanagan.com>
> Signed-off-by: Marie Claire LeBlanc Flanagan <hel…@marieflanagan.com>
> Signed-off-by: Julia Evans <jul…@jvns.ca>
> ---
> Documentation/Makefile | 1 +
> Documentation/detached-head.adoc | 65 ++++++++++++++
> Documentation/git-checkout.adoc | 129 +---------------------------
> Documentation/gitdetached-head.adoc | 14 +++
> Documentation/meson.build | 1 +
> advice.c | 1 +
> command-list.txt | 1 +
> t/t2020-checkout-detach.sh | 2 +
> 8 files changed, 86 insertions(+), 128 deletions(-)
> create mode 100644 Documentation/detached-head.adoc
> create mode 100644 Documentation/gitdetached-head.adoc
>
> diff --git a/Documentation/Makefile b/Documentation/Makefile
> index f8dea4b395..5892f48b06 100644
> --- a/Documentation/Makefile
> +++ b/Documentation/Makefile
> @@ -54,6 +54,7 @@ MAN7_TXT += gitcore-tutorial.adoc
> MAN7_TXT += gitcredentials.adoc
> MAN7_TXT += gitcvs-migration.adoc
> MAN7_TXT += gitdatamodel.adoc
> +MAN7_TXT += gitdetached-head.adoc
> MAN7_TXT += gitdiffcore.adoc
> MAN7_TXT += giteveryday.adoc
> MAN7_TXT += gitfaq.adoc
> diff --git a/Documentation/detached-head.adoc b/Documentation/detached-head.adoc
> new file mode 100644
> index 0000000000..36fa422753
> --- /dev/null
> +++ b/Documentation/detached-head.adoc
> @@ -0,0 +1,65 @@
> +In Git, you can always use `HEAD` to refer to the commit you have
> +checked out. But `HEAD` is also where Git stores the name of your
> +current branch. Internally in Git, `HEAD` can store either:
> +
> +1. A branch name, which is your current branch.
> + The commit you have checked out is the latest commit on that 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, because
> +it lets you easily check out an old version of your project. For
> +example, a data scientist could use this to run an old version of a
> +data pipeline or a programmer could use this to figure out when a bug
> +was introduced. You can also use this to make experimental commits
> +without being on a branch. By detaching `HEAD`, you don't need to create
> +a temporary branch.
> +
> +But it's important to know 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. Those commits will be deleted after 30 days by
> +default, see linkgit:git-gc[1] for more.
> +
> +There are 3 main ways you can end up in detached HEAD state:
> +
> +1. `git checkout COMMIT_ID` or `git switch --detach 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
A small nit, probably not worth re-rolling for on its own: what do you
think about backtick-quoting "v1.3" (in part for parallelism with the
other examples)?
> +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 tagged commit.
Related: I'm not sure I understand the rules for when "HEAD" is
backtick-quoted and not. It seems that "detached HEAD state" or
similar avoided quotes, but the rest I'm not clear on.
> +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 switch branches after making commits in detached HEAD state, Git
> +will warn you that you might be about to "lose" those commits and print
> +out the commit ID. You can also always find those commits later using
> +linkgit:git-reflog[1].
> +
> +Here's what that warning looks like:
> +
> +-------------------------
> +Warning: you are leaving 1 commit behind, not connected to
> +any of your branches:
> +
> + 48c09f6 fix bug
> +-------------------------
Is it worth mentioning that we can make that commit findable again
(and not garbage) with "git branch BRANCHNAME ID" (or "… HEAD@{1}")?
Other than that, all looks good to me. I do sometimes wonder about
lots of "include" documents in our documentation (vs. having a central
doc and having, say, git-checkout(1) refer to gitdetachedhead(1)), but
that's not something we need to deal with in this series.