Skip to content

Commit fdc99cb

Browse files
author
Junio C Hamano
committed
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 <[email protected]>
1 parent cbb84e5 commit fdc99cb

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

git-checkout.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe_detached_head () {
170170
}
171171
}
172172

173-
if test -z "$branch$newbranch" && test "$new" != "$old"
173+
if test -z "$branch$newbranch" && test "$new_name" != "$old_name"
174174
then
175175
detached="$new"
176176
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
180180
(now or later) by using -b with the checkout command again. Example:
181181
git checkout -b <new_branch_name>"
182182
fi
183-
elif test -z "$oldbranch"
183+
elif test -z "$oldbranch" && test "$new" != "$old"
184184
then
185185
describe_detached_head 'Previous HEAD position was' "$old"
186186
fi

t/t7201-co.sh

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@
33
# Copyright (c) 2006 Junio C Hamano
44
#
55

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+
'
720

821
. ./test-lib.sh
922

@@ -129,4 +142,52 @@ test_expect_success 'checkout -m with merge conflict' '
129142
! test -s current
130143
'
131144

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+
132193
test_done

0 commit comments

Comments
 (0)