mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Taylor Blau <>
Cc: Jeff King <>, Martin von Zweigbergk <>
Subject: [PATCH] branch: gracefully handle '-d' on detached HEAD
Date: Tue, 1 Nov 2022 16:32:18 -0400	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <Y2F9lkCWf/2rjT2E@nand.local>

From: Jeff King <>

Since 67affd5173 (git-branch -D: make it work even when on a
yet-to-be-born branch, 2006-11-24), 'git branch -d' refuses to work when
orphaned, since there is no HEAD to resolve.

But since 67affd5173, there have been other checks, like 99c419c915
(branch -d: base the "already-merged" safety on the branch it merges
with, 2009-12-29), which makes it OK to delete a branch if it is merged
to HEAD or its upstream.

99c419c915 makes the check in 67affd5173 wrong, since it's OK to delete
a branch if it is merged to its upstream.

Since the code in delete_branches() tolerates a NULL head_rev perfectly
fine, make it non-fatal to fail to resolve a commit object at HEAD.

Signed-off-by: Jeff King <>
Signed-off-by: Taylor Blau <>
OK, here's that patch. Since Peff wrote the main portion of it, he gets
authorship credit. My version comes with a test, which we should
consider picking up.

Even though it doesn't resolve Martin's original scenario (i.e., he'd
still have to use -D or -f to actually delete 'main' there), I still
think the bugfix is worth pursuing in its own right.

 builtin/branch.c  | 5 +----
 t/ | 7 +++++++
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 15be0c03ef..f6ff9084c8 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -235,11 +235,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 	branch_name_pos = strcspn(fmt, "%");

-	if (!force) {
+	if (!force)
 		head_rev = lookup_commit_reference(the_repository, &head_oid);
-		if (!head_rev)
-			die(_("Couldn't look up commit object for HEAD"));
-	}

 	for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
 		char *target = NULL;
diff --git a/t/ b/t/
index 7f605f865b..464d3f610b 100755
--- a/t/
+++ b/t/
@@ -279,6 +279,13 @@ test_expect_success 'git branch -M and -C fail on detached HEAD' '
 	test_cmp expect err

+test_expect_success 'git branch -d on detached HEAD' '
+	test_when_finished git checkout main &&
+	git checkout --orphan orphan &&
+	test_must_fail git branch -d main 2>err &&
+	grep "not fully merged" err
 test_expect_success 'git branch -v -d t should work' '
 	git branch t &&
 	git rev-parse --verify refs/heads/t &&

  parent reply	other threads:[~2022-11-01 20:33 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-29  5:46 Bug in `git branch --delete main` when on other orphan branch Martin von Zweigbergk
2022-11-01 10:15 ` Jeff King
2022-11-01 15:31   ` Martin von Zweigbergk
2022-11-01 20:12   ` Taylor Blau
2022-11-01 20:14     ` Bug in `git branch --delete main` when on other orphan brancht Taylor Blau
2022-11-01 21:45       ` Jeff King
2022-11-02  0:59         ` Taylor Blau
2022-11-01 20:32     ` Taylor Blau [this message]
2022-11-02  5:27       ` [PATCH v2] branch: gracefully handle '-d' on orphan HEAD Jeff King
2022-11-04  1:26         ` Rubén Justo
2022-11-04  5:36           ` Jeff King
2022-11-06 22:22             ` Rubén Justo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).