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 ...