summaryrefslogtreecommitdiff
path: root/git-checkout.sh
diff options
context:
space:
mode:
authorJunio C Hamano <junkio@cox.net>2007-01-08 02:42:30 -0800
committerJunio C Hamano <junkio@cox.net>2007-01-08 03:02:11 -0800
commitead80606d47ed94aad958ee660f62cde00bb6018 (patch)
tree03f53b526480a5293d82d8e64a2660c203a3a4f6 /git-checkout.sh
parent73c838e4c9437a0c41082c32a3a832aa385460a9 (diff)
downloadgit-ead80606d47ed94aad958ee660f62cde00bb6018.tar.gz
git-checkout: safety when coming back from the detached HEAD state.
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>
Diffstat (limited to 'git-checkout.sh')
-rwxr-xr-xgit-checkout.sh18
1 files changed, 18 insertions, 0 deletions
diff --git a/git-checkout.sh b/git-checkout.sh
index 3250f64ccc..69d0c1c43a 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -14,6 +14,8 @@ branch=
newbranch=
newbranch_log=
merge=
+LF='
+'
while [ "$#" != "0" ]; do
arg="$1"
shift
@@ -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 ]