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

Subsystems: the rest

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

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

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:43:02

Xavier Maillard [off-list ref] writes:
   > 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 [off-list ref]
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
 ...

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

From: Linus Torvalds <torvalds@linux-foundation.org>
Date: 2016-06-15 22:43:02


On Tue, 3 Apr 2007, Junio C Hamano wrote:
Could anybody remind me why we have the "new != old" check here?
Afaik, it's just so that you can do

	git checkout -f

without having it complain. And I think it just comes from the earlier:

	[ -z "$new" ] && new=$old && new_name="$old_name"

that we did - ie without that it would incorrectly just overwrite the 
current working tree with the new commit, but not actually do the "git 
reset" part.

So without that check, the way things used to work (*before* detached 
heads), if you were to have done

	git checkout <some-random-head>

it would have screwed up the current branch horribly.

With detached heads, I don't think it's needed.

		Linus
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help