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

Re: [1.8.0] git checkout refs/heads/foo checks out branch foo

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 7, 2011, 23:37 UTC
Message-ID
<7vaai7s9g4.fsf@alter.siamese.dyndns.org>
In-Reply-To
<20110207220030.GA19357@elie>
Jonathan Nieder <jrnieder@gmail.com> writes:
Show 15 quoted lines
> Sverre Rabbelier wrote:
>
>> Now _that_ is an excellent usability improvement, assuming we want to
>> encourage detaching HEAD... do we?
>
> Yes.
>
> -- 8<
> Subject: commit: document --detach synonym for "git checkout foo^{commit}"
>
> For example, one might use this when making a temporary merge to test
> that two topics work well together.
>
> This patch just documents the option.  It is not meant for application
> without an implementation and tests for the option.
On top of v1.7.3.5-1-g0cb6ad3 (uk/checkout-ambiguous-ref)...
 builtin/checkout.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 953abdd..141f6a3 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -685,6 +685,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	char *conflict_style = NULL;
 	int patch_mode = 0;
 	int dwim_new_local_branch = 1;
+	int force_detach = 0;
 	struct option options[] = {
 		OPT__QUIET(&opts.quiet),
 		OPT_STRING('b', NULL, &opts.new_branch, "branch",
@@ -692,6 +693,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		OPT_STRING('B', NULL, &opts.new_branch_force, "branch",
 			   "create/reset and checkout a branch"),
 		OPT_BOOLEAN('l', NULL, &opts.new_branch_log, "create reflog for new branch"),
+		OPT_BOOLEAN(0, "detach", &force_detach, "detach the HEAD at named commit"),
 		OPT_SET_INT('t', "track",  &opts.track, "set upstream info for new branch",
 			BRANCH_TRACK_EXPLICIT),
 		OPT_STRING(0, "orphan", &opts.new_orphan_branch, "new branch", "new unparented branch"),
@@ -726,6 +728,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	if (opts.new_branch && opts.new_branch_force)
 		die("-B cannot be used with -b");
 
+	if ((opts.new_branch || opts.new_orphan_branch) && force_detach)
+		die("--detach cannot be used with -b/-B/--orphan");
+
 	/* copy -B over to -b, so that we can just check the latter */
 	if (opts.new_branch_force)
 		opts.new_branch = opts.new_branch_force;
@@ -834,7 +839,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		new.name = arg;
 		setup_branch_path(&new);
 
-		if (check_ref_format(new.path) == CHECK_REF_FORMAT_OK &&
+		if (!force_detach &&
+		    check_ref_format(new.path) == CHECK_REF_FORMAT_OK &&
 		    resolve_ref(new.path, branch_rev, 1, NULL))
 			hashcpy(rev, branch_rev);
 		else
Previous: Jonathan NiederNext: Jeff King
Message 6 of 18 in “[1.8.0] git checkout refs/heads/foo checks out branch foo”
  1. Martin von ZweigbergkFeb 7, 2011
  2. Junio C HamanoFeb 7, 2011
  3. Jeff KingFeb 7, 2011
  4. Sverre RabbelierFeb 7, 2011
  5. Jonathan NiederFeb 7, 2011
  6. Junio C HamanoFeb 7, 2011
  7. Jeff KingFeb 7, 2011
  8. Jonathan NiederFeb 8, 2011
  9. Jeff KingFeb 8, 2011
  10. Martin von ZweigbergkFeb 8, 2011
  11. checkout: introduce --detach synonym for "git checkout foo^{commit}"Jonathan Nieder, Feb 8, 2011
  12. Jonathan NiederFeb 8, 2011
  13. 0/3 checkout: introduce --detach synonym for "git checkout foo^{commit}"Jonathan Nieder, Feb 8, 2011
  14. 1/3 checkout: split off a function to peel away branchname argJonathan Nieder, Feb 8, 2011
  15. 2/3 checkout: introduce --detach synonym for "git checkout foo^{commit}"Jonathan Nieder, Feb 8, 2011
  16. 3/3 checkout: rearrange update_refs_for_switch for clarityJonathan Nieder, Feb 8, 2011
  17. Heiko VoigtFeb 7, 2011
  18. Martin von ZweigbergkFeb 8, 2011

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.