Skip to content

Commit

Permalink
git-checkout: safety when coming back from the detached HEAD state.
Browse files Browse the repository at this point in the history
After making commits in the detached HEAD state, if you run "git
checkout" to switch to an existing branch, you will lose your
work.  Make sure the switched-to branch is a fast-forward of the
current HEAD, or require -f when switching.

Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
Junio C Hamano committed Jan 8, 2007
1 parent 73c838e commit ead8060
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions git-checkout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ branch=
newbranch=
newbranch_log=
merge=
LF='
'
while [ "$#" != "0" ]; do
arg="$1"
shift
Expand Down Expand Up @@ -164,6 +166,22 @@ associate a new branch with the wanted checkout. Example:
git checkout -b <new_branch_name> $arg
"
fi
elif test -z "$oldbranch" && test -n "$branch"
then
# Coming back...
if test -z "$force"
then
mb=$(git merge-base --all $old $new) &&
case "$LF$mb$LF" in
*"$LF$old$LF"*) : ;;
*) false ;;
esac || {
echo >&2 \
"You are not on a branch and switching to $new_name branch may lose
your changes. Use 'git checkout -f $new_name' if you want to."
exit 1;
}
fi
fi

if [ "X$old" = X ]
Expand Down

0 comments on commit ead8060

Please sign in to comment.