🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
31 messages
31 total messages Started by "Julia Evans via GitGitGadget" Wed, 07 Oct 2026 14:52
[PATCH] doc: checkout: rewrite detached HEAD state explanation
#25
Author: "Julia Evans via GitGitGadget"
Date: Wed, 07 Oct 2026 14:52
298 lines
18745 bytes
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 ++++
 advice.c                           |   1 +
 command-list.txt                   |   1 +
 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:
+
+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`).
+
+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.
+
+There are 3 main ways you can end up in detached HEAD 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.
+
+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].
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 linkgit:gitglossary[7].
 [[DETACHED_HEAD]]
 DETACHED HEAD
 -------------
-`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
-branch refers to a specific commit. Let's look at a repo with three
-commits, one of them tagged, and with branch `master` checked out:
 
-------------
-           HEAD (refers to branch 'master')
-            |
-            v
-a---b---c  branch 'master' (refers to commit 'c')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-When a commit is created in this state, the branch is updated to refer to
-the new commit. Specifically, `git commit` creates a new commit `d`, whose
-parent is commit `c`, and then updates branch `master` to refer to new
-commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers
-to commit `d`:
-
-------------
-$ edit; git add; git commit
-
-               HEAD (refers to branch 'master')
-                |
-                v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is sometimes useful to be able to checkout a commit that is not at
-the tip of any named branch, or even to create a new commit that is not
-referenced by a named branch. Let's look at what happens when we
-checkout commit `b` (here we show two ways this may be done):
-
-------------
-$ git checkout v2.0  # or
-$ git checkout master^^
-
-   HEAD (refers to commit 'b')
-    |
-    v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-Notice that regardless of which checkout command we use, `HEAD` now refers
-directly to commit `b`. This is known as being in detached `HEAD` state.
-It means simply that `HEAD` refers to a specific commit, as opposed to
-referring to a named branch. Let's see what happens when we create a commit:
-
-------------
-$ edit; git add; git commit
-
-     HEAD (refers to commit 'e')
-      |
-      v
-      e
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-There is now a new commit `e`, but it is referenced only by `HEAD`. We can
-of course add yet another commit in this state:
-
-------------
-$ edit; git add; git commit
-
-	 HEAD (refers to commit 'f')
-	  |
-	  v
-      e---f
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-In fact, we can perform all the normal Git operations. But, let's look
-at what happens when we then checkout `master`:
-
-------------
-$ git checkout master
-
-               HEAD (refers to branch 'master')
-      e---f     |
-     /          v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is important to realize that at this point nothing refers to commit
-`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
-by the routine Git garbage collection process, unless we create a reference
-before that happens. If we have not yet moved away from commit `f`,
-any of these will create a reference to it:
-
-------------
-$ git checkout -b foo  # or "git switch -c foo"  <1>
-$ git branch foo                                 <2>
-$ git tag foo                                    <3>
-------------
-<1> creates a new branch `foo`, which refers to commit `f`, and then
-    updates `HEAD` to refer to branch `foo`. In other words, we'll no longer
-    be in detached `HEAD` state after this command.
-<2> similarly creates a new branch `foo`, which refers to commit `f`,
-    but leaves `HEAD` detached.
-<3> creates a new tag `foo`, which refers to commit `f`,
-    leaving `HEAD` detached.
-
-If we have moved away from commit `f`, then we must first recover its object
-name (typically by using git reflog), and then we can create a reference to
-it. For example, to see the last two commits to which `HEAD` referred, we
-can use either of these commands:
-
-------------
-$ git reflog -2 HEAD # or
-$ git log -g -2 HEAD
-------------
+include::detached-head.adoc[]
 
 [[ARGUMENT_DISAMBIGUATION]]
 ARGUMENT DISAMBIGUATION
diff --git a/Documentation/gitdetachedhead.adoc b/Documentation/gitdetachedhead.adoc
new file mode 100644
index 0000000000..0aeecd159c
--- /dev/null
+++ b/Documentation/gitdetachedhead.adoc
@@ -0,0 +1,14 @@
+gitdetachedhead(7)
+===============
+
+NAME
+----
+gitdetachedhead - How detached HEAD state works
+
+DESCRIPTION
+-----------
+include::detached-head.adoc[]
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/advice.c b/advice.c
index 401d047391..43f86c2eaf 100644
--- a/advice.c
+++ b/advice.c
@@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
 	"\n"
 	"  git switch -\n"
 	"\n"
+	"Run `git help detachedhead` to learn more.\n"
 	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
 
 	fprintf(stderr, fmt, new_name);
diff --git a/command-list.txt b/command-list.txt
index 63ae2a67c9..313689335f 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -218,6 +218,7 @@ gitcore-tutorial                        guide
 gitcredentials                          guide
 gitcvs-migration                        guide
 gitdatamodel                            guide
+gitdetachedhead                         guide
 gitdiffcore                             guide
 giteveryday                             guide
 gitfaq                                  guide

base-commit: 5a7d1e8045ce66c908f62598e26cbb8df7b39a90
-- 
gitgitgadget
Re: [PATCH] doc: checkout: rewrite detached HEAD state explanation
#32
Author: Junio C Hamano
Date: Wed, 07 Oct 2026 16:43
194 lines
17167 bytes
"Julia Evans via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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:
> +
> +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".

HEAD is branch which can either be a branch or something else?

    HEAD records the state from which your current changes started.
    This can be a branch (the "current branch"), or it may not be
    associated with any concrete branch (in which case HEAD is
    "detached").

> +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`).

To "look at" an older state is more versatile than simply running
'git show COMMIT_ID:path', as it allows you to do anything you can
do on a branch, such as building and testing.  Thus,

 	A detached HEAD is useful when you want to tentatively visit
	an old state with 'git checkout --detach v0.1', without
	having to create a dedicated branch for it.

> +The only problem is that if you create new commits while in detached
> +HEAD state, those commits won't be on a branch.

I would suggest rephrasing "The only problem" to "One caveat is" or
something.  When sightseeing and experimenting, the ability to
create throw-away commits without the need to clean them up later,
or to invent a unique name for a temporary branch, is not a problem.
It is an advantage.

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

Correct.

Perhaps it is better to clarify this by adding "after you leave
the detached HEAD state" after "to find later".  While you are in
the detached HEAD state, you can run 'git log' to find the commits
you created there, as you can do everything you normally can on a
branch, or use 'git reflog @{now}' for that matter.

> +There are 3 main ways you can end up in detached HEAD 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

These I wouldn't call "unintentionally".  They are documented and
supported ways to do so, and will remain to be so.  "unknowingly"
may be a more fair way to call these gotchas, though.

> +3. `git checkout origin/main`, where `origin/main` is
> +   a remote-tracking branch

This can happen when the user forgets to say "-t" ('git checkout -t
origin/main' dwims to 'git checkout -t -b main origin/main'), so it
is closer to 'unintentionally' than the above two.

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

Correct.

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

Correct, but the motivation to 'avoid accidentally ending up' may
want to be spelled out.  Most often checking out a tag is to go
sightseeing, where you do not want to 'avoid' detached HEAD.

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

Very good.

> +If you create commits in detached HEAD state that aren't on a branch,
> +you can find them later using linkgit:git-reflog[1].

This, as you already said, is "much harder to find later" option.
There should be a better recovery option described here before
resorting to "git reflog HEAD" after you switched back.  E.g.,

    If you have created commits in detached HEAD state and then
    switched away from the state, all is not lost.  "git checkout"
    would have give you a warning message, like this:

	Warning: you are leaving 2 commits behind, not connected to
	any of your branches

	c816689 typofix the previous
	8e1bff5 decsribe detached HEAD state better

    You can create branches to keep them, e.g., "git branch saved c816689",
    by using the commit object names left there.

> 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 linkgit:gitglossary[7].
>  [[DETACHED_HEAD]]
>  DETACHED HEAD
>  -------------
> +include::detached-head.adoc[]

Losing the diagrams is a bit concerning.  However, if the intention
is to have readers learn how HEAD refers to a branch pointing at a
commit with history structured as a DAG elsewhere in a more basic
concepts guide, I suspect that it would actually work better.  This
could even be reduced to a simple "see also" pointer to that other
guide.

If an existing user only uses 'switch' and never reads 'git help
checkout', they are already not seeing these diagrams, or perhaps
they learned the concepts elsewhere.  They still manage to
understand Git well enough to make use of it, so perhaps this
approach is OK.  I dunno.

> diff --git a/Documentation/gitdetachedhead.adoc b/Documentation/gitdetachedhead.adoc
> new file mode 100644
> index 0000000000..0aeecd159c
> --- /dev/null
> +++ b/Documentation/gitdetachedhead.adoc
> @@ -0,0 +1,14 @@
> +gitdetachedhead(7)
> +===============
> +
> +NAME
> +----
> +gitdetachedhead - How detached HEAD state works
> +
> +DESCRIPTION
> +-----------
> +include::detached-head.adoc[]

This may be good as a first step, but we may want to have more here
than what we show in "git help checkout" later.  If we miss the
pictures we lost from "git help checkout", it can be moved here.

Thanks.

> +
> +GIT
> +---
> +Part of the linkgit:git[1] suite
> diff --git a/advice.c b/advice.c
> index 401d047391..43f86c2eaf 100644
> --- a/advice.c
> +++ b/advice.c
> @@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
>  	"\n"
>  	"  git switch -\n"
>  	"\n"
> +	"Run `git help detachedhead` to learn more.\n"
>  	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
>  
>  	fprintf(stderr, fmt, new_name);
> diff --git a/command-list.txt b/command-list.txt
> index 63ae2a67c9..313689335f 100644
> --- a/command-list.txt
> +++ b/command-list.txt
> @@ -218,6 +218,7 @@ gitcore-tutorial                        guide
>  gitcredentials                          guide
>  gitcvs-migration                        guide
>  gitdatamodel                            guide
> +gitdetachedhead                         guide
>  gitdiffcore                             guide
>  giteveryday                             guide
>  gitfaq                                  guide
>
> base-commit: 5a7d1e8045ce66c908f62598e26cbb8df7b39a90
Re: [PATCH] doc: checkout: rewrite detached HEAD state explanation
#48
Author: "Kristoffer Haugsbakk"
Date: Wed, 07 Oct 2026 19:02
267 lines
20235 bytes
> 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.
Re: [PATCH] doc: checkout: rewrite detached HEAD state explanation
#389
Author: "Kristoffer Haugsbakk"
Date: Tue, 13 Oct 2026 12:20
46 lines
11604 bytes
On Tue, Oct 13, 2026, at 13:48, Julia Evans wrote:
> On Wed, Oct 7, 2026, at 3:02 PM, Kristoffer Haugsbakk wrote:
>>>[snip]
>>>  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))
>
> Currently the guides are all one word with no hyphens.
> Happy to use a different style if folks think we shouldn't use
> the current naming convention for the guides

I’m confused. It doesn’t look like that to me?

    $ git help --guides
    The Git concept guides are:
       core-tutorial    A Git core tutorial for developers
       credentials      Providing usernames and passwords to Git
       cvs-migration    Git for CVS users
       diffcore         Tweaking diff output
       everyday         A useful minimum set of commands for Everyday Git
       faq              Frequently asked questions about using Git
       glossary         A Git Glossary
       namespaces       Git namespaces
       remote-helpers   Helper programs to interact with remote repositories
       submodules       Mounting one repository inside another
       tutorial         A tutorial introduction to Git
       tutorial-2       A tutorial introduction to Git: part two
       workflows        An overview of recommended workflows with Git

    'git help -a' and 'git help -g' list available subcommands and some
    concept guides. See 'git help <command>' or 'git help <concept>'
    to read about a specific subcommand or concept.
    See 'git help git' for an overview of the system.

What is definitely the case is that there is no hyphen between `git` and
the first word, maybe in order to distinguish section 1 commands) and
section 5 and 7 manpages.

>
>[snip]
[PATCH v2] doc: checkout: rewrite detached HEAD state explanation
#391
Author: "Julia Evans via GitGitGadget"
Date: Tue, 13 Oct 2026 13:01
459 lines
25561 bytes
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
    
    This is a pretty quick rewrite where we didn't get any user feedback. If
    we really want to make sure it's clear to users as possible, we should
    IMO do a second stage later where we ask users which parts are unclear.
    
    Changes in v2:
    
     * Make the meson.build and Makefile changes (thanks to Kristoffer)
     * Rename to gitdata-model (thanks to Kristoffer)
     * Mention commits will be deleted after 90 days to make it seem less
       scary (thanks to Kristoffer)
     * Add an "easily" qualifier to "it lets you look at an old version of
       your code", to avoid implying that this is the only way (thanks to
       Junio)
     * Mention that Git will warn you when switching branches if you're
       leaving commits behind (thanks to Junio)
     * Remove "unintentionally" (thanks to Junio)
     * Mention git help datamodel (thanks to Junio)
    
    Left the style comments alone (ideas for how to make it more clear) to
    be potentially addressed in v3, after we finalize the information
    content.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2250%2Fjvns%2Fdetached-head-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2250/jvns/detached-head-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2250

Range-diff vs v1:

 1:  efe0f9db36 ! 1:  83139be46f doc: checkout: rewrite detached HEAD state explanation
     @@ Commit message
          Co-authored-by: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
          Signed-off-by: Julia Evans <julia@jvns.ca>
      
     + ## Documentation/Makefile ##
     +@@ Documentation/Makefile: 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
     +
       ## Documentation/detached-head.adoc (new) ##
      @@
      +`HEAD` is where Git stores your current branch. `HEAD` can either be:
     @@ Documentation/detached-head.adoc (new)
      +   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`).
     ++For example, it lets you easily look at an old version of your code
     ++(with `git checkout COMMIT_ID` or `git switch --detach COMMIT_ID`).
      +
      +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.
     ++garbage collection. Those commits will be deleted after 90 days by
     ++default, see linkgit:git-gc[1] for more.
      +
     -+There are 3 main ways you can end up in detached HEAD state
     -+unintentionally:
     ++There are 3 main ways you can end up in detached HEAD state:
      +
      +1. `git checkout COMMIT_ID`, where `COMMIT_ID` is a commit ID
      +2. `git checkout v1.3`, where v1.3 is a tag name
     @@ Documentation/detached-head.adoc (new)
      +   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].
     ++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 3
     ++-------------------------
     ++
     ++For a more in-depth explanation of how `HEAD` works in Git,
     ++see linkgit:gitdatamodel[7] (`git help datamodel`).
      
       ## Documentation/git-checkout.adoc ##
      @@ Documentation/git-checkout.adoc: For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
     @@ Documentation/git-checkout.adoc: For more details, see the 'pathspec' entry in l
       [[ARGUMENT_DISAMBIGUATION]]
       ARGUMENT DISAMBIGUATION
      
     - ## Documentation/gitdetachedhead.adoc (new) ##
     + ## Documentation/gitdetached-head.adoc (new) ##
      @@
     -+gitdetachedhead(7)
     -+===============
     ++gitdetached-head(7)
     ++===================
      +
      +NAME
      +----
     -+gitdetachedhead - How detached HEAD state works
     ++gitdetached-head - How detached HEAD state works
      +
      +DESCRIPTION
      +-----------
     @@ Documentation/gitdetachedhead.adoc (new)
      +---
      +Part of the linkgit:git[1] suite
      
     + ## Documentation/meson.build ##
     +@@ Documentation/meson.build: manpages = {
     +   'gitcredentials.adoc' : 7,
     +   'gitcvs-migration.adoc' : 7,
     +   'gitdatamodel.adoc' : 7,
     ++  'gitdetached-head.adoc' : 7,
     +   'gitdiffcore.adoc' : 7,
     +   'giteveryday.adoc' : 7,
     +   'gitfaq.adoc' : 7,
     +
       ## advice.c ##
      @@ advice.c: void detach_advice(const char *new_name)
       	"\n"
     @@ command-list.txt: gitcore-tutorial                        guide
       gitcredentials                          guide
       gitcvs-migration                        guide
       gitdatamodel                            guide
     -+gitdetachedhead                         guide
     ++gitdetached-head                         guide
       gitdiffcore                             guide
       giteveryday                             guide
       gitfaq                                  guide


 Documentation/Makefile              |   1 +
 Documentation/detached-head.adoc    |  57 ++++++++++++
 Documentation/git-checkout.adoc     | 129 +---------------------------
 Documentation/gitdetached-head.adoc |  14 +++
 Documentation/meson.build           |   1 +
 advice.c                            |   1 +
 command-list.txt                    |   1 +
 7 files changed, 76 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..712074c0ef
--- /dev/null
+++ b/Documentation/detached-head.adoc
@@ -0,0 +1,57 @@
+`HEAD` is where Git stores your current branch. `HEAD` can either be:
+
+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 easily look at an old version of your code
+(with `git checkout COMMIT_ID` or `git switch --detach COMMIT_ID`).
+
+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. Those commits will be deleted after 90 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`, 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.
+
+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 3
+-------------------------
+
+For a more in-depth explanation of how `HEAD` works in Git,
+see linkgit:gitdatamodel[7] (`git help datamodel`).
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 linkgit:gitglossary[7].
 [[DETACHED_HEAD]]
 DETACHED HEAD
 -------------
-`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
-branch refers to a specific commit. Let's look at a repo with three
-commits, one of them tagged, and with branch `master` checked out:
 
-------------
-           HEAD (refers to branch 'master')
-            |
-            v
-a---b---c  branch 'master' (refers to commit 'c')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-When a commit is created in this state, the branch is updated to refer to
-the new commit. Specifically, `git commit` creates a new commit `d`, whose
-parent is commit `c`, and then updates branch `master` to refer to new
-commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers
-to commit `d`:
-
-------------
-$ edit; git add; git commit
-
-               HEAD (refers to branch 'master')
-                |
-                v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is sometimes useful to be able to checkout a commit that is not at
-the tip of any named branch, or even to create a new commit that is not
-referenced by a named branch. Let's look at what happens when we
-checkout commit `b` (here we show two ways this may be done):
-
-------------
-$ git checkout v2.0  # or
-$ git checkout master^^
-
-   HEAD (refers to commit 'b')
-    |
-    v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-Notice that regardless of which checkout command we use, `HEAD` now refers
-directly to commit `b`. This is known as being in detached `HEAD` state.
-It means simply that `HEAD` refers to a specific commit, as opposed to
-referring to a named branch. Let's see what happens when we create a commit:
-
-------------
-$ edit; git add; git commit
-
-     HEAD (refers to commit 'e')
-      |
-      v
-      e
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-There is now a new commit `e`, but it is referenced only by `HEAD`. We can
-of course add yet another commit in this state:
-
-------------
-$ edit; git add; git commit
-
-	 HEAD (refers to commit 'f')
-	  |
-	  v
-      e---f
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-In fact, we can perform all the normal Git operations. But, let's look
-at what happens when we then checkout `master`:
-
-------------
-$ git checkout master
-
-               HEAD (refers to branch 'master')
-      e---f     |
-     /          v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is important to realize that at this point nothing refers to commit
-`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
-by the routine Git garbage collection process, unless we create a reference
-before that happens. If we have not yet moved away from commit `f`,
-any of these will create a reference to it:
-
-------------
-$ git checkout -b foo  # or "git switch -c foo"  <1>
-$ git branch foo                                 <2>
-$ git tag foo                                    <3>
-------------
-<1> creates a new branch `foo`, which refers to commit `f`, and then
-    updates `HEAD` to refer to branch `foo`. In other words, we'll no longer
-    be in detached `HEAD` state after this command.
-<2> similarly creates a new branch `foo`, which refers to commit `f`,
-    but leaves `HEAD` detached.
-<3> creates a new tag `foo`, which refers to commit `f`,
-    leaving `HEAD` detached.
-
-If we have moved away from commit `f`, then we must first recover its object
-name (typically by using git reflog), and then we can create a reference to
-it. For example, to see the last two commits to which `HEAD` referred, we
-can use either of these commands:
-
-------------
-$ git reflog -2 HEAD # or
-$ git log -g -2 HEAD
-------------
+include::detached-head.adoc[]
 
 [[ARGUMENT_DISAMBIGUATION]]
 ARGUMENT DISAMBIGUATION
diff --git a/Documentation/gitdetached-head.adoc b/Documentation/gitdetached-head.adoc
new file mode 100644
index 0000000000..a477060db2
--- /dev/null
+++ b/Documentation/gitdetached-head.adoc
@@ -0,0 +1,14 @@
+gitdetached-head(7)
+===================
+
+NAME
+----
+gitdetached-head - How detached HEAD state works
+
+DESCRIPTION
+-----------
+include::detached-head.adoc[]
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Documentation/meson.build b/Documentation/meson.build
index cfa9c67609..6b3141647f 100644
--- a/Documentation/meson.build
+++ b/Documentation/meson.build
@@ -197,6 +197,7 @@ manpages = {
   'gitcredentials.adoc' : 7,
   'gitcvs-migration.adoc' : 7,
   'gitdatamodel.adoc' : 7,
+  'gitdetached-head.adoc' : 7,
   'gitdiffcore.adoc' : 7,
   'giteveryday.adoc' : 7,
   'gitfaq.adoc' : 7,
diff --git a/advice.c b/advice.c
index 401d047391..43f86c2eaf 100644
--- a/advice.c
+++ b/advice.c
@@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
 	"\n"
 	"  git switch -\n"
 	"\n"
+	"Run `git help detachedhead` to learn more.\n"
 	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
 
 	fprintf(stderr, fmt, new_name);
diff --git a/command-list.txt b/command-list.txt
index 63ae2a67c9..922a1dbd84 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -218,6 +218,7 @@ gitcore-tutorial                        guide
 gitcredentials                          guide
 gitcvs-migration                        guide
 gitdatamodel                            guide
+gitdetached-head                         guide
 gitdiffcore                             guide
 giteveryday                             guide
 gitfaq                                  guide

base-commit: 5a7d1e8045ce66c908f62598e26cbb8df7b39a90
-- 
gitgitgadget
Re: [PATCH v2] doc: checkout: rewrite detached HEAD state explanation
#537
Author: Junio C Hamano
Date: Wed, 14 Oct 2026 18:52
67 lines
12315 bytes
"Julia Evans via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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`").

Isn't it 'git help detached-head' in this edition?

> Co-authored-by: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
> Signed-off-by: Julia Evans <julia@jvns.ca>

Can the other author also sign it off?

> +Git will eventually permanently delete those "garbage" commits during
> +garbage collection. Those commits will be deleted after 90 days by
> +default, see linkgit:git-gc[1] for more.

Does this say 90 days because that is the gc.reflogExpire retention?

If you create a commit, change your mind, rewind the tip of the
branch (e.g., with "git commit --amend"), and then keep working, the
original commit before amending would be unreachable from the tip of
that branch.  Its reflog entry would then expire after
gc.reflogExpireUnreachable, which defaults to 30 days.  If your
commit were created on a detached HEAD, I am not sure offhand how
the thresholds for gc.reflogExpire and gc.reflogExpireUnreachable
come into play for HEAD, as HEAD by default flips back and forth
regularly across branch tips that are not reachable from each other.
When "gc" kicks in, the commit HEAD currently points to is unlikely
to reach the abandoned commit.

It might be safer to either say "after 30 days by default" or
generalize it to "after a few weeks by default" to avoid being
overly prescriptive.

> diff --git a/advice.c b/advice.c
> index 401d047391..43f86c2eaf 100644
> --- a/advice.c
> +++ b/advice.c
> @@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
>  	"\n"
>  	"  git switch -\n"
>  	"\n"
> +	"Run `git help detachedhead` to learn more.\n"

"git help deatched-head" here, too.

>  	fprintf(stderr, fmt, new_name);
> diff --git a/command-list.txt b/command-list.txt
> index 63ae2a67c9..922a1dbd84 100644
> --- a/command-list.txt
> +++ b/command-list.txt
> @@ -218,6 +218,7 @@ gitcore-tutorial                        guide
>  gitcredentials                          guide
>  gitcvs-migration                        guide
>  gitdatamodel                            guide
> +gitdetached-head                         guide
>  gitdiffcore                             guide
>  giteveryday                             guide
>  gitfaq                                  guide

This seems to be manually aligned with SP, so compensate the
addition of "-" between detached and head by removing one SP before
guide on the same line.

Thanks.
[PATCH v3] doc: checkout: rewrite detached HEAD state explanation
#633
Author: "Julia Evans via GitGitGadget"
Date: Thu, 15 Oct 2026 20:58
433 lines
24892 bytes
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 detached-head`").
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: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
Signed-off-by: Julia Evans <julia@jvns.ca>
---
    doc: checkout: rewrite detached HEAD state explanation
    
    This is a pretty quick rewrite where we didn't get any user feedback. If
    we really want to make sure it's clear to users as possible, we should
    IMO do a second stage later where we ask users which parts are unclear.
    
    Changes in v2:
    
     * Make the meson.build and Makefile changes (thanks to Kristoffer)
     * Rename to gitdata-model (thanks to Kristoffer)
     * Mention commits will be deleted after 90 days to make it seem less
       scary (thanks to Kristoffer)
     * Add an "easily" qualifier to "it lets you look at an old version of
       your code", to avoid implying that this is the only way (thanks to
       Junio)
     * Mention that Git will warn you when switching branches if you're
       leaving commits behind (thanks to Junio)
     * Remove "unintentionally" (thanks to Junio)
     * Mention git help datamodel (thanks to Junio)
    
    Changes in v3:
    
    I went through the style comments from v1.
    
    content changes:
    
     * Change "90 days" to "30 days" (thanks to Junio)
     * Say that HEAD is where Git stores your "current branch or commit" to
       make the first sentence read better (thanks to Junio)
     * Mention git switch --detach a little earlier (thanks to Kristoffer)
     * Replace "the only problem" with "it's important to know that" (thanks
       to Junio)
    
    formatting changes etc:
    
     * Replace help detachedhead with help detached-head (thanks to Junio)
     * Fix spacing in command-list.txt (thanks to Junio)
     * Get Marie's signoff (thanks to Junio)
    
    non-changes: Left these phrases alone because I don't think the proposed
    changes would make it more clear.
    
     * "3 main ways you can end up in detached HEAD state"
     * "to look at an old version of your code"
     * "avoid accidentally ending up"

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2250%2Fjvns%2Fdetached-head-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2250/jvns/detached-head-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/2250

Range-diff vs v2:

 1:  83139be46f ! 1:  0ab8814664 doc: checkout: rewrite detached HEAD state explanation
     @@ Commit message
      
          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`").
     +    ("see `git help detached-head`").
          The result is a shorter guide that covers more material.
      
          The framing that "Git considers commits that aren't on a branch/other
     @@ Commit message
          "garbage collection".
      
          Co-authored-by: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
     +    Signed-off-by: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
          Signed-off-by: Julia Evans <julia@jvns.ca>
      
       ## Documentation/Makefile ##
     @@ Documentation/Makefile: MAN7_TXT += gitcore-tutorial.adoc
      
       ## Documentation/detached-head.adoc (new) ##
      @@
     -+`HEAD` is where Git stores your current branch. `HEAD` can either be:
     ++`HEAD` is where Git stores your current branch or commit. `HEAD` can either be:
      +
      +1. A branch, which is your current branch.
      +2. A commit ID, when you don't have a current branch.
     @@ Documentation/detached-head.adoc (new)
      +For example, it lets you easily look at an old version of your code
      +(with `git checkout COMMIT_ID` or `git switch --detach COMMIT_ID`).
      +
     -+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.
     ++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 90 days by
     ++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:
     @@ advice.c: void detach_advice(const char *new_name)
       	"\n"
       	"  git switch -\n"
       	"\n"
     -+	"Run `git help detachedhead` to learn more.\n"
     ++	"Run `git help detached-head` to learn more.\n"
       	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
       
       	fprintf(stderr, fmt, new_name);
     @@ command-list.txt: gitcore-tutorial                        guide
       gitcredentials                          guide
       gitcvs-migration                        guide
       gitdatamodel                            guide
     -+gitdetached-head                         guide
     ++gitdetached-head                        guide
       gitdiffcore                             guide
       giteveryday                             guide
       gitfaq                                  guide


 Documentation/Makefile              |   1 +
 Documentation/detached-head.adoc    |  57 ++++++++++++
 Documentation/git-checkout.adoc     | 129 +---------------------------
 Documentation/gitdetached-head.adoc |  14 +++
 Documentation/meson.build           |   1 +
 advice.c                            |   1 +
 command-list.txt                    |   1 +
 7 files changed, 76 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..d8e59e10c6
--- /dev/null
+++ b/Documentation/detached-head.adoc
@@ -0,0 +1,57 @@
+`HEAD` is where Git stores your current branch or commit. `HEAD` can either be:
+
+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 easily look at an old version of your code
+(with `git checkout COMMIT_ID` or `git switch --detach COMMIT_ID`).
+
+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`, 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.
+
+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 3
+-------------------------
+
+For a more in-depth explanation of how `HEAD` works in Git,
+see linkgit:gitdatamodel[7] (`git help datamodel`).
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 linkgit:gitglossary[7].
 [[DETACHED_HEAD]]
 DETACHED HEAD
 -------------
-`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
-branch refers to a specific commit. Let's look at a repo with three
-commits, one of them tagged, and with branch `master` checked out:
 
-------------
-           HEAD (refers to branch 'master')
-            |
-            v
-a---b---c  branch 'master' (refers to commit 'c')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-When a commit is created in this state, the branch is updated to refer to
-the new commit. Specifically, `git commit` creates a new commit `d`, whose
-parent is commit `c`, and then updates branch `master` to refer to new
-commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers
-to commit `d`:
-
-------------
-$ edit; git add; git commit
-
-               HEAD (refers to branch 'master')
-                |
-                v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is sometimes useful to be able to checkout a commit that is not at
-the tip of any named branch, or even to create a new commit that is not
-referenced by a named branch. Let's look at what happens when we
-checkout commit `b` (here we show two ways this may be done):
-
-------------
-$ git checkout v2.0  # or
-$ git checkout master^^
-
-   HEAD (refers to commit 'b')
-    |
-    v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-Notice that regardless of which checkout command we use, `HEAD` now refers
-directly to commit `b`. This is known as being in detached `HEAD` state.
-It means simply that `HEAD` refers to a specific commit, as opposed to
-referring to a named branch. Let's see what happens when we create a commit:
-
-------------
-$ edit; git add; git commit
-
-     HEAD (refers to commit 'e')
-      |
-      v
-      e
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-There is now a new commit `e`, but it is referenced only by `HEAD`. We can
-of course add yet another commit in this state:
-
-------------
-$ edit; git add; git commit
-
-	 HEAD (refers to commit 'f')
-	  |
-	  v
-      e---f
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-In fact, we can perform all the normal Git operations. But, let's look
-at what happens when we then checkout `master`:
-
-------------
-$ git checkout master
-
-               HEAD (refers to branch 'master')
-      e---f     |
-     /          v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is important to realize that at this point nothing refers to commit
-`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
-by the routine Git garbage collection process, unless we create a reference
-before that happens. If we have not yet moved away from commit `f`,
-any of these will create a reference to it:
-
-------------
-$ git checkout -b foo  # or "git switch -c foo"  <1>
-$ git branch foo                                 <2>
-$ git tag foo                                    <3>
-------------
-<1> creates a new branch `foo`, which refers to commit `f`, and then
-    updates `HEAD` to refer to branch `foo`. In other words, we'll no longer
-    be in detached `HEAD` state after this command.
-<2> similarly creates a new branch `foo`, which refers to commit `f`,
-    but leaves `HEAD` detached.
-<3> creates a new tag `foo`, which refers to commit `f`,
-    leaving `HEAD` detached.
-
-If we have moved away from commit `f`, then we must first recover its object
-name (typically by using git reflog), and then we can create a reference to
-it. For example, to see the last two commits to which `HEAD` referred, we
-can use either of these commands:
-
-------------
-$ git reflog -2 HEAD # or
-$ git log -g -2 HEAD
-------------
+include::detached-head.adoc[]
 
 [[ARGUMENT_DISAMBIGUATION]]
 ARGUMENT DISAMBIGUATION
diff --git a/Documentation/gitdetached-head.adoc b/Documentation/gitdetached-head.adoc
new file mode 100644
index 0000000000..a477060db2
--- /dev/null
+++ b/Documentation/gitdetached-head.adoc
@@ -0,0 +1,14 @@
+gitdetached-head(7)
+===================
+
+NAME
+----
+gitdetached-head - How detached HEAD state works
+
+DESCRIPTION
+-----------
+include::detached-head.adoc[]
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Documentation/meson.build b/Documentation/meson.build
index cfa9c67609..6b3141647f 100644
--- a/Documentation/meson.build
+++ b/Documentation/meson.build
@@ -197,6 +197,7 @@ manpages = {
   'gitcredentials.adoc' : 7,
   'gitcvs-migration.adoc' : 7,
   'gitdatamodel.adoc' : 7,
+  'gitdetached-head.adoc' : 7,
   'gitdiffcore.adoc' : 7,
   'giteveryday.adoc' : 7,
   'gitfaq.adoc' : 7,
diff --git a/advice.c b/advice.c
index 401d047391..8d2c1ff1ee 100644
--- a/advice.c
+++ b/advice.c
@@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
 	"\n"
 	"  git switch -\n"
 	"\n"
+	"Run `git help detached-head` to learn more.\n"
 	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
 
 	fprintf(stderr, fmt, new_name);
diff --git a/command-list.txt b/command-list.txt
index 63ae2a67c9..7fb57765fb 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -218,6 +218,7 @@ gitcore-tutorial                        guide
 gitcredentials                          guide
 gitcvs-migration                        guide
 gitdatamodel                            guide
+gitdetached-head                        guide
 gitdiffcore                             guide
 giteveryday                             guide
 gitfaq                                  guide

base-commit: 5a7d1e8045ce66c908f62598e26cbb8df7b39a90
-- 
gitgitgadget
Re: [PATCH v3] doc: checkout: rewrite detached HEAD state explanation
#638
Author: Junio C Hamano
Date: Thu, 15 Oct 2026 22:39
117 lines
14821 bytes
"Julia Evans via GitGitGadget" <gitgitgadget@gmail.com> writes:

>     non-changes: Left these phrases alone because I don't think the proposed
>     changes would make it more clear.

Those who contributed their time to review this document did so
believing they were suggesting improvements.  Simply dismissing
their feedback as "style" in 'v2' without explaining what you
disliked about their suggestions is unhelpful.  It is hard to know
what to make of a dismissal that lacks any supporting reasoning,
other than "I don't think".

> diff --git a/Documentation/detached-head.adoc b/Documentation/detached-head.adoc
> new file mode 100644
> index 0000000000..d8e59e10c6
> --- /dev/null
> +++ b/Documentation/detached-head.adoc
> @@ -0,0 +1,57 @@
> +`HEAD` is where Git stores your current branch or commit. `HEAD` can either be:
> +
> +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".

Differences between v1 and v3 did not make much improvement in this
area, though.  The phrase "branch or commit" highlights the
distinction, but it fails to clarify what is common to both, which
is equally important.

Perhaps restarting from something along this line

    HEAD records the state from which your current changes started.
    This can be a branch (the "current branch"), or it be a commit,
    (in which case HEAD is "detached").

and polishing further may take us to a better end state.  An
important distinction that is not captured in the above (or your
version) that we may want to cover is that when you are on a branch,
your changes will grow that branch, and when you are not on a
branch, your changes will not affect any branch and stay temporary.

> +It can sometimes be useful for `HEAD` to be a commit ID.
> +For example, it lets you easily look at an old version of your code
> +(with `git checkout COMMIT_ID` or `git switch --detach COMMIT_ID`).

We should use a verb stronger than "look at" to emphasize the
primary advantage of having the entire state visible in the working
tree.  If a user merely wants to read or inspect a single historical
file, they can do so easily without moving their HEAD by running
'git show COMMIT_ID:path/to/file'.  Checking out a commit allows
something much more powerful.  It restores your entire working
directory to that exact state.

    In the detached HEAD state, you can safely build, run, and
    experiment with any historical version of your codebase, without
    having to worry about clobbering your branches, and you do not
    have to clutter your repository with temporary branches.

I said "safely build, run" because you had "old version of your
CODE" in the paragraph, but if you can give examples outside the
programming area, that would be even better.  E.g.,

- Writers may able to rebuild and locally preview a complete
  historical version of a book or documentation site to see how
  cross-references and formatting rendered at that time.

- Game Designers may open an entire historical scene in an editor
  (like Unity or Blender) where assets, textures, and coordinates
  must all exist in a precise, interconnected state to load
  correctly.

- Data Scientists may execute and reproduce a specific historical
  data pipeline or model run using the exact combination of scripts
  and datasets that existed on that day.


> +There are 3 main ways you can end up in detached HEAD state:
> +
> +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.

A more standard phrase for "the commit for that tag" is "the tagged
commit".

Because we will recommend "switch" to be a way to require to be more
explicit when users mean to go into the detached HEAD state soon
below, I think we would want to move the "checkout <COMMIT>" and
"switch --detach <COMMIT>" description to here from the earlier
"sometimes useful" paragraph, to complement the above list of ways
to detach your HEAD.

> +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 3
> +-------------------------

Your readers can guess that the 7 digit hex may probably be the
commit object name you are about to lose, but you should use a bit
more realistic descriptive title, not "3", to avoid confusion.  I
know many users do not write a good log message, but even a single
word "bugfix" is better than "3" ;-)
Re: [PATCH v3] doc: checkout: rewrite detached HEAD state explanation
#678
Author: Junio C Hamano
Date: Fri, 16 Oct 2026 17:03
22 lines
10584 bytes
"Julia Evans" <julia@jvns.ca> writes:

> This is a case where if we really want to make sure it's clear, we need
> to go get feedback from users. There's likely a better way to explain it
> but I don't think that this conversation will result in an improvement.
> Really doing a good job of explaining HEAD is a pretty big project.
>
> It's not clear to me if you think the current wording is unusable
> or if you just think it could be better.

Then let me make it clear.

I do not know if your version is unusable or good enough, but if you
are going to take one version to guinea-pig readers to test and
polish starting from that version, I think what I gave you would
give us a better starting point.

Otherwise I would not be suggesting replacements (this applies to
all other points in the message you are responding to).

Thanks.
[PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#685
Author: "Julia Evans via GitGitGadget"
Date: Fri, 16 Oct 2026 19:03
434 lines
24849 bytes
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 detached-head`").
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: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
Signed-off-by: Julia Evans <julia@jvns.ca>
---
    doc: checkout: rewrite detached HEAD state explanation
    
    This is a pretty quick rewrite where we didn't get any user feedback. If
    we really want to make sure it's clear to users as possible, we should
    IMO do a second stage later where we ask users which parts are unclear.
    
    Changes in v2:
    
     * Make the meson.build and Makefile changes (thanks to Kristoffer)
     * Rename to gitdata-model (thanks to Kristoffer)
     * Mention commits will be deleted after 90 days to make it seem less
       scary (thanks to Kristoffer)
     * Add an "easily" qualifier to "it lets you look at an old version of
       your code", to avoid implying that this is the only way (thanks to
       Junio)
     * Mention that Git will warn you when switching branches if you're
       leaving commits behind (thanks to Junio)
     * Remove "unintentionally" (thanks to Junio)
     * Mention git help datamodel (thanks to Junio)
    
    Changes in v3:
    
    I went through the style comments from v1.
    
    content changes:
    
     * Change "90 days" to "30 days" (thanks to Junio)
     * Say that HEAD is where Git stores your "current branch or commit" to
       make the first sentence read better (thanks to Junio)
     * Mention git switch --detach a little earlier (thanks to Kristoffer)
     * Replace "the only problem" with "it's important to know that" (thanks
       to Junio)
    
    formatting changes etc:
    
     * Replace help detachedhead with help detached-head (thanks to Junio)
     * Fix spacing in command-list.txt (thanks to Junio)
     * Get Marie's signoff (thanks to Junio)
    
    non-changes: Left these phrases alone because I don't think the proposed
    changes would make it more clear.
    
     * "3 main ways you can end up in detached HEAD state"
     * "to look at an old version of your code"
     * "avoid accidentally ending up"
    
    Changes in v4:
    
     * Fix commit message in example: "fix bug", not "3" (thanks to Junio)
     * "commit for that tag" -> "tagged commit" (thanks to Junio)
     * Move "git checkout COMMIT_ID or git switch --detach COMMIT_ID" down
       to the "3 main ways" list to reduce duplication (thanks to Junio)
     * Add some examples and be clearer about how detached HEAD state lets
       you check out an old version of the project. (thanks to Junio)
    
    non-changes:
    
     * It's probably possible to explain HEAD better. That seems like a big
       project to me since HEAD in Git is a very tricky topic. I think it
       would be better to address in a separate patch series to have more
       time to think it through. Or if we think it's not possible to merge
       this without explaining HEAD better then I guess it shouldn't be
       merged.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2250%2Fjvns%2Fdetached-head-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2250/jvns/detached-head-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/2250

Range-diff vs v3:

 1:  0ab8814664 ! 1:  88210ad52d doc: checkout: rewrite detached HEAD state explanation
     @@ Documentation/detached-head.adoc (new)
      +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 easily look at an old version of your code
     -+(with `git checkout COMMIT_ID` or `git switch --detach COMMIT_ID`).
     ++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.
      +
      +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
     @@ Documentation/detached-head.adoc (new)
      +
      +There are 3 main ways you can end up in detached HEAD state:
      +
     -+1. `git checkout COMMIT_ID`, where `COMMIT_ID` is a commit ID
     ++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
      +
      +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.
     ++So `git checkout TAG` will set HEAD to the tagged commit.
      +
      +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
     @@ Documentation/detached-head.adoc (new)
      +Warning: you are leaving 1 commit behind, not connected to
      +any of your branches:
      +
     -+  48c09f6 3
     ++  48c09f6 fix bug
      +-------------------------
      +
      +For a more in-depth explanation of how `HEAD` works in Git,


 Documentation/Makefile              |   1 +
 Documentation/detached-head.adoc    |  60 +++++++++++++
 Documentation/git-checkout.adoc     | 129 +---------------------------
 Documentation/gitdetached-head.adoc |  14 +++
 Documentation/meson.build           |   1 +
 advice.c                            |   1 +
 command-list.txt                    |   1 +
 7 files changed, 79 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..a918a75799
--- /dev/null
+++ b/Documentation/detached-head.adoc
@@ -0,0 +1,60 @@
+`HEAD` is where Git stores your current branch or commit. `HEAD` can either be:
+
+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, 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.
+
+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
+
+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.
+
+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
+-------------------------
+
+For a more in-depth explanation of how `HEAD` works in Git,
+see linkgit:gitdatamodel[7] (`git help datamodel`).
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 linkgit:gitglossary[7].
 [[DETACHED_HEAD]]
 DETACHED HEAD
 -------------
-`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
-branch refers to a specific commit. Let's look at a repo with three
-commits, one of them tagged, and with branch `master` checked out:
 
-------------
-           HEAD (refers to branch 'master')
-            |
-            v
-a---b---c  branch 'master' (refers to commit 'c')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-When a commit is created in this state, the branch is updated to refer to
-the new commit. Specifically, `git commit` creates a new commit `d`, whose
-parent is commit `c`, and then updates branch `master` to refer to new
-commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers
-to commit `d`:
-
-------------
-$ edit; git add; git commit
-
-               HEAD (refers to branch 'master')
-                |
-                v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is sometimes useful to be able to checkout a commit that is not at
-the tip of any named branch, or even to create a new commit that is not
-referenced by a named branch. Let's look at what happens when we
-checkout commit `b` (here we show two ways this may be done):
-
-------------
-$ git checkout v2.0  # or
-$ git checkout master^^
-
-   HEAD (refers to commit 'b')
-    |
-    v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-Notice that regardless of which checkout command we use, `HEAD` now refers
-directly to commit `b`. This is known as being in detached `HEAD` state.
-It means simply that `HEAD` refers to a specific commit, as opposed to
-referring to a named branch. Let's see what happens when we create a commit:
-
-------------
-$ edit; git add; git commit
-
-     HEAD (refers to commit 'e')
-      |
-      v
-      e
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-There is now a new commit `e`, but it is referenced only by `HEAD`. We can
-of course add yet another commit in this state:
-
-------------
-$ edit; git add; git commit
-
-	 HEAD (refers to commit 'f')
-	  |
-	  v
-      e---f
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-In fact, we can perform all the normal Git operations. But, let's look
-at what happens when we then checkout `master`:
-
-------------
-$ git checkout master
-
-               HEAD (refers to branch 'master')
-      e---f     |
-     /          v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is important to realize that at this point nothing refers to commit
-`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
-by the routine Git garbage collection process, unless we create a reference
-before that happens. If we have not yet moved away from commit `f`,
-any of these will create a reference to it:
-
-------------
-$ git checkout -b foo  # or "git switch -c foo"  <1>
-$ git branch foo                                 <2>
-$ git tag foo                                    <3>
-------------
-<1> creates a new branch `foo`, which refers to commit `f`, and then
-    updates `HEAD` to refer to branch `foo`. In other words, we'll no longer
-    be in detached `HEAD` state after this command.
-<2> similarly creates a new branch `foo`, which refers to commit `f`,
-    but leaves `HEAD` detached.
-<3> creates a new tag `foo`, which refers to commit `f`,
-    leaving `HEAD` detached.
-
-If we have moved away from commit `f`, then we must first recover its object
-name (typically by using git reflog), and then we can create a reference to
-it. For example, to see the last two commits to which `HEAD` referred, we
-can use either of these commands:
-
-------------
-$ git reflog -2 HEAD # or
-$ git log -g -2 HEAD
-------------
+include::detached-head.adoc[]
 
 [[ARGUMENT_DISAMBIGUATION]]
 ARGUMENT DISAMBIGUATION
diff --git a/Documentation/gitdetached-head.adoc b/Documentation/gitdetached-head.adoc
new file mode 100644
index 0000000000..a477060db2
--- /dev/null
+++ b/Documentation/gitdetached-head.adoc
@@ -0,0 +1,14 @@
+gitdetached-head(7)
+===================
+
+NAME
+----
+gitdetached-head - How detached HEAD state works
+
+DESCRIPTION
+-----------
+include::detached-head.adoc[]
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Documentation/meson.build b/Documentation/meson.build
index cfa9c67609..6b3141647f 100644
--- a/Documentation/meson.build
+++ b/Documentation/meson.build
@@ -197,6 +197,7 @@ manpages = {
   'gitcredentials.adoc' : 7,
   'gitcvs-migration.adoc' : 7,
   'gitdatamodel.adoc' : 7,
+  'gitdetached-head.adoc' : 7,
   'gitdiffcore.adoc' : 7,
   'giteveryday.adoc' : 7,
   'gitfaq.adoc' : 7,
diff --git a/advice.c b/advice.c
index 401d047391..8d2c1ff1ee 100644
--- a/advice.c
+++ b/advice.c
@@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
 	"\n"
 	"  git switch -\n"
 	"\n"
+	"Run `git help detached-head` to learn more.\n"
 	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
 
 	fprintf(stderr, fmt, new_name);
diff --git a/command-list.txt b/command-list.txt
index 63ae2a67c9..7fb57765fb 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -218,6 +218,7 @@ gitcore-tutorial                        guide
 gitcredentials                          guide
 gitcvs-migration                        guide
 gitdatamodel                            guide
+gitdetached-head                        guide
 gitdiffcore                             guide
 giteveryday                             guide
 gitfaq                                  guide

base-commit: 5a7d1e8045ce66c908f62598e26cbb8df7b39a90
-- 
gitgitgadget
Re: [PATCH v3] doc: checkout: rewrite detached HEAD state explanation
#688
Author: Junio C Hamano
Date: Fri, 16 Oct 2026 19:05
29 lines
11091 bytes
"Julia Evans" <julia@jvns.ca> writes:

> I'm not planning to test this version on readers right now, no.

That is fine.  But eventually we will reach the point where we
have something we think is good enough to show to test readers,
no?  And I thought you were asking for help to improve the draft
to reach that stage by sending the patch.

> Here's what I wrote in the first email in this thread:
>
>     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.

After a long process, you may forget which suggestions you chose not
to adopt by the time you are ready to show the draft to your test
readers, and those who spent their brain cycles reviewing it might
not notice that their input has been forgotten, either.  I was
worried about that kind of waste.

Thanks.
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#697
Author: Junio C Hamano
Date: Fri, 16 Oct 2026 21:10
41 lines
11691 bytes
"Julia Evans via GitGitGadget" <gitgitgadget@gmail.com> writes:

>     This is a pretty quick rewrite where we didn't get any user feedback. If
>     we really want to make sure it's clear to users as possible, we should
>     IMO do a second stage later where we ask users which parts are unclear.

OK.

>      * It's probably possible to explain HEAD better. That seems like a big
>        project to me since HEAD in Git is a very tricky topic. I think it
>        would be better to address in a separate patch series to have more
>        time to think it through. Or if we think it's not possible to merge
>        this without explaining HEAD better then I guess it shouldn't be
>        merged.

I do not understand why you need to draw a "take it or leave it"
with this topic here.  If you feel that the explanation of HEAD is
iffy, you know what we have here will not be the final version, and
you think we lack the capacity to reach the best possible
description with the usual review exchange, then I do not see why
you have to insist keeping what you initially have written.  You can
replace it with anything that is better than your initial version
and then still record that we will further polish it later in the
proposed log message and we will be in a better place, no?

Just to leave a record, so that we have better chance remembering
the suggested improved text, here are what I think the first two
paragraphs should be replaced with.

    HEAD records the state from which your current changes started.
    This can be a branch (the "current branch"), or it be a commit,
    (in which case HEAD is "detached").

    In the detached HEAD state, you can safely build, run, and
    experiment with any historical version of your codebase, without
    having to worry about clobbering your branches, and you do not
    have to clutter your repository with temporary branches.

Having said all that, the changes between v3 and v4 look OK to me,
so let me update the previous copy I have with this version.
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#707
Author: Junio C Hamano
Date: Fri, 16 Oct 2026 22:02
62 lines
11731 bytes
"Julia Evans via GitGitGadget" <gitgitgadget@gmail.com> writes:

>  advice.c                            |   1 +
> ...
> diff --git a/advice.c b/advice.c
> index 401d047391..8d2c1ff1ee 100644
> --- a/advice.c
> +++ b/advice.c
> @@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
>  	"\n"
>  	"  git switch -\n"
>  	"\n"
> +	"Run `git help detached-head` to learn more.\n"
>  	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
>  
>  	fprintf(stderr, fmt, new_name);

This breaks t2020 without an accompanying change.

You can squash in the attached patch to unbreak the test.

Since nobody's terminal talks markdown or asciidoc, let's use the
vanilla single quote instead of backquote while we are at it.

 advice.c                   | 2 +-
 t/t2020-checkout-detach.sh | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git c/advice.c w/advice.c
index 8d2c1ff1ee..3238e9b866 100644
--- c/advice.c
+++ w/advice.c
@@ -291,7 +291,7 @@ void detach_advice(const char *new_name)
 	"\n"
 	"  git switch -\n"
 	"\n"
-	"Run `git help detached-head` to learn more.\n"
+	"Run 'git help detached-head' to learn more.\n"
 	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
 
 	fprintf(stderr, fmt, new_name);
diff --git c/t/t2020-checkout-detach.sh w/t/t2020-checkout-detach.sh
index 28bbbe6c05..804dc2a67f 100755
--- c/t/t2020-checkout-detach.sh
+++ w/t/t2020-checkout-detach.sh
@@ -228,6 +228,7 @@ test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not as
 
 	  git switch -
 
+	Run ${SQ}git help detached-head${SQ} to learn more.
 	Turn off this advice by setting config variable advice.detachedHead to false
 
 	HEAD is now at \$commit three
@@ -310,6 +311,7 @@ test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked
 
 	  git switch -
 
+	Run ${SQ}git help detached-head${SQ} to learn more.
 	Turn off this advice by setting config variable advice.detachedHead to false
 
 	HEAD is now at \$commit... three
Re: [PATCH] doc: checkout: rewrite detached HEAD state explanation
#839
Author: Junio C Hamano
Date: Mon, 19 Oct 2026 21:24
48 lines
14740 bytes
"Julia Evans" <julia@jvns.ca> writes:

> Let me know if this is a helpful way for you to bring some user perspectives
> into the conversation. Like I said earlier I don't really think it's possible to
> discuss how to explain HEAD in a vacuum so maybe these quotes will help!

It surely is more helpful than "I think it is not better" that is
not backed by anything.

> Also if you could give me any ideas for how to reply in a "shorter" way than
> "I don't think this is clearer" let me know, I can't bring you quotes from user
> research to justify every single wording choice :)

IOW, "Trust me" ;-)

It is often good enough among those who have worked together for
some time, and you certainly do qualify.

> -  "HEAD is a mysterious thing I never need to know about unless something has
> g-one wrong, eg. merge conflicts."

Somebody may have said so, but how does bringing it (together with
many other examples) up in _this_ discussion help us find a better
way to explain it to users?

Somebody is clueless about HEAD and does not feel the need to
understand it until they really need to.  This fact alone, that they
can get going with such a sketchy understanding and make use of Git
sufficiently well may be a great thing by itself.  It may support an
argument that we do not really have to explain what HEAD is to our
readers.  Is that the argument you are making?

I won't comment on each and every one of them, but is your point
that many potential readers do not understand HEAD and doing fine?

Of course there are a handful of examples that they do get it, like
this one.

> - "HEAD is a reference to a commit, either directly or indirectly. A direct
> commit is just in detached state, where HEAD is looking at the commit itself.
> An indirect reference would be the case you’ve shown above, where HEAD is a
> reference to a reference."

But what did you want to illustrate again?  I am little bit fuzzy on
what you wanted to achieve by dumping these at me.

Thanks.
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#2620
Author: "D. Ben Knoble"
Date: Sat, 07 Nov 2026 20:06
76 lines
15264 bytes
Hi all,

I've read through this thread a few times, and I'm still interested in
seeing the detached HEAD manual land. It seems like the following need
to happen?

1. We are waiting on a reroll from Julia with the SQUASH??? commit or
something like it applied
(https://lore.kernel.org/git/xmqqse25cni7.fsf@gitster.g/)
2. The discussion about how to describe HEAD needs resolved.

On Fri, Oct 16, 2026 at 5:11 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Just to leave a record, so that we have better chance remembering
> the suggested improved text, here are what I think the first two
> paragraphs should be replaced with.
>
>     HEAD records the state from which your current changes started.
>     This can be a branch (the "current branch"), or it be a commit,
>     (in which case HEAD is "detached").
>
>     In the detached HEAD state, you can safely build, run, and
>     experiment with any historical version of your codebase, without
>     having to worry about clobbering your branches, and you do not
>     have to clutter your repository with temporary branches.

Here's what Junio suggested^, and here's what v4 has

+`HEAD` is where Git stores your current branch or commit. `HEAD` can either be:
+
+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, 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.

I do not personally think it's worth focusing on "the state from which
your current changes started" (even leaving aside the "what if I have
no changes?" moment), in part because I don't think that's how most
users conceive of HEAD. Other than that detail, I think the first
paragraphs of each option coincide.

There are some quotes which use the "it's the parent of the next
commit" mental model, though, so we could include a comment on that?
It might be distracting, and could make its way into some kind of
admonition/aside if need be?

The second paragraphs differ a bit, with Junio's containing some bits
about clobbering extant branches and not needing temporary branches. I
think we could work something in there, like

    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. By detaching
    `HEAD`, you don't need to worry about clobbering an existing
    branch or creating a temporary branch.

? I suppose we could also be more explicit ("build, run, and
experiment"), if we wanted, but this is a good example to start from.

> Having said all that, the changes between v3 and v4 look OK to me,
> so let me update the previous copy I have with this version.

Otherwise, we're approaching a finish here?

-- 
D. Ben Knoble
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#2636
Author: Junio C Hamano
Date: Sun, 08 Nov 2026 04:25
63 lines
15661 bytes
"D. Ben Knoble" <ben.knoble@gmail.com> writes:

> +`HEAD` is where Git stores your current branch or commit. `HEAD` can either be:
> +
> +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, 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.
> ...
> There are some quotes which use the "it's the parent of the next
> commit" mental model, though, so we could include a comment on that?

"From which your current changes started" is exactly that.  HEAD is
where you started from (and can come back to, if you get lost after
botching your working tree state).  Your next commit is built on top
of it, and then that new commit becomes your current commit.

This is why it is useful for users to realize that HEAD is simply
where you started from, regardless of where you are going.

> The second paragraphs differ a bit, with Junio's containing some bits
> about clobbering extant branches and not needing temporary branches. I
> think we could work something in there, like
> ...
>     this to figure out when a bug was introduced. By detaching
>     `HEAD`, you don't need to worry about clobbering an existing
>     branch or creating a temporary branch.
>
> ? I suppose we could also be more explicit ("build, run, and
> experiment"), if we wanted, but this is a good example to start from.

I assume you mean the last two lines?  I do appreciate that addition.

However, we should avoid saying "useful for HEAD to be a commit ID".
Being a commit ID is not the notable characteristic of a detached
HEAD; the fact that it directly refers to a commit is what gives us
these useful features.  Recording a hexadecimal commit object name
(instead of a refname of a branch) is a mere implementation detail
of that direct reference.

Since Julia's introductory paragraph already established the term
"detached HEAD" very well, there is no need to resort to a roundabout
phrasing like "useful for HEAD to be a commit ID".  I suggest
rewriting the beginning of that paragraph to start with:

    It can be useful for the HEAD to be directly refering to a
    commit (instead of indirectly referring to a commit by naming
    the current branch).

This introduces the remainder of her paragraph much better, and
makes your addition about not needing to worry about clobbering
existing branches or consuming temporary refs easier to understand.

> Otherwise, we're approaching a finish here?

I hope so.  The document overall is moving in the right direction,
and I just want to see these minor technical descriptions improved.
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#2783
Author: Junio C Hamano
Date: Tue, 10 Nov 2026 06:46
52 lines
14807 bytes
"Julia Evans" <julia@jvns.ca> writes:

> On Sat, Nov 7, 2026, at 11:25 PM, Junio C Hamano wrote:
>> "D. Ben Knoble" <ben.knoble@gmail.com> writes:
>>
>>> +`HEAD` is where Git stores your current branch or commit. `HEAD` can either be:
>>> +
>>> +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".
> ...
> Saying "it's a commit ID" is a simple way to say that it directly refers
> to a commit in a small number of words. I'm not sure what your goal is
> here, do you think that statement is somehow misleading to users?

Yes, I find it highly misleading, and otherwise I wouldn't be
discussing this patch.

I think the part I find it problematic the most in

    HEAD is a branch vs HEAD is a commit ID

is that HEAD *IS NOT* either of these things.  HEAD may record one
of these kinds of things but it never is a branch or an commit
object name.

And HEAD records one of these kinds of things to either directly or
indirectly refer to a commit that is currently checked out.

That is why I want to see the first description of the above to
serve as a solid base to convey these points, with later
descriptions we build on top:

 * `HEAD` can record either (1) a branch, or (2) a commit object
    name.

 * The purpose of HEAD doing so is to refer to a commit that is
   currently checked out, either directly or indirectly.

 * The latter (i.e., the direct reference) may be unfamiliar to
   users.  It is called "detached HEAD" state.

 * The commit referred to by the HEAD will become the parent of the
   commit you create.  When your HEAD records a branch, its tip is
   advanced.  Otherwise no branch is affected.  Either case, HEAD
   now refers to the commit you newly created.

And I find that saying "HEAD is a branch" or "HEAD is a commit" is
misleading and does not help that goal.

HTH.
[PATCH v5] doc: checkout: rewrite detached HEAD state explanation
#2945
Author: "Julia Evans via GitGitGadget"
Date: Wed, 11 Nov 2026 21:34
429 lines
27384 bytes
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 detached-head`").
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: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
Signed-off-by: Julia Evans <julia@jvns.ca>
---
    doc: checkout: rewrite detached HEAD state explanation
    
    This is a pretty quick rewrite where we didn't get any user feedback. If
    we really want to make sure it's clear to users as possible, we should
    IMO do a second stage later where we ask users which parts are unclear.
    
    Changes in v2:
    
     * Make the meson.build and Makefile changes (thanks to Kristoffer)
     * Rename to gitdata-model (thanks to Kristoffer)
     * Mention commits will be deleted after 90 days to make it seem less
       scary (thanks to Kristoffer)
     * Add an "easily" qualifier to "it lets you look at an old version of
       your code", to avoid implying that this is the only way (thanks to
       Junio)
     * Mention that Git will warn you when switching branches if you're
       leaving commits behind (thanks to Junio)
     * Remove "unintentionally" (thanks to Junio)
     * Mention git help datamodel (thanks to Junio)
    
    Changes in v3:
    
    I went through the style comments from v1.
    
    content changes:
    
     * Change "90 days" to "30 days" (thanks to Junio)
     * Say that HEAD is where Git stores your "current branch or commit" to
       make the first sentence read better (thanks to Junio)
     * Mention git switch --detach a little earlier (thanks to Kristoffer)
     * Replace "the only problem" with "it's important to know that" (thanks
       to Junio)
    
    formatting changes etc:
    
     * Replace help detachedhead with help detached-head (thanks to Junio)
     * Fix spacing in command-list.txt (thanks to Junio)
     * Get Marie's signoff (thanks to Junio)
    
    non-changes: Left these phrases alone because I don't think the proposed
    changes would make it more clear.
    
     * "3 main ways you can end up in detached HEAD state"
     * "to look at an old version of your code"
     * "avoid accidentally ending up"
    
    Changes in v4:
    
     * Fix commit message in example: "fix bug", not "3" (thanks to Junio)
     * "commit for that tag" -> "tagged commit" (thanks to Junio)
     * Move "git checkout COMMIT_ID or git switch --detach COMMIT_ID" down
       to the "3 main ways" list to reduce duplication (thanks to Junio)
     * Add some examples and be clearer about how detached HEAD state lets
       you check out an old version of the project. (thanks to Junio)
    
    non-changes:
    
     * It's probably possible to explain HEAD better. That seems like a big
       project to me since HEAD in Git is a very tricky topic. I think it
       would be better to address in a separate patch series to have more
       time to think it through. Or if we think it's not possible to merge
       this without explaining HEAD better then I guess it shouldn't be
       merged.
    
    Changes in v5:
    
     * Explain HEAD's relationship to the current commit to make it feel
       more uniform (thanks to Junio)
     * Add a note about the benefits of detaching HEAD (thanks to Junio &
       Ben)
     * Change "HEAD can either be a commit ID or a branch name"

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2250%2Fjvns%2Fdetached-head-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2250/jvns/detached-head-v5
Pull-Request: https://github.com/gitgitgadget/git/pull/2250

Range-diff vs v4:

 1:  88210ad52d ! 1:  cd4e443809 doc: checkout: rewrite detached HEAD state explanation
     @@ Documentation/Makefile: MAN7_TXT += gitcore-tutorial.adoc
      
       ## Documentation/detached-head.adoc (new) ##
      @@
     -+`HEAD` is where Git stores your current branch or commit. `HEAD` can either be:
     ++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 either be stored as:
      +
     -+1. A branch, which is your current branch.
     ++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".
      +
     @@ Documentation/detached-head.adoc (new)
      +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.
     ++was introduced. 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


 Documentation/Makefile              |   1 +
 Documentation/detached-head.adoc    |  64 ++++++++++++++
 Documentation/git-checkout.adoc     | 129 +---------------------------
 Documentation/gitdetached-head.adoc |  14 +++
 Documentation/meson.build           |   1 +
 advice.c                            |   1 +
 command-list.txt                    |   1 +
 7 files changed, 83 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..48dbc1da1c
--- /dev/null
+++ b/Documentation/detached-head.adoc
@@ -0,0 +1,64 @@
+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 either be stored as:
+
+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. 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
+
+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.
+
+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
+-------------------------
+
+For a more in-depth explanation of how `HEAD` works in Git,
+see linkgit:gitdatamodel[7] (`git help datamodel`).
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 linkgit:gitglossary[7].
 [[DETACHED_HEAD]]
 DETACHED HEAD
 -------------
-`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
-branch refers to a specific commit. Let's look at a repo with three
-commits, one of them tagged, and with branch `master` checked out:
 
-------------
-           HEAD (refers to branch 'master')
-            |
-            v
-a---b---c  branch 'master' (refers to commit 'c')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-When a commit is created in this state, the branch is updated to refer to
-the new commit. Specifically, `git commit` creates a new commit `d`, whose
-parent is commit `c`, and then updates branch `master` to refer to new
-commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers
-to commit `d`:
-
-------------
-$ edit; git add; git commit
-
-               HEAD (refers to branch 'master')
-                |
-                v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is sometimes useful to be able to checkout a commit that is not at
-the tip of any named branch, or even to create a new commit that is not
-referenced by a named branch. Let's look at what happens when we
-checkout commit `b` (here we show two ways this may be done):
-
-------------
-$ git checkout v2.0  # or
-$ git checkout master^^
-
-   HEAD (refers to commit 'b')
-    |
-    v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-Notice that regardless of which checkout command we use, `HEAD` now refers
-directly to commit `b`. This is known as being in detached `HEAD` state.
-It means simply that `HEAD` refers to a specific commit, as opposed to
-referring to a named branch. Let's see what happens when we create a commit:
-
-------------
-$ edit; git add; git commit
-
-     HEAD (refers to commit 'e')
-      |
-      v
-      e
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-There is now a new commit `e`, but it is referenced only by `HEAD`. We can
-of course add yet another commit in this state:
-
-------------
-$ edit; git add; git commit
-
-	 HEAD (refers to commit 'f')
-	  |
-	  v
-      e---f
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-In fact, we can perform all the normal Git operations. But, let's look
-at what happens when we then checkout `master`:
-
-------------
-$ git checkout master
-
-               HEAD (refers to branch 'master')
-      e---f     |
-     /          v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is important to realize that at this point nothing refers to commit
-`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
-by the routine Git garbage collection process, unless we create a reference
-before that happens. If we have not yet moved away from commit `f`,
-any of these will create a reference to it:
-
-------------
-$ git checkout -b foo  # or "git switch -c foo"  <1>
-$ git branch foo                                 <2>
-$ git tag foo                                    <3>
-------------
-<1> creates a new branch `foo`, which refers to commit `f`, and then
-    updates `HEAD` to refer to branch `foo`. In other words, we'll no longer
-    be in detached `HEAD` state after this command.
-<2> similarly creates a new branch `foo`, which refers to commit `f`,
-    but leaves `HEAD` detached.
-<3> creates a new tag `foo`, which refers to commit `f`,
-    leaving `HEAD` detached.
-
-If we have moved away from commit `f`, then we must first recover its object
-name (typically by using git reflog), and then we can create a reference to
-it. For example, to see the last two commits to which `HEAD` referred, we
-can use either of these commands:
-
-------------
-$ git reflog -2 HEAD # or
-$ git log -g -2 HEAD
-------------
+include::detached-head.adoc[]
 
 [[ARGUMENT_DISAMBIGUATION]]
 ARGUMENT DISAMBIGUATION
diff --git a/Documentation/gitdetached-head.adoc b/Documentation/gitdetached-head.adoc
new file mode 100644
index 0000000000..a477060db2
--- /dev/null
+++ b/Documentation/gitdetached-head.adoc
@@ -0,0 +1,14 @@
+gitdetached-head(7)
+===================
+
+NAME
+----
+gitdetached-head - How detached HEAD state works
+
+DESCRIPTION
+-----------
+include::detached-head.adoc[]
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Documentation/meson.build b/Documentation/meson.build
index cfa9c67609..6b3141647f 100644
--- a/Documentation/meson.build
+++ b/Documentation/meson.build
@@ -197,6 +197,7 @@ manpages = {
   'gitcredentials.adoc' : 7,
   'gitcvs-migration.adoc' : 7,
   'gitdatamodel.adoc' : 7,
+  'gitdetached-head.adoc' : 7,
   'gitdiffcore.adoc' : 7,
   'giteveryday.adoc' : 7,
   'gitfaq.adoc' : 7,
diff --git a/advice.c b/advice.c
index 401d047391..8d2c1ff1ee 100644
--- a/advice.c
+++ b/advice.c
@@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
 	"\n"
 	"  git switch -\n"
 	"\n"
+	"Run `git help detached-head` to learn more.\n"
 	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
 
 	fprintf(stderr, fmt, new_name);
diff --git a/command-list.txt b/command-list.txt
index 63ae2a67c9..7fb57765fb 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -218,6 +218,7 @@ gitcore-tutorial                        guide
 gitcredentials                          guide
 gitcvs-migration                        guide
 gitdatamodel                            guide
+gitdetached-head                        guide
 gitdiffcore                             guide
 giteveryday                             guide
 gitfaq                                  guide

base-commit: 5a7d1e8045ce66c908f62598e26cbb8df7b39a90
-- 
gitgitgadget
Re: [PATCH v5] doc: checkout: rewrite detached HEAD state explanation
#2967
Author: Junio C Hamano
Date: Wed, 11 Nov 2026 23:14
39 lines
13971 bytes
"Julia Evans via GitGitGadget" <gitgitgadget@gmail.com> writes:

> +current branch. Internally in Git, `HEAD` can either be stored as:

I am not sure if I am the only one who finds this phrasing awkward.

It is not like HEAD is stored (in somewhere) as something.  Rather,
HEAD *is* the container.  Its implementation may be a file on the
filesystem in reffiles backend, or a record in the reftable
database.  In either case, HEAD stores the refname of the current
branch, or the object name of the current commit the HEAD is
detached at.

So, from that point of view, I would understand it better if the
above said

    Internally in Git, `HEAD` can store either ... (branch name or
    commit object name) ...

or something.

> diff --git a/advice.c b/advice.c
> index 401d047391..8d2c1ff1ee 100644
> --- a/advice.c
> +++ b/advice.c
> @@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
>  	"\n"
>  	"  git switch -\n"
>  	"\n"
> +	"Run `git help detached-head` to learn more.\n"

Shouldn't we be using a normal single quote instead of backtick?

Existing references to command in this file has the command without
quotes, on its own line "\tgit merge --no-ff\n", or uses a normal
single quote as in "... use 'git add/rm <file>'".

People's terminals do not grok markdown anyway ;-)
Re: [PATCH v5] doc: checkout: rewrite detached HEAD state explanation
#2970
Author: Junio C Hamano
Date: Thu, 12 Nov 2026 04:08
26 lines
13449 bytes
Junio C Hamano <gitster@pobox.com> writes:

>
>> diff --git a/advice.c b/advice.c
>> index 401d047391..8d2c1ff1ee 100644
>> --- a/advice.c
>> +++ b/advice.c
>> @@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
>>  	"\n"
>>  	"  git switch -\n"
>>  	"\n"
>> +	"Run `git help detached-head` to learn more.\n"
>
> Shouldn't we be using a normal single quote instead of backtick?
>
> Existing references to command in this file has the command without
> quotes, on its own line "\tgit merge --no-ff\n", or uses a normal
> single quote as in "... use 'git add/rm <file>'".
>
> People's terminals do not grok markdown anyway ;-)

One more thing.

This change needs to update t/t2020-checkout-detach.sh because the
new output is not expected there (yet).
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#2993
Author: Junio C Hamano
Date: Thu, 12 Nov 2026 15:40
50 lines
15215 bytes
Julia Evans <julia@jvns.ca> writes:

> Yeah I agree the "it's the parent of the next commit" would be 
> distracting unless there's a specific reason we need to include it.
>
> I agree "the state from which your current changes started" isn't right 
> but I think if we reinterpret it as "the current commit you have checked 
> out" (which I think has the same or a very similar meaning) then it'll 
> seem familiar to folks and might even help.
> ...
> because I couldn't think of a reason why someone would clobber an 
> existing branch to check out an old version of their code.

When I find something interesting in a topic, which may be an old
commit from the point of view of 'seen', I would detach HEAD at the
tip of the branch to take a look at its contents, because what I am
going to do next would very often involve modifying the working tree
files to experiment.  Even a topic that is documentation only, like
the je/doc-detached-head topic, can have a cascading effect that
ends up breaking tests, for example, and I'd be futzing with the
sources and tests to figure out what the best fix would be.  Even
though I am not going to make any permanent commits out of these
efforts, being able to make temporary commits to mark a resumable
state is handy, and when my spelunking gets longer, I do make such
commits.

But of course, I do *not* want to contaminate the topic branch I
have been looking at.  The work I do, whether it is for validation
or for satisfying my own curiosity, does not necessarily align with
the objective the topic author had in mind when they wrote it.

This is where detached HEAD is the perfect mechanism to take
advantage of.  You do not want to clobber the branch you are going
to start your journey at, yet you want to be able to make tentative
changes to experiment, and you want to be able to take snapshots.

And during the journey, your HEAD points at a commit from which your
current changes started and points at a commit your next commit will
record as its parent.

I guess it requires a developer's mindset and imagination to
appreciate why detached HEAD is useful while spelunking.  We often
phrase the act of visiting random commits (often older ones) as
"sight-seeing", but in reality, even when you are mostly looking,
you do want to futz with things you see there.

It is in a developer's heart ;-).

Hope this clarifies.
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#3026
Author: Junio C Hamano
Date: Thu, 12 Nov 2026 19:25
32 lines
14405 bytes
Julia Evans <julia@jvns.ca> writes:

>> I guess it requires a developer's mindset and imagination to
>> appreciate why detached HEAD is useful while spelunking.  We often
>> phrase the act of visiting random commits (often older ones) as
>> "sight-seeing", but in reality, even when you are mostly looking,
>> you do want to futz with things you see there.
>
> I'm not sure what you mean by "it requires a developer's mindset", in my 
> time working as a developer using Git I never ran into a case where I 
> would have needed to clobber a branch in this way :)

Sightseeing is not just going to look and see, but touching and
breaking things.  That kind of curiosity was what I meant by a
developer's mindset.

And of course, I never ran into a case where I needed to clobber a
branch, either.  That is why I do not work on other people's topic
branches to begin with, and make use of detached HEAD instead.

In any case, now we have a shared better understanding of where
"where your current work started" and "will be the parent of your
next commit" came from.  Hopefully the next iteration will become
clearer on these points?  I think the central difference is that you
seem to consider detached HEAD state is only about pure sightseeing
while it was designed to allow touching and breaking.  If the user
is not making any changes, of course it does not make sense to talk
about "your current changes" or "next commit".  But detached HEAD is
not limited to such a world view.

Thanks.
[PATCH v6] doc: checkout: rewrite detached HEAD state explanation
#3050
Author: "Julia Evans via GitGitGadget"
Date: Thu, 12 Nov 2026 22:47
471 lines
28964 bytes
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 detached-head`").
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: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
Signed-off-by: Julia Evans <julia@jvns.ca>
---
    doc: checkout: rewrite detached HEAD state explanation
    
    This is a pretty quick rewrite where we didn't get any user feedback. If
    we really want to make sure it's clear to users as possible, we should
    IMO do a second stage later where we ask users which parts are unclear.
    
    Changes in v2:
    
     * Make the meson.build and Makefile changes (thanks to Kristoffer)
     * Rename to gitdata-model (thanks to Kristoffer)
     * Mention commits will be deleted after 90 days to make it seem less
       scary (thanks to Kristoffer)
     * Add an "easily" qualifier to "it lets you look at an old version of
       your code", to avoid implying that this is the only way (thanks to
       Junio)
     * Mention that Git will warn you when switching branches if you're
       leaving commits behind (thanks to Junio)
     * Remove "unintentionally" (thanks to Junio)
     * Mention git help datamodel (thanks to Junio)
    
    Changes in v3:
    
    I went through the style comments from v1.
    
    content changes:
    
     * Change "90 days" to "30 days" (thanks to Junio)
     * Say that HEAD is where Git stores your "current branch or commit" to
       make the first sentence read better (thanks to Junio)
     * Mention git switch --detach a little earlier (thanks to Kristoffer)
     * Replace "the only problem" with "it's important to know that" (thanks
       to Junio)
    
    formatting changes etc:
    
     * Replace help detachedhead with help detached-head (thanks to Junio)
     * Fix spacing in command-list.txt (thanks to Junio)
     * Get Marie's signoff (thanks to Junio)
    
    non-changes: Left these phrases alone because I don't think the proposed
    changes would make it more clear.
    
     * "3 main ways you can end up in detached HEAD state"
     * "to look at an old version of your code"
     * "avoid accidentally ending up"
    
    Changes in v4:
    
     * Fix commit message in example: "fix bug", not "3" (thanks to Junio)
     * "commit for that tag" -> "tagged commit" (thanks to Junio)
     * Move "git checkout COMMIT_ID or git switch --detach COMMIT_ID" down
       to the "3 main ways" list to reduce duplication (thanks to Junio)
     * Add some examples and be clearer about how detached HEAD state lets
       you check out an old version of the project. (thanks to Junio)
    
    non-changes:
    
     * It's probably possible to explain HEAD better. That seems like a big
       project to me since HEAD in Git is a very tricky topic. I think it
       would be better to address in a separate patch series to have more
       time to think it through. Or if we think it's not possible to merge
       this without explaining HEAD better then I guess it shouldn't be
       merged.
    
    Changes in v5:
    
     * Explain HEAD's relationship to the current commit to make it feel
       more uniform (thanks to Junio)
     * Add a note about the benefits of detaching HEAD (thanks to Junio &
       Ben)
     * Change "HEAD can either be a commit ID or a branch name"
    
    Changes in v6:
    
     * Adjust some wording around "stores" (thanks to Junio)
     * Change the quote style in advice.c (thanks to Junio)
     * Update expected output in t2020-checkout-detach.sh (thanks to Junio)

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2250%2Fjvns%2Fdetached-head-v6
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2250/jvns/detached-head-v6
Pull-Request: https://github.com/gitgitgadget/git/pull/2250

Range-diff vs v5:

 1:  cd4e443809 ! 1:  64f50b8248 doc: checkout: rewrite detached HEAD state explanation
     @@ Documentation/detached-head.adoc (new)
      @@
      +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 either be stored as:
     ++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.
     @@ advice.c: void detach_advice(const char *new_name)
       	"\n"
       	"  git switch -\n"
       	"\n"
     -+	"Run `git help detached-head` to learn more.\n"
     ++	"Run 'git help detached-head' to learn more.\n"
       	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
       
       	fprintf(stderr, fmt, new_name);
     @@ command-list.txt: gitcore-tutorial                        guide
       gitdiffcore                             guide
       giteveryday                             guide
       gitfaq                                  guide
     +
     + ## t/t2020-checkout-detach.sh ##
     +@@ t/t2020-checkout-detach.sh: test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not as
     + 
     + 	  git switch -
     + 
     ++	Run 'git help detached-head' to learn more.
     + 	Turn off this advice by setting config variable advice.detachedHead to false
     + 
     + 	HEAD is now at \$commit three
     +@@ t/t2020-checkout-detach.sh: test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked
     + 
     + 	  git switch -
     + 
     ++	Run 'git help detached-head' to learn more.
     + 	Turn off this advice by setting config variable advice.detachedHead to false
     + 
     + 	HEAD is now at \$commit... three


 Documentation/Makefile              |   1 +
 Documentation/detached-head.adoc    |  64 ++++++++++++++
 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, 85 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..78ee6631a4
--- /dev/null
+++ b/Documentation/detached-head.adoc
@@ -0,0 +1,64 @@
+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. 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
+
+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.
+
+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
+-------------------------
+
+For a more in-depth explanation of how `HEAD` works in Git,
+see linkgit:gitdatamodel[7] (`git help datamodel`).
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 linkgit:gitglossary[7].
 [[DETACHED_HEAD]]
 DETACHED HEAD
 -------------
-`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
-branch refers to a specific commit. Let's look at a repo with three
-commits, one of them tagged, and with branch `master` checked out:
 
-------------
-           HEAD (refers to branch 'master')
-            |
-            v
-a---b---c  branch 'master' (refers to commit 'c')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-When a commit is created in this state, the branch is updated to refer to
-the new commit. Specifically, `git commit` creates a new commit `d`, whose
-parent is commit `c`, and then updates branch `master` to refer to new
-commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers
-to commit `d`:
-
-------------
-$ edit; git add; git commit
-
-               HEAD (refers to branch 'master')
-                |
-                v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is sometimes useful to be able to checkout a commit that is not at
-the tip of any named branch, or even to create a new commit that is not
-referenced by a named branch. Let's look at what happens when we
-checkout commit `b` (here we show two ways this may be done):
-
-------------
-$ git checkout v2.0  # or
-$ git checkout master^^
-
-   HEAD (refers to commit 'b')
-    |
-    v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-Notice that regardless of which checkout command we use, `HEAD` now refers
-directly to commit `b`. This is known as being in detached `HEAD` state.
-It means simply that `HEAD` refers to a specific commit, as opposed to
-referring to a named branch. Let's see what happens when we create a commit:
-
-------------
-$ edit; git add; git commit
-
-     HEAD (refers to commit 'e')
-      |
-      v
-      e
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-There is now a new commit `e`, but it is referenced only by `HEAD`. We can
-of course add yet another commit in this state:
-
-------------
-$ edit; git add; git commit
-
-	 HEAD (refers to commit 'f')
-	  |
-	  v
-      e---f
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-In fact, we can perform all the normal Git operations. But, let's look
-at what happens when we then checkout `master`:
-
-------------
-$ git checkout master
-
-               HEAD (refers to branch 'master')
-      e---f     |
-     /          v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is important to realize that at this point nothing refers to commit
-`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
-by the routine Git garbage collection process, unless we create a reference
-before that happens. If we have not yet moved away from commit `f`,
-any of these will create a reference to it:
-
-------------
-$ git checkout -b foo  # or "git switch -c foo"  <1>
-$ git branch foo                                 <2>
-$ git tag foo                                    <3>
-------------
-<1> creates a new branch `foo`, which refers to commit `f`, and then
-    updates `HEAD` to refer to branch `foo`. In other words, we'll no longer
-    be in detached `HEAD` state after this command.
-<2> similarly creates a new branch `foo`, which refers to commit `f`,
-    but leaves `HEAD` detached.
-<3> creates a new tag `foo`, which refers to commit `f`,
-    leaving `HEAD` detached.
-
-If we have moved away from commit `f`, then we must first recover its object
-name (typically by using git reflog), and then we can create a reference to
-it. For example, to see the last two commits to which `HEAD` referred, we
-can use either of these commands:
-
-------------
-$ git reflog -2 HEAD # or
-$ git log -g -2 HEAD
-------------
+include::detached-head.adoc[]
 
 [[ARGUMENT_DISAMBIGUATION]]
 ARGUMENT DISAMBIGUATION
diff --git a/Documentation/gitdetached-head.adoc b/Documentation/gitdetached-head.adoc
new file mode 100644
index 0000000000..a477060db2
--- /dev/null
+++ b/Documentation/gitdetached-head.adoc
@@ -0,0 +1,14 @@
+gitdetached-head(7)
+===================
+
+NAME
+----
+gitdetached-head - How detached HEAD state works
+
+DESCRIPTION
+-----------
+include::detached-head.adoc[]
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Documentation/meson.build b/Documentation/meson.build
index cfa9c67609..6b3141647f 100644
--- a/Documentation/meson.build
+++ b/Documentation/meson.build
@@ -197,6 +197,7 @@ manpages = {
   'gitcredentials.adoc' : 7,
   'gitcvs-migration.adoc' : 7,
   'gitdatamodel.adoc' : 7,
+  'gitdetached-head.adoc' : 7,
   'gitdiffcore.adoc' : 7,
   'giteveryday.adoc' : 7,
   'gitfaq.adoc' : 7,
diff --git a/advice.c b/advice.c
index 401d047391..3238e9b866 100644
--- a/advice.c
+++ b/advice.c
@@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
 	"\n"
 	"  git switch -\n"
 	"\n"
+	"Run 'git help detached-head' to learn more.\n"
 	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
 
 	fprintf(stderr, fmt, new_name);
diff --git a/command-list.txt b/command-list.txt
index 63ae2a67c9..7fb57765fb 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -218,6 +218,7 @@ gitcore-tutorial                        guide
 gitcredentials                          guide
 gitcvs-migration                        guide
 gitdatamodel                            guide
+gitdetached-head                        guide
 gitdiffcore                             guide
 giteveryday                             guide
 gitfaq                                  guide
diff --git a/t/t2020-checkout-detach.sh b/t/t2020-checkout-detach.sh
index 28bbbe6c05..b0bfd3191c 100755
--- a/t/t2020-checkout-detach.sh
+++ b/t/t2020-checkout-detach.sh
@@ -228,6 +228,7 @@ test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not as
 
 	  git switch -
 
+	Run 'git help detached-head' to learn more.
 	Turn off this advice by setting config variable advice.detachedHead to false
 
 	HEAD is now at \$commit three
@@ -310,6 +311,7 @@ test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked
 
 	  git switch -
 
+	Run 'git help detached-head' to learn more.
 	Turn off this advice by setting config variable advice.detachedHead to false
 
 	HEAD is now at \$commit... three

base-commit: 5a7d1e8045ce66c908f62598e26cbb8df7b39a90
-- 
gitgitgadget
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#3052
Author: Junio C Hamano
Date: Fri, 13 Nov 2026 02:25
28 lines
14473 bytes
Julia Evans <julia@jvns.ca> writes:

>> I think the central difference is that you
>> seem to consider detached HEAD state is only about pure sightseeing
>> while it was designed to allow touching and breaking.  If the user
>> is not making any changes, of course it does not make sense to talk
>> about "your current changes" or "next commit".  But detached HEAD is
>> not limited to such a world view.
>
> We say very clearly that you can make commits while in detached HEAD 
> state so I don't think there's any danger of readers not realizing 
> that's possible.
>
>    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.

Yeah, but that isn't the purpose of mentioning "next commit" etc. to
make readers realize that.  So I am not sure what you want to say
with that.

The mention of "your current work" (started at the HEAD) and "next
commit" is to lay groundwork to explain what happens when they make
that commit, and how it is different when they are on detached HEAD,
in contrast to when they are on a branch.  After all, what we were
discusssing is an improved explanation on detached HEAD, and your
commits not affecting any of your branch *is* the most important
aspect of what detached HEAD gives you.
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#3089
Author: Junio C Hamano
Date: Fri, 13 Nov 2026 17:23
31 lines
14543 bytes
Julia Evans <julia@jvns.ca> writes:

>> The mention of "your current work" (started at the HEAD) and "next
>> commit" is to lay groundwork to explain what happens when they make
>> that commit, and how it is different when they are on detached HEAD,
>> in contrast to when they are on a branch.  After all, what we were
>> discusssing is an improved explanation on detached HEAD, and your
>> commits not affecting any of your branch *is* the most important
>> aspect of what detached HEAD gives you
>
> I read this a few times and it's hard for me to tell what the issue 
> being raised is.
>
> Are you saying that we're not explaining all the details from "`HEAD` 
> can store either a branch or commit ID" to "if you create new commits 
> while in detached HEAD state, those commits won't be on a branch"?

No.  I do not expect an introductory document to tell *all* the
things that needs to be known about a topic.  That is an
encyclopedia, not a technical manual.

The fact that you would not clobber any branches when you make a
temporary commit on a detached HEAD, however, is the most important
consequence of HEAD *not* recording what branch we are on that has
real *impact* to the end-users.  They can leave their temporary work
behind without explicit removal by just checking out a branch, which
also means they have to be careful about losing *if* they care about
what they did during this temporary state.

I view it as a disservice to deliberately avoid talking about it.
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#3097
Author: Junio C Hamano
Date: Fri, 13 Nov 2026 21:56
24 lines
14251 bytes
Julia Evans <julia@jvns.ca> writes:

> Of course these are both important audiences, but it's always hard to 
> write a document for two very different audience at the same time. For 
> example, one thing that came up in this thread is that creating commits 
> while not on a branch is a problem for the first person and a good thing 
> for the second person.

This is a manual page, which means it has to cater to both audiences.
Unlike a tutorial or guide, we cannot say we target this audience
primarily and expect the other audience to figure things out
themselves.

To cover the "oops, I got myself into a problem?  I do not even know
if I am in trouble, or if Git is behaving as expected, and I am
totally lost.  I do not even know which commands I typed in the last
5 minutes got me into this state" situation, manual pages that are
organized along commands to help quick look-up are a bad match.

Perhaps a FAQ or "survival guide" that targets these inexperienced
users would help?  Unlike manual pages, guides have more freedom in
choosing the material covered and the order in which they are
presented.  I dunno.
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#3111
Author: Junio C Hamano
Date: Sat, 14 Nov 2026 01:54
22 lines
14405 bytes
Julia Evans <julia@jvns.ca> writes:

> Okay. How about this?  (the added sentence is between the **)
>
>    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.

Yes, taking the "data scientist" example as a "pure time machine
without touching, just observing" scenario, and the "programmer"
example as an "inspect and experiment" scenario, the addition of
this single sentence is sufficient to hint at the wide range of
usefulness to which this feature applies.  Nicely done.

I think this is short and thin enough to be good material to be
shared between the manual for everybody (which would want to cover
wider workflows with possibly thin coverage on some and thorough
coverage on others) and a survival guide for those who are lost
(which can be more targeted towards specific audiences).
Re: [PATCH v4] doc: checkout: rewrite detached HEAD state explanation
#3112
Author: Junio C Hamano
Date: Sat, 14 Nov 2026 02:54
21 lines
14000 bytes
Junio C Hamano <gitster@pobox.com> writes:

> This is a manual page, which means it has to cater to both audiences.
> Unlike a tutorial or guide, we cannot say we target this audience
> primarily and expect the other audience to figure things out
> themselves.

The detached-head.adoc is still a part of a manual page for the
checkout command, but gitdetached-head.adoc can have its own
material.  It does not need to stay "just include detached-head.adoc
and be done with it" documentation at all.

> Perhaps a FAQ or "survival guide" that targets these inexperienced
> users would help?  Unlike manual pages, guides have more freedom in
> choosing the material covered and the order in which they are
> presented.  I dunno.

So the rest of the gitdetached-head.adoc, other than the "include
detached-head.adoc" line, can be the "survival guide" document, if
we wanted it to.
[PATCH v7] doc: add new detached HEAD state guide
#3179
Author: "Julia Evans via GitGitGadget"
Date: Mon, 16 Nov 2026 15:33
496 lines
30624 bytes
From: Julia Evans <julia@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 <hello@marieflanagan.com>
Signed-off-by: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
Signed-off-by: Julia Evans <julia@jvns.ca>
---
    doc: checkout: rewrite detached HEAD state explanation
    
    This is a pretty quick rewrite where we didn't get any user feedback. If
    we really want to make sure it's clear to users as possible, we should
    IMO do a second stage later where we ask users which parts are unclear.
    
    Changes in v2:
    
     * Make the meson.build and Makefile changes (thanks to Kristoffer)
     * Rename to gitdata-model (thanks to Kristoffer)
     * Mention commits will be deleted after 90 days to make it seem less
       scary (thanks to Kristoffer)
     * Add an "easily" qualifier to "it lets you look at an old version of
       your code", to avoid implying that this is the only way (thanks to
       Junio)
     * Mention that Git will warn you when switching branches if you're
       leaving commits behind (thanks to Junio)
     * Remove "unintentionally" (thanks to Junio)
     * Mention git help datamodel (thanks to Junio)
    
    Changes in v3:
    
    I went through the style comments from v1.
    
    content changes:
    
     * Change "90 days" to "30 days" (thanks to Junio)
     * Say that HEAD is where Git stores your "current branch or commit" to
       make the first sentence read better (thanks to Junio)
     * Mention git switch --detach a little earlier (thanks to Kristoffer)
     * Replace "the only problem" with "it's important to know that" (thanks
       to Junio)
    
    formatting changes etc:
    
     * Replace help detachedhead with help detached-head (thanks to Junio)
     * Fix spacing in command-list.txt (thanks to Junio)
     * Get Marie's signoff (thanks to Junio)
    
    non-changes: Left these phrases alone because I don't think the proposed
    changes would make it more clear.
    
     * "3 main ways you can end up in detached HEAD state"
     * "to look at an old version of your code"
     * "avoid accidentally ending up"
    
    Changes in v4:
    
     * Fix commit message in example: "fix bug", not "3" (thanks to Junio)
     * "commit for that tag" -> "tagged commit" (thanks to Junio)
     * Move "git checkout COMMIT_ID or git switch --detach COMMIT_ID" down
       to the "3 main ways" list to reduce duplication (thanks to Junio)
     * Add some examples and be clearer about how detached HEAD state lets
       you check out an old version of the project. (thanks to Junio)
    
    Changes in v5:
    
     * Explain HEAD's relationship to the current commit to make it feel
       more uniform (thanks to Junio)
     * Add a note about the benefits of detaching HEAD (thanks to Junio &
       Ben)
     * Change "HEAD can either be a commit ID or a branch name"
    
    Changes in v6:
    
     * Adjust some wording around "stores" (thanks to Junio)
     * Change the quote style in advice.c (thanks to Junio)
     * Update expected output in t2020-checkout-detach.sh (thanks to Junio)
    
    Changes in v7:
    
     * Add "You can also use this to make experimental commits without being
       on a branch."
     * Update the commit message to explain that the goal is to help users
       who get into detached HEAD state accidentally, and how the guide is
       written to serve two purposes at the same time.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2250%2Fjvns%2Fdetached-head-v7
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2250/jvns/detached-head-v7
Pull-Request: https://github.com/gitgitgadget/git/pull/2250

Range-diff vs v6:

 1:  64f50b8248 ! 1:  020aac7cc4 doc: checkout: rewrite detached HEAD state explanation
     @@ Metadata
      Author: Julia Evans <julia@jvns.ca>
      
       ## Commit message ##
     -    doc: checkout: rewrite detached HEAD state explanation
     +    doc: add new detached HEAD state guide
      
     -    The current explanation has the following issues:
     +    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
     @@ Commit message
            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 detached-head`").
     -    The result is a shorter guide that covers more material.
     +    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
     @@ Documentation/detached-head.adoc (new)
      +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. By detaching `HEAD`, you don't need to create a
     -+temporary branch.
     ++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


 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
+
+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.
+
+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
+-------------------------
+
+For a more in-depth explanation of how `HEAD` works in Git,
+see linkgit:gitdatamodel[7] (`git help datamodel`).
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 linkgit:gitglossary[7].
 [[DETACHED_HEAD]]
 DETACHED HEAD
 -------------
-`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
-branch refers to a specific commit. Let's look at a repo with three
-commits, one of them tagged, and with branch `master` checked out:
 
-------------
-           HEAD (refers to branch 'master')
-            |
-            v
-a---b---c  branch 'master' (refers to commit 'c')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-When a commit is created in this state, the branch is updated to refer to
-the new commit. Specifically, `git commit` creates a new commit `d`, whose
-parent is commit `c`, and then updates branch `master` to refer to new
-commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers
-to commit `d`:
-
-------------
-$ edit; git add; git commit
-
-               HEAD (refers to branch 'master')
-                |
-                v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is sometimes useful to be able to checkout a commit that is not at
-the tip of any named branch, or even to create a new commit that is not
-referenced by a named branch. Let's look at what happens when we
-checkout commit `b` (here we show two ways this may be done):
-
-------------
-$ git checkout v2.0  # or
-$ git checkout master^^
-
-   HEAD (refers to commit 'b')
-    |
-    v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-Notice that regardless of which checkout command we use, `HEAD` now refers
-directly to commit `b`. This is known as being in detached `HEAD` state.
-It means simply that `HEAD` refers to a specific commit, as opposed to
-referring to a named branch. Let's see what happens when we create a commit:
-
-------------
-$ edit; git add; git commit
-
-     HEAD (refers to commit 'e')
-      |
-      v
-      e
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-There is now a new commit `e`, but it is referenced only by `HEAD`. We can
-of course add yet another commit in this state:
-
-------------
-$ edit; git add; git commit
-
-	 HEAD (refers to commit 'f')
-	  |
-	  v
-      e---f
-     /
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-In fact, we can perform all the normal Git operations. But, let's look
-at what happens when we then checkout `master`:
-
-------------
-$ git checkout master
-
-               HEAD (refers to branch 'master')
-      e---f     |
-     /          v
-a---b---c---d  branch 'master' (refers to commit 'd')
-    ^
-    |
-  tag 'v2.0' (refers to commit 'b')
-------------
-
-It is important to realize that at this point nothing refers to commit
-`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
-by the routine Git garbage collection process, unless we create a reference
-before that happens. If we have not yet moved away from commit `f`,
-any of these will create a reference to it:
-
-------------
-$ git checkout -b foo  # or "git switch -c foo"  <1>
-$ git branch foo                                 <2>
-$ git tag foo                                    <3>
-------------
-<1> creates a new branch `foo`, which refers to commit `f`, and then
-    updates `HEAD` to refer to branch `foo`. In other words, we'll no longer
-    be in detached `HEAD` state after this command.
-<2> similarly creates a new branch `foo`, which refers to commit `f`,
-    but leaves `HEAD` detached.
-<3> creates a new tag `foo`, which refers to commit `f`,
-    leaving `HEAD` detached.
-
-If we have moved away from commit `f`, then we must first recover its object
-name (typically by using git reflog), and then we can create a reference to
-it. For example, to see the last two commits to which `HEAD` referred, we
-can use either of these commands:
-
-------------
-$ git reflog -2 HEAD # or
-$ git log -g -2 HEAD
-------------
+include::detached-head.adoc[]
 
 [[ARGUMENT_DISAMBIGUATION]]
 ARGUMENT DISAMBIGUATION
diff --git a/Documentation/gitdetached-head.adoc b/Documentation/gitdetached-head.adoc
new file mode 100644
index 0000000000..a477060db2
--- /dev/null
+++ b/Documentation/gitdetached-head.adoc
@@ -0,0 +1,14 @@
+gitdetached-head(7)
+===================
+
+NAME
+----
+gitdetached-head - How detached HEAD state works
+
+DESCRIPTION
+-----------
+include::detached-head.adoc[]
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Documentation/meson.build b/Documentation/meson.build
index cfa9c67609..6b3141647f 100644
--- a/Documentation/meson.build
+++ b/Documentation/meson.build
@@ -197,6 +197,7 @@ manpages = {
   'gitcredentials.adoc' : 7,
   'gitcvs-migration.adoc' : 7,
   'gitdatamodel.adoc' : 7,
+  'gitdetached-head.adoc' : 7,
   'gitdiffcore.adoc' : 7,
   'giteveryday.adoc' : 7,
   'gitfaq.adoc' : 7,
diff --git a/advice.c b/advice.c
index 401d047391..3238e9b866 100644
--- a/advice.c
+++ b/advice.c
@@ -291,6 +291,7 @@ void detach_advice(const char *new_name)
 	"\n"
 	"  git switch -\n"
 	"\n"
+	"Run 'git help detached-head' to learn more.\n"
 	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
 
 	fprintf(stderr, fmt, new_name);
diff --git a/command-list.txt b/command-list.txt
index 63ae2a67c9..7fb57765fb 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -218,6 +218,7 @@ gitcore-tutorial                        guide
 gitcredentials                          guide
 gitcvs-migration                        guide
 gitdatamodel                            guide
+gitdetached-head                        guide
 gitdiffcore                             guide
 giteveryday                             guide
 gitfaq                                  guide
diff --git a/t/t2020-checkout-detach.sh b/t/t2020-checkout-detach.sh
index 28bbbe6c05..b0bfd3191c 100755
--- a/t/t2020-checkout-detach.sh
+++ b/t/t2020-checkout-detach.sh
@@ -228,6 +228,7 @@ test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not as
 
 	  git switch -
 
+	Run 'git help detached-head' to learn more.
 	Turn off this advice by setting config variable advice.detachedHead to false
 
 	HEAD is now at \$commit three
@@ -310,6 +311,7 @@ test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked
 
 	  git switch -
 
+	Run 'git help detached-head' to learn more.
 	Turn off this advice by setting config variable advice.detachedHead to false
 
 	HEAD is now at \$commit... three

base-commit: 5a7d1e8045ce66c908f62598e26cbb8df7b39a90
-- 
gitgitgadget
Re: [PATCH v7] doc: add new detached HEAD state guide
#3196
Author: Junio C Hamano
Date: Mon, 16 Nov 2026 22:08
40 lines
14508 bytes
"Julia Evans via GitGitGadget" <gitgitgadget@gmail.com> writes:

>  1:  64f50b8248 ! 1:  020aac7cc4 doc: checkout: rewrite detached HEAD state explanation
>      @@ Metadata
>       Author: Julia Evans <julia@jvns.ca>
>       
>        ## Commit message ##
>      -    doc: checkout: rewrite detached HEAD state explanation
>      +    doc: add new detached HEAD state guide
>       
>      -    The current explanation has the following issues:
>      +    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:

This makes it clearer what we are doing with this change.

>       +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. By detaching `HEAD`, you don't need to create a
>      -+temporary branch.
>      ++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

I've stared at this patch for too long that my eyes are
contaminated.  It would be great if somebody gave a hopefully final
pass with a fresh eye before we declare victory and merge it down to
'next'.

Thanks.
Re: [PATCH v7] doc: add new detached HEAD state guide
#4125
Author: "D. Ben Knoble"
Date: Mon, 30 Nov 2026 14:32
161 lines
18709 bytes
I think we may be nearing (or at?) the final version.

On Mon, Nov 16, 2026 at 10:33 AM Julia Evans via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Julia Evans <julia@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 <hello@marieflanagan.com>
> Signed-off-by: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
> Signed-off-by: Julia Evans <julia@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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads