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>
Junio C Hamano committed
Jan 8, 2007 at 02:42 UTC
ead80606d47ed94aad958ee660f62cde00bb6018
1 file changed
+18
git-checkout.sh
+18
@@ -14,6 +14,8 @@ branch=
14
newbranch=
15
newbranch_log=
16
merge=
17
+LF='
18
+'
19
while [ "$#" != "0" ]; do
20
arg="$1"
21
shift
@@ -164,6 +166,22 @@ associate a new branch with the wanted checkout. Example:
166
git checkout -b <new_branch_name> $arg
167
"
168
fi
169
+elif test -z "$oldbranch" && test -n "$branch"
170
+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 || {
179
+ 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."
182
+ exit 1;
183
+ }
184
+ fi
185
fi
186
187
if [ "X$old" = X ]