Skip to content

Commit dc9195a

Browse files
Nicolas PitreJunio C Hamano
authored andcommitted
Let git-checkout always drop any detached head
We used to refuse leaving a detached HEAD when it wasn't matching an existing ref so not to lose any commit that might have been performed while not on any branch (unless -f was provided). But this protection was completely bogus since it was still possible to move to HEAD^ while still remaining detached but losing the last commit anyway if there was one. Now that we have a proper reflog for HEAD it is best to simply remove that bogus (and admitedly annoying) protection and simply display the last HEAD position instead. If one wants to recover a lost detached state then it can be retrieved from the HEAD reflog. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent f2eba66 commit dc9195a

File tree

1 file changed

+2
-15
lines changed

1 file changed

+2
-15
lines changed

git-checkout.sh

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,9 @@ If you want to create a new branch from this checkout, you may do so
164164
(now or later) by using -b with the checkout command again. Example:
165165
git checkout -b <new_branch_name>"
166166
fi
167-
elif test -z "$oldbranch" && test -n "$branch"
167+
elif test -z "$oldbranch" && test -z "$quiet"
168168
then
169-
# Coming back...
170-
if test -z "$force"
171-
then
172-
git show-ref -d -s | grep "$old" >/dev/null || {
173-
echo >&2 \
174-
"You are not on any branch and switching to branch '$new_name'
175-
may lose your changes. At this point, you can do one of two things:
176-
(1) Decide it is Ok and say 'git checkout -f $new_name';
177-
(2) Start a new branch from the current commit, by saying
178-
'git checkout -b <branch-name>'.
179-
Leaving your HEAD detached; not switching to branch '$new_name'."
180-
exit 1;
181-
}
182-
fi
169+
echo >&2 "Previous HEAD position was $old"
183170
fi
184171

185172
if [ "X$old" = X ]

0 commit comments

Comments
 (0)