git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Bug in `git branch --delete main` when on other orphan branch

From
Jeff King <peff@peff.net>
Date
Nov 1, 2022, 10:15 UTC
Message-ID
<Y2DxxZAFbN8juHY6@coredump.intra.peff.net>
In-Reply-To
<CAESOdVBpsbJ0obD=qDjHBJg-wwWUL5sQ-7X_h13Vw39Q9QUzHA@mail.gmail.com>
On Fri, Oct 28, 2022 at 10:46:37PM -0700, Martin von Zweigbergk wrote:
Show 14 quoted lines
> I did this:
> git init test
> cd test
> echo a > file
> git add file
> git commit -m a
> git checkout --orphan other
> git branch --delete main
> 
> The last command fails with:
> fatal: Couldn't look up commit object for HEAD
> 
> That's a bug, right? I can of course work around it with `rm
> .git/refs/heads/main`.

Sort of. This is part of the "is the thing we are deleting merged into HEAD" check. It tries to look up the HEAD and calls die() when it can't. The more correct thing, I think, would be for it to just return "nope, there is no HEAD so nothing is merged into it".

But that probably won't make your command succeed; you'll just get:
  error: The branch 'main' is not fully merged.

At which point you'd retry with "-f" (or "-D"). And then it succeeds, because the force path is smart enough to skip loading HEAD, from 67affd5173 (git-branch -D: make it work even when on a yet-to-be-born branch, 2006-11-24).

At the time, I suspect that logic was "good enough". You'd need "-f" either way, so it is really just a question of producing a lousy error message.

But since then, I think there are more cases. For example, 99c419c915 (branch -d: base the "already-merged" safety on the branch it merges with, 2009-12-29) makes it OK to delete the branch if it's merged to HEAD _or_ to its upstream. You don't have an upstream in your example, but it's not hard to imagine one (just start the repo via "clone" rather than from scratch).

And in that case I think the HEAD check calling die() is actively doing the wrong thing, and would prevent an otherwise successful deletion.

The fix might be as simple as:
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;

as the later code seems to do the right thing with the NULL head_rev. It
would definitely need more careful investigation (and tests!) to confirm
that, though.

And in the meantime, hopefully you noticed that "-f" is a better
workaround than manually deleting the refs file. :)

-Peff
Previous: Martin von ZweigbergkNext: Martin von Zweigbergk
Message 2 of 12 in “Bug in `git branch --delete main` when on other orphan branch”
  1. Martin von ZweigbergkOct 29, 2022
  2. Jeff KingNov 1, 2022
  3. Martin von ZweigbergkNov 1, 2022
  4. Taylor BlauNov 1, 2022
  5. Taylor BlauNov 1, 2022
  6. Jeff KingNov 1, 2022
  7. Taylor BlauNov 2, 2022
  8. branch: gracefully handle '-d' on detached HEADTaylor Blau, Nov 1, 2022
  9. branch: gracefully handle '-d' on orphan HEADJeff King, Nov 2, 2022
  10. Rubén JustoNov 4, 2022
  11. Jeff KingNov 4, 2022
  12. Rubén JustoNov 6, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.