Allow git-checkout when on a non-existant branch.
I've seen some users get into situtations where their HEAD symbolic-ref is pointing at a non-existant ref. (Sometimes this happens during clone when the remote repository lacks a 'master' branch.) If this happens the user is unable to use git-checkout to switch branches as there is no prior commit to merge from. So instead of giving the user low-level errors about how HEAD can't be resolved and how not a single revision was given change the type of checkout to be a force and go through with the user's request anyway. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn Pearce committed
Sep 25, 2006 at 01:24 UTC
5a03e7f25334a6bf1dbbfdb9830d41de5b8f0d7f
2 files changed
+17
-1
git-checkout.sh
+8
-1
@@ -4,8 +4,8 @@ USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4
SUBDIRECTORY_OK=Sometimes
5
. git-sh-setup
6
7
-old=$(git-rev-parse HEAD)
7
old_name=HEAD
8
+old=$(git-rev-parse --verify $old_name 2>/dev/null)
9
new=
10
new_name=
11
force=
@@ -139,6 +139,13 @@ fi
139
die "git checkout: to checkout the requested commit you need to specify
140
a name for a new branch which is created and switched to"
141
142
+if [ "X$old" = X ]
143
+then
144
+ echo "warning: You do not appear to currently be on a branch." >&2
145
+ echo "warning: Forcing checkout of $new_name." >&2
146
+ force=1
147
+fi
148
+
149
if [ "$force" ]
150
then
151
git-read-tree --reset -u $new
t/t7201-co.sh
+9
@@ -31,6 +31,15 @@ test_expect_success setup '
31
git checkout master
32
'
33
34
+test_expect_success "checkout from non-existing branch" '
35
+
36
+ git checkout -b delete-me master &&
37
+ rm .git/refs/heads/delete-me &&
38
+ test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
39
+ git checkout master &&
40
+ test refs/heads/master = "$(git symbolic-ref HEAD)"
41
+'
42
+
43
test_expect_success "checkout with dirty tree without -m" '
44
45
fill 0 1 2 3 4 5 >one &&