{"thread":{"id":"5688","subject":"[PATCH] Allow git-checkout when on a non-existant branch.","startedAt":"2006-09-25T05:24:38Z","lastAt":"2006-09-25T05:24:38Z","messageCount":1,"participants":["Shawn Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"27611","messageId":"20060925052438.GA16023@spearce.org","threadId":"5688","inReplyTo":null,"subject":"[PATCH] Allow git-checkout when on a non-existant branch.","fromName":"Shawn Pearce","fromEmail":"spearce@spearce.org","sentAt":"2006-09-25T05:24:38Z","receivedAt":"2006-09-25T05:24:38Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"I've seen some users get into situtations where their HEAD\nsymbolic-ref is pointing at a non-existant ref.  (Sometimes this\nhappens during clone when the remote repository lacks a 'master'\nbranch.)  If this happens the user is unable to use git-checkout\nto switch branches as there is no prior commit to merge from.\n\nSo instead of giving the user low-level errors about how HEAD\ncan't be resolved and how not a single revision was given change\nthe type of checkout to be a force and go through with the user's\nrequest anyway.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n git-checkout.sh |    9 ++++++++-\n t/t7201-co.sh   |    9 +++++++++\n 2 files changed, 17 insertions(+), 1 deletions(-)\n\ndiff --git a/git-checkout.sh b/git-checkout.sh\nindex f03620b..119bca1 100755\n--- a/git-checkout.sh\n+++ b/git-checkout.sh\n@@ -4,8 +4,8 @@ USAGE='[-f] [-b <new_branch>] [-m] [<bra\n SUBDIRECTORY_OK=Sometimes\n . git-sh-setup\n \n-old=$(git-rev-parse HEAD)\n old_name=HEAD\n+old=$(git-rev-parse --verify $old_name 2>/dev/null)\n new=\n new_name=\n force=\n@@ -140,6 +140,13 @@ # what we already had\n \tdie \"git checkout: to checkout the requested commit you need to specify \n               a name for a new branch which is created and switched to\"\n \n+if [ \"X$old\" = X ]\n+then\n+\techo \"warning: You do not appear to currently be on a branch.\" >&2\n+\techo \"warning: Forcing checkout of $new_name.\" >&2\n+\tforce=1\n+fi\n+\n if [ \"$force\" ]\n then\n     git-read-tree --reset -u $new\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex b64e8b7..085d4a0 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -31,6 +31,15 @@ test_expect_success setup '\n \tgit checkout master\n '\n \n+test_expect_success \"checkout from non-existing branch\" '\n+\n+\tgit checkout -b delete-me master &&\n+\trm .git/refs/heads/delete-me &&\n+\ttest refs/heads/delete-me = \"$(git symbolic-ref HEAD)\" &&\n+\tgit checkout master &&\n+\ttest refs/heads/master = \"$(git symbolic-ref HEAD)\"\n+'\n+\n test_expect_success \"checkout with dirty tree without -m\" '\n \n \tfill 0 1 2 3 4 5 >one &&\n-- \n1.4.2.1.g7a39b\n"}]}