git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: master^ is not a local branch -- huh?!?

From
Jay Soffian <jaysoffian@gmail.com>
Date
Jan 30, 2010, 04:52 UTC
Message-ID
<76718491001292052x7f46d479lfeff7b66121502c3@mail.gmail.com>
In-Reply-To
<7vbpgc8fhb.fsf@alter.siamese.dyndns.org>
On Fri, Jan 29, 2010 at 9:59 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 22 quoted lines
> Ron Garret <ron1@flownet.com> writes:
>
>> 1.  The term "detached HEAD" is inherently misleading.  A detached HEAD
>> isn't detached from anything, it's just pointing to the middle of a
>> branch, which is to say, to a commit that happens to already have
>> descendants.  For that matter, the name HEAD is itself misleading, since
>> HEAD need not be the head of a branch (though normally it is).  A better
>> name for HEAD would have been CURRENT or ACTIVE.  I recognize it's
>> probably too late to change it now.
>
> This description, especially the phrase "middle of a branch" shows that
> you don't understand git yet.  A git branch is _not_ a line (nor multiple
> lines) of development.  It is merely a _point_ in the history.
>
> "A commit that is in the middle of an ancestry chain with existing
> descendants" can be at the tip of a branch and does not have anything to
> do with detached HEAD state.
>
> When HEAD points at a branch, making a commit advances _that_ branch.  And
> we say you are "on that branch".  When HEAD is detached, because it is not
> attached to anything, it advances no branch.  "detached HEAD" is detached
> in the very real sense.  It is not attached to _any_ branch.

Let me try wording this slightly different, because I think I can see Ron's confusion.

HEAD normally refers to a named branch. For example "master" (technically, HEAD would contain "ref: refs/heads/master"), but we'll just say "master" for now.

Meanwhile, the branch named "master" refers to a specific commit by its SHA-1 hash.

The particular commit which "master" refers to is a branch head.

Now, when you create a commit in this state, the branch named "master" is updated with the SHA-1 of the new commit. So let's say you create a new repo and have three commits. Your history would look like this:

a---b---c master (HEAD is "ref: refs/heads/master")

That is, if you look at .git/HEAD, it will say "ref: refs/heads/master" and if you look at .git/refs/heads/master it will have the SHA-1 of commit "c". If you create a new commit:

a---b---c---d master (HEAD is "ref: refs/heads/master")

.git/HEAD still says "ref: refs/heads/master" but now .git/refs/heads/master has the SHA-1 of commit "d".

Okay, now let's talk about what happens when you type:
$ git checkout master^
At this point, git updates HEAD to contain the SHA-1 of "c":
a---b---c---d master (HEAD is c's SHA-1)

You now have a "detached HEAD" because HEAD doesn't refer to any named branch. Instead it refers to a specific commit by its SHA-1. So let's create a new commit while HEAD is detached:

a---b---c---d master
         \
          e   (HEAD is e's SHA-1)

So, yes, you've created a "branch" in the DAG sense of the word. But this branch is anonymous since it has no name. That means that commit "e" is subject to garbage collection and may be removed. If you want to keep it around, then you need to create a name for it. Git provides you a number of ways to "name" commits:

$ git checkout -b foo # (1) $ git branch foo # (2) $ git tag foo # (3)

(1) will create .git/refs/heads/foo, make it have the SHA-1 of commit "e", then update HEAD to say "ref: refs/heads/foo". You are now "on" branch "foo" and any commits you create will update .git/refs/heads/foo:

a---b---c---d master
         \
          e  foo (HEAD is "ref: refs/heads/foo")

(2) will also create .refs/heads/foo, and make it have the SHA-1 of commit "e", but will leave HEAD as it is:

a---b---c---d master
         \
          e  foo (HEAD is SHA-1 of "e")

You still have a detached HEAD and any commits you create that descend from "e" are still subject to garbage collection (although "e" itself is not), as follows:

a---b---c---d master
         \
          e  <-- foo
           \
            f (HEAD is SHA-1 of "f")
(3) creates .refs/tags/foo, but is otherwise the same as (2).

So that was a really long explanation, but I hope it clears things up. I think the disconnect between you and Junio is that you're thinking of branches in the DAG sense of the word, while Junio is talking about them in the context of git.

j.
Previous: Junio C HamanoNext: Nicolas Pitre
Message 40 of 94 in “master^ is not a local branch -- huh?!?”
  1. Ron1Jan 29, 2010
  2. Jacob HelwigJan 29, 2010
  3. Sverre RabbelierJan 29, 2010
  4. Jacob HelwigJan 29, 2010
  5. Junio C HamanoJan 29, 2010
  6. Sverre RabbelierJan 29, 2010
  7. checkout: warn about 'branch name' rather than 'local branch'Sverre Rabbelier, Jan 29, 2010
  8. checkout: Fix test for s/local branch/branch name/ change.Jacob Helwig, Jan 29, 2010
  9. Sverre RabbelierJan 29, 2010
  10. Nicolas PitreJan 29, 2010
  11. Ron GarretJan 29, 2010
  12. Junio C HamanoJan 29, 2010
  13. Sverre RabbelierJan 29, 2010
  14. Junio C HamanoJan 29, 2010
  15. Nicolas PitreJan 29, 2010
  16. Sverre RabbelierJan 29, 2010
  17. Nicolas PitreJan 29, 2010
  18. Sverre RabbelierJan 29, 2010
  19. Nicolas PitreJan 29, 2010
  20. Junio C HamanoJan 29, 2010
  21. Sverre RabbelierJan 29, 2010
  22. Nicolas PitreJan 29, 2010
  23. Junio C HamanoJan 29, 2010
  24. Jacob HelwigJan 29, 2010
  25. Nicolas PitreJan 29, 2010
  26. Junio C HamanoJan 30, 2010
  27. Sverre RabbelierJan 30, 2010
  28. Junio C HamanoJan 30, 2010
  29. Sverre RabbelierJan 30, 2010
  30. Michael WittenJan 30, 2010
  31. Nicolas PitreJan 30, 2010
  32. Mark LodatoJan 30, 2010
  33. Nicolas PitreJan 30, 2010
  34. Ron GarretJan 30, 2010
  35. Junio C HamanoJan 30, 2010
  36. Ron GarretJan 30, 2010
  37. Nicolas PitreJan 30, 2010
  38. Jay SoffianJan 30, 2010
  39. Junio C HamanoJan 30, 2010
  40. Jay SoffianJan 30, 2010
  41. Nicolas PitreJan 30, 2010
  42. Junio C HamanoJan 30, 2010
  43. Ron GarretJan 30, 2010
  44. Junio C HamanoJan 30, 2010
  45. Jay SoffianJan 30, 2010
  46. Junio C HamanoJan 30, 2010
  47. Ron GarretJan 30, 2010
  48. Mark LodatoJan 30, 2010
  49. Nicolas PitreJan 30, 2010
  50. Mark LodatoJan 30, 2010
  51. Nicolas PitreJan 30, 2010
  52. Nicolas PitreJan 30, 2010
  53. Jay SoffianJan 30, 2010
  54. Nicolas PitreJan 30, 2010
  55. Mark LodatoJan 30, 2010
  56. Junio C HamanoJan 30, 2010
  57. Jeff KingJan 30, 2010
  58. Ron GarretJan 30, 2010
  59. A Large Angry SCMJan 29, 2010
  60. Junio C HamanoJan 29, 2010
  61. Johannes SchindelinJan 30, 2010
  62. Nicolas PitreJan 30, 2010
  63. Johannes SchindelinJan 29, 2010
  64. Ron1Jan 29, 2010
  65. Jacob HelwigJan 29, 2010
  66. Ron GarretJan 29, 2010
  67. Junio C HamanoJan 29, 2010
  68. Ron GarretJan 29, 2010
  69. Junio C HamanoJan 31, 2010
  70. Octavio AlvarezJan 29, 2010
  71. Ron GarretJan 29, 2010
  72. Octavio AlvarezJan 29, 2010
  73. Ron GarretJan 29, 2010
  74. Octavio AlvarezJan 29, 2010
  75. Junio C HamanoJan 29, 2010
  76. Ron GarretJan 29, 2010
  77. Julian PhillipsJan 29, 2010
  78. Ron GarretJan 30, 2010
  79. Ron GarretJan 30, 2010
  80. Junio C HamanoJan 30, 2010
  81. Ron GarretJan 30, 2010
  82. Scott R. GodinJan 29, 2010
  83. Ron GarretJan 29, 2010
  84. Sverre RabbelierJan 29, 2010
  85. Ron GarretJan 29, 2010
  86. Junio C HamanoJan 29, 2010
  87. Ron GarretJan 29, 2010
  88. Michael WittenJan 30, 2010
  89. Junio C HamanoJan 30, 2010
  90. Ron GarretJan 30, 2010
  91. Junio C HamanoJan 30, 2010
  92. Ron GarretJan 30, 2010
  93. Junio C HamanoJan 30, 2010
  94. Ron GarretJan 30, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.