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

Re: [PATCH] Detached HEAD (experimental)

From
ETEdgar Toernig <froese@gmx.de>
Date
Jan 2, 2007, 19:59 UTC
Message-ID
<20070102205901.3f4a9f1e.froese@gmx.de>
In-Reply-To
<7vac11yirf.fsf@assigned-by-dhcp.cox.net>
[Note: casual git-user speaking]
Junio C Hamano wrote:
>
> This allows "git checkout -d v1.4.3" to detach the HEAD from any
> branch but point directly at the named commit.  After this, "git
> branch" starts reporting that you are not on any branch.
Nice.  But why -d?  Create more confusion? [1]
> You can go back the normal state by switching to an existing
> branch, say, "git checkout master" for example.

This is fine. Often you want to test a couple of tags, one after another, so "git checkout v1", "git checkout v2", ... should work.

> Another way to get out of this is "git checkout -b newbranch".

Wth should this do? I already noticed this line in your posting from 29.Dec: [slightly edited]

	$ git checkout v1.5.0
	Checking out a tag -- you are not on any branch now...
	$ <modify>
	$ git commit -m 'fix' -a
	You cannot commit without a current branch.
	$ git checkout -b maint-1.5.0
	$ git commit -m 'fix' -a
I assume it will create a new branch and modify HEAD so that
the current working dir/index gets committed into that branch.
(Basically "git branch main-1.5.0 &&
            echo 'ref: refs/head/main-1.5.0' >.git/HEAD")
If that's the case, I was looking for that incantation for
a long time and couldn't find it.  I'm using the git-branch
and echo as shown above to get that.  The man-page isn't
very helpful for that special case of git-checkout.

Anyway, if I want to commit and git tells me that I can't because I'm not on a branch, the _most unintuitive_ thing would be calling 'git-checkout'. I want to checkin! No way that I call checkout and risk losing all my changes.

What's wrong with a -b option to commit, similar to -b on checkout?

	$ git checkout v1.5.0
	Checking out a tag -- you are not on any branch now...
	$ <modify>
	$ git commit -m 'fix' -a
	You cannot commit without a current branch.
	Give '-b <newbranch>' to commit into a new branch. 
	$ git commit -b maint-1.5.0 -m 'fix' -a

Another variant (the one I prefer): commit just updates HEAD and git-branch can be used to give it a name (and switch to it!). So these workflows would be possible:

Name after commit:
	$ git checkout v1.5.0
	Checking out a tag -- you are not on any branch now...
	$ git branch
	  master
	* (unnamed) c8ff51290518949225c832bae1e22b1bba6ab2cd
	$ <modify>
	$ git commit -m 'fix' -a
	Warning: data committed into unnamed branch.
	Give it a name now with "git branch <newname>"
	$ git branch
	  master
	* (unnamed) 13482f25863e5380cdd41065338e1709d469a605
	$ git branch maint-1.5.0
	$ git branch
	  master
	* main-1.5.0
or name before commit:
	$ git checkout v1.5.0
	Checking out a tag -- you are not on any branch now...
	$ <modify>
	$ git branch
	  master
	* (unnamed) c8ff51290518949225c832bae1e22b1bba6ab2cd
	$ git branch maint-1.5.0
	$ git branch
	  master
	* maint-1.5.0
	$ git commit -m 'fix' -a

Yes, 'git branch <newname>' would get a new semantic: if no start-point is given the newname will become the new current branch.

[Btw, I would even do that when we are on some branch. How often did I do a checkout of a regular branch and only later noticed, that I want to commit changes into a temp-branch:

	$ git checkout master
	$ <play around, fix compile issues, add debug stuff>
	$ git branch debug
	$ git branch
	  master
	* debug
	$ git commit -a -m "Add foo debugging code"
]

But that special case is IMHO (M = my, a casual user's) much better than some weird checkout-incantations.

Ciao, ET.
[1] My pet-peeve:
	$ git checkout foo
	fatal: Entry 'bar' not uptodate. Cannot merge.
    What the heck?  Nobody asked for a merge!?!?!  What
    is it trying to do?  Does it actually mean:
	fatal: Working dir is dirty.  Either give '-f'
	to force the checkout and lose your changes, or
	give '-m' to merge 'foo' and your changes.
   ?  But then, why does an 'rm bar' fixes that?  Now 'bar'
   definitely isn't "uptodate".
Previous: Junio C HamanoNext: Carl Worth
Message 2 of 68 in “Detached HEAD (experimental)”
  1. Detached HEAD (experimental)Junio C Hamano, Jan 2, 2007
  2. Edgar ToernigJan 2, 2007
  3. Carl WorthJan 2, 2007
  4. Jakub NarebskiJan 2, 2007
  5. Carl WorthJan 3, 2007
  6. J. Bruce FieldsJan 6, 2007
  7. Alan ChandlerJan 6, 2007
  8. J. Bruce FieldsJan 6, 2007
  9. Junio C HamanoJan 2, 2007
  10. Carl WorthJan 2, 2007
  11. Junio C HamanoJan 3, 2007
  12. Junio C HamanoJan 8, 2007
  13. Jeff KingJan 8, 2007
  14. Junio C HamanoJan 9, 2007
  15. Carl WorthJan 9, 2007
  16. Junio C HamanoJan 9, 2007
  17. Carl WorthJan 9, 2007
  18. Shawn O. PearceJan 9, 2007
  19. Junio C HamanoJan 9, 2007
  20. 0/6 Expose in_merge_bases() via merge-base.Junio C Hamano, Jan 9, 2007
  21. Luben TuikovJan 9, 2007
  22. Jeff KingJan 9, 2007
  23. Junio C HamanoJan 9, 2007
  24. J. Bruce FieldsJan 9, 2007
  25. Carl WorthJan 9, 2007
  26. J. Bruce FieldsJan 9, 2007
  27. Shawn O. PearceJan 9, 2007
  28. Jakub NarebskiJan 10, 2007
  29. Shawn O. PearceJan 10, 2007
  30. J. Bruce FieldsJan 10, 2007
  31. Shawn O. PearceJan 10, 2007
  32. Nicolas PitreJan 10, 2007
  33. Junio C HamanoJan 10, 2007
  34. Shawn O. PearceJan 10, 2007
  35. Nicolas PitreJan 10, 2007
  36. Shawn O. PearceJan 10, 2007
  37. Jakub NarebskiJan 10, 2007
  38. Andreas EricssonJan 10, 2007
  39. Junio C HamanoJan 10, 2007
  40. Daniel BarkalowJan 10, 2007
  41. Andreas EricssonJan 11, 2007
  42. Junio C HamanoJan 10, 2007
  43. Junio C HamanoJan 9, 2007
  44. Shawn O. PearceJan 9, 2007
  45. Linus TorvaldsJan 9, 2007
  46. Junio C HamanoJan 10, 2007
  47. Shawn O. PearceJan 10, 2007
  48. Linus TorvaldsJan 10, 2007
  49. Carl WorthJan 10, 2007
  50. Junio C HamanoJan 10, 2007
  51. Andy ParkinsJan 10, 2007
  52. Shawn O. PearceJan 10, 2007
  53. Junio C HamanoJan 10, 2007
  54. Andy ParkinsJan 10, 2007
  55. Shawn O. PearceJan 10, 2007
  56. Junio C HamanoJan 10, 2007
  57. Jeff KingJan 10, 2007
  58. Junio C HamanoJan 11, 2007
  59. J. Bruce FieldsJan 11, 2007
  60. Jeff KingJan 3, 2007
  61. Jeff KingJan 3, 2007
  62. git-branch: show detached HEADLars Hjemli, Jan 2, 2007
  63. Shawn O. PearceJan 3, 2007
  64. Junio C HamanoJan 3, 2007
  65. Lars HjemliJan 3, 2007
  66. Junio C HamanoJan 3, 2007
  67. Junio C HamanoJan 3, 2007
  68. Lars HjemliJan 3, 2007

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.