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

Re: [PATCH] checkout -d: explicitly detach HEAD even when switching to the tip of a branch

From
Junio C Hamano <junkio@cox.net>
Date
Apr 4, 2007, 00:11 UTC
Message-ID
<7vtzvxt2v3.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<200703311455.l2VEtHO5021300@localhost.localdomain>
Xavier Maillard <zedek@gnu.org> writes:
Show 15 quoted lines
>    > You cannot currently checkout the tip of an existing branch
>    > without moving to the branch.
>    > 
>    > This allows you to detach your HEAD and place it at such a
>    > commit, with:
>    > 
>    >     $ git checkout -d master
>
>    What about
>
> 	 $ git checkout master^0
>
>    trick to force detaching?
>
> I love this idea.
Could anybody remind me why we have the "new != old" check here?
diff --git a/git-checkout.sh b/git-checkout.sh
index a7390e8..573a3c0 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -170,7 +170,7 @@ describe_detached_head () {
 	}
 }
 
-if test -z "$branch$newbranch" && test "$new" != "$old"
+if test -z "$branch$newbranch"
 then
 	detached="$new"
 	if test -n "$oldbranch" && test -z "$quiet"

I think I do not need an explicit -d option if we just remove
that second test.  It is coming from an ancient commit 91dcdfd3,
and I _think_ it was to prevent something like:

	git-checkout-script v2.6.12^0

from succeeding, while allowing

	git-checkout-script HEAD^0

to be a no-op (as it happens to be naming the same commit).

commit 91dcdfd3b5331d955cfb60edf8930f1b5c142905
Author: Linus Torvalds <torvalds@g5.osdl.org>
Date:   Mon Jul 11 20:44:20 2005 -0700

    Make "git checkout" create new branches on demand

diff --git a/git-checkout-script b/git-checkout-script
index 48e1da9..7e70338 100755
--- a/git-checkout-script
+++ b/git-checkout-script
@@ -5,10 +5,19 @@ old=$(git-rev-parse HEAD)
 ...
@@ -32,6 +41,16 @@ while [ "$#" != "0" ]; do
 done
 [ -z "$new" ] && new=$old
 
+#
+# If we don't have an old branch that we're switching to,
+# and we don't have a new branch name for the target we
+# are switching to, then we'd better just be checking out
+# what we already had
+#
+[ -z "$branch$newbranch" ] &&
+	[ "$new" != "$old" ] &&
+	die "git checkout: you need to specify a new branch name"
+
 if [ "$force" ]
 then
     git-read-tree --reset $new &&
@@ -47,6 +66,10 @@ fi
 ...
Previous: Xavier MaillardNext: Linus Torvalds
Message 7 of 8 in “checkout -d: explicitly detach HEAD even when switching to the tip of a branch”
  1. checkout -d: explicitly detach HEAD even when switching to the tip of a branchJunio C Hamano, Mar 29, 2007
  2. Xavier MaillardMar 29, 2007
  3. Alex RiesenMar 29, 2007
  4. Xavier MaillardMar 29, 2007
  5. Jakub NarebskiMar 31, 2007
  6. Xavier MaillardMar 31, 2007
  7. Junio C HamanoApr 4, 2007
  8. Linus TorvaldsApr 4, 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.