checkout: allow detaching to HEAD even when switching to the tip of a branch
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 master^0 Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Mar 29, 2007 at 01:02 UTC
fdc99cbbdcdcf4ee2c336f02b7b2dbcb11696392
2 files changed
+64
-3
git-checkout.sh
+2
-2
@@ -170,7 +170,7 @@ describe_detached_head () {
170
}
171
}
172
173
-if test -z "$branch$newbranch" && test "$new" != "$old"
173
+if test -z "$branch$newbranch" && test "$new_name" != "$old_name"
174
then
175
detached="$new"
176
if test -n "$oldbranch" && test -z "$quiet"
@@ -180,7 +180,7 @@ If you want to create a new branch from this checkout, you may do so
180
(now or later) by using -b with the checkout command again. Example:
181
git checkout -b <new_branch_name>"
182
fi
183
-elif test -z "$oldbranch"
183
+elif test -z "$oldbranch" && test "$new" != "$old"
184
then
185
describe_detached_head 'Previous HEAD position was' "$old"
186
fi
t/t7201-co.sh
+62
-1
@@ -3,7 +3,20 @@
3
# Copyright (c) 2006 Junio C Hamano
4
#
5
6
-test_description='git-checkout tests.'
6
+test_description='git-checkout tests.
7
+
8
+Creates master, forks renamer and side branches from it.
9
+Test switching across them.
10
+
11
+ ! [master] Initial A one, A two
12
+ * [renamer] Renamer R one->uno, M two
13
+ ! [side] Side M one, D two, A three
14
+ ---
15
+ + [side] Side M one, D two, A three
16
+ * [renamer] Renamer R one->uno, M two
17
+ +*+ [master] Initial A one, A two
18
+
19
+'
20
21
. ./test-lib.sh
22
@@ -129,4 +142,52 @@ test_expect_success 'checkout -m with merge conflict' '
142
! test -s current
143
'
144
145
+test_expect_success 'checkout to detach HEAD' '
146
+
147
+ git checkout -f renamer && git clean &&
148
+ git checkout renamer^ &&
149
+ H=$(git rev-parse --verify HEAD) &&
150
+ M=$(git show-ref -s --verify refs/heads/master) &&
151
+ test "z$H" = "z$M" &&
152
+ if git symbolic-ref HEAD >/dev/null 2>&1
153
+ then
154
+ echo "OOPS, HEAD is still symbolic???"
155
+ false
156
+ else
157
+ : happy
158
+ fi
159
+'
160
+
161
+test_expect_success 'checkout to detach HEAD with branchname^' '
162
+
163
+ git checkout -f master && git clean &&
164
+ git checkout renamer^ &&
165
+ H=$(git rev-parse --verify HEAD) &&
166
+ M=$(git show-ref -s --verify refs/heads/master) &&
167
+ test "z$H" = "z$M" &&
168
+ if git symbolic-ref HEAD >/dev/null 2>&1
169
+ then
170
+ echo "OOPS, HEAD is still symbolic???"
171
+ false
172
+ else
173
+ : happy
174
+ fi
175
+'
176
+
177
+test_expect_success 'checkout to detach HEAD with HEAD^0' '
178
+
179
+ git checkout -f master && git clean &&
180
+ git checkout HEAD^0 &&
181
+ H=$(git rev-parse --verify HEAD) &&
182
+ M=$(git show-ref -s --verify refs/heads/master) &&
183
+ test "z$H" = "z$M" &&
184
+ if git symbolic-ref HEAD >/dev/null 2>&1
185
+ then
186
+ echo "OOPS, HEAD is still symbolic???"
187
+ false
188
+ else
189
+ : happy
190
+ fi
191
+'
192
+
193
test_done