@matheusbsbs / matheus-bagliano / commits / 75b364dfe2
Fork 0

git-checkout: safety check for detached HEAD checks existing refs

Checking for reachability from refs does not help much if the state we are currently on is somewhere in the middle. We will lose where we were. So this makes sureh that HEAD is something directly pointed at by one of the existing refs (most likely a tag for a user who has been "sightseeing"). Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jan 9, 2007 at 17:37 UTC 75b364dfe28cf1700ac4f519ff7d3e42505e17a6
1 file changed +7 -7
git-checkout.sh
+7 -7
@@ -171,14 +171,14 @@ then
171 # Coming back...
172 if test -z "$force"
173 then
174 - mb=$(git merge-base --all $old $new) &&
175 - case "$LF$mb$LF" in
176 - *"$LF$old$LF"*) : ;;
177 - *) false ;;
178 - esac || {
174 + git show-ref -d -s | grep "$old" >/dev/null || {
175 echo >&2 \
180 -"You are not on a branch and switching to $new_name branch may lose
181 -your changes. Use 'git checkout -f $new_name' if you want to."
176 +"You are not on any branch and switching to branch '$new_name'
177 +may lose your changes. At this point, you can do one of two things:
178 + (1) Decide it is Ok and say 'git checkout -f $new_name';
179 + (2) Start a new branch from the current commit, by saying
180 + 'git checkout -b <branch-name>'.
181 +Leaving your HEAD detached; not switching to branch '$new_name'."
182 exit 1;
183 }
184 fi