Skip to content

Commit 9ea5c63

Browse files
committed
Merge branch 'cw/no-detaching-an-unborn' into maint
"git checkout --detach", when you are still on an unborn branch, should be forbidden, but it wasn't. * cw/no-detaching-an-unborn: git-checkout: disallow --detach on unborn branch
2 parents bb3ed29 + 8ced1aa commit 9ea5c63

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

builtin/checkout.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,8 @@ static int switch_unborn_to_new_branch(struct checkout_opts *opts)
915915
int status;
916916
struct strbuf branch_ref = STRBUF_INIT;
917917

918+
if (!opts->new_branch)
919+
die(_("You are on a branch yet to be born"));
918920
strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch);
919921
status = create_symref("HEAD", branch_ref.buf, "checkout -b");
920922
strbuf_release(&branch_ref);

t/t2017-checkout-orphan.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,10 @@ test_expect_success '--orphan refuses to switch if a merge is needed' '
116116
git reset --hard
117117
'
118118

119+
test_expect_success 'cannot --detach on an unborn branch' '
120+
git checkout master &&
121+
git checkout --orphan new &&
122+
test_must_fail git checkout --detach
123+
'
124+
119125
test_done

0 commit comments

Comments
 (0)