threads / bug / 61218

bug report: spurious "cannot delete branch '%s' used by worktree"

Subject: bug report: spurious "cannot delete branch '%s' used by worktree"

## tl;dr

11 messages between Mar 28, 2024 and Apr 2, 2024.

replies: 10people: 4as markdown or json

Tamir Duberstein· Mar 28, 2024, 14:53 UTC · lore
Pasting the output of `git bugreport`:

Thank you for filling out a Git bug report! Please answer the following questions to help us understand your issue.

What did you do before the bug happened? (Steps to reproduce your issue)

I created a branch, pushed to a remote, and then the remote deleted the branch. Then I tried to delete the local branch.

What did you expect to happen? (Expected behavior)
The branch to be deleted.
What happened instead? (Actual behavior)
% git branch -d cleanup
error: cannot delete branch 'cleanup' used by worktree at '<my source dir>'
% git worktree list
<my source dir>  dc46f6d5e [main]
% git branch
  cleanup
* main
What's different between what you expected and what actually happened?

Git reports the branch is being used by a worktree, but prints the current repository's directory, where a different branch is clearly checked out.

Anything else you want to add:

Please review the rest of the bug report below. You can delete any lines you don't wish to share.

[System Info] git version: git version 2.44.0 cpu: arm64 no commit associated with this build sizeof-long: 8 sizeof-size_t: 8 shell-path: /bin/sh feature: fsmonitor--daemon uname: Darwin 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:25 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6030 arm64 compiler info: clang: 15.0.0 (clang-1500.1.0.2.5) libc info: no libc information available $SHELL (typically, interactive shell): /bin/zsh

[Enabled Hooks]
Eric Sunshine· Mar 28, 2024, 17:24 UTC · re: Tamir Duberstein · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

On Thu, Mar 28, 2024 at 10:54 AM Tamir Duberstein <tamird@fuseenergy.com> wrote:
Show 7 quoted lines
> % git branch -d cleanup
> error: cannot delete branch 'cleanup' used by worktree at '<my source dir>'
> % git worktree list
> <my source dir>  dc46f6d5e [main]
> % git branch
>   cleanup
> * main

Is this error persistent once it arises? That is, if you invoke `git branch -d cleanup` again immediately after (or a little while after) the above sequence, does the problem persist? Or does it "clear up" on its own at some point?

Tamir Duberstein· Mar 28, 2024, 17:39 UTC · re: Eric Sunshine · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

Yes, the problem is persistent. The branch is never deleted.
On Thu, Mar 28, 2024 at 5:24 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
Show 14 quoted lines
>
> On Thu, Mar 28, 2024 at 10:54 AM Tamir Duberstein <tamird@fuseenergy.com> wrote:
> > % git branch -d cleanup
> > error: cannot delete branch 'cleanup' used by worktree at '<my source dir>'
> > % git worktree list
> > <my source dir>  dc46f6d5e [main]
> > % git branch
> >   cleanup
> > * main
>
> Is this error persistent once it arises? That is, if you invoke `git
> branch -d cleanup` again immediately after (or a little while after)
> the above sequence, does the problem persist? Or does it "clear up" on
> its own at some point?
Eric Sunshine· Mar 31, 2024, 06:49 UTC · re: Tamir Duberstein · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

[please reply inline rather than top-posting; I've moved your reply inline for this response]

On Thu, Mar 28, 2024 at 1:40 PM Tamir Duberstein <tamird@fuseenergy.com> wrote:
Show 16 quoted lines
> On Thu, Mar 28, 2024 at 5:24 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
> > On Thu, Mar 28, 2024 at 10:54 AM Tamir Duberstein <tamird@fuseenergy.com> wrote:
> > > % git branch -d cleanup
> > > error: cannot delete branch 'cleanup' used by worktree at '<my source dir>'
> > > % git worktree list
> > > <my source dir>  dc46f6d5e [main]
> > > % git branch
> > >   cleanup
> > > * main
> >
> > Is this error persistent once it arises? That is, if you invoke `git
> > branch -d cleanup` again immediately after (or a little while after)
> > the above sequence, does the problem persist? Or does it "clear up" on
> > its own at some point?
>
> Yes, the problem is persistent. The branch is never deleted.

I'd guess that there may be some sort of "ref" still pointing at the "cleanup" branch which presumably was, at some point, checked out at "<my source dir>". Digging through the code[1,2,3] suggests that you might have some stale state from a rebase, bisect, or other sequencer operation which still references the "cleanup" branch.

[Cc'ing Phillip who is probably much more familiar with this code than am I.]

By the way, it's not clear from your initial report what you mean when you say "then the remote deleted the branch". Also, did you fetch and/or pull from the remote after that?

[1]: https://github.com/git/git/blob/d6fd04375f91/branch.c#L454 [2]: https://github.com/git/git/blob/d6fd04375f91/branch.c#L386 [3]: https://github.com/git/git/blob/d6fd04375f91/sequencer.c#L6551

Tamir Duberstein· Mar 31, 2024, 07:45 UTC · re: Eric Sunshine · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

On Sun, Mar 31, 2024 at 7:49 AM Eric Sunshine <sunshine@sunshineco.com> wrote:
Show 37 quoted lines
>
> [please reply inline rather than top-posting; I've moved your reply
> inline for this response]
>
> On Thu, Mar 28, 2024 at 1:40 PM Tamir Duberstein <tamird@fuseenergy.com> wrote:
> > On Thu, Mar 28, 2024 at 5:24 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
> > > On Thu, Mar 28, 2024 at 10:54 AM Tamir Duberstein <tamird@fuseenergy.com> wrote:
> > > > % git branch -d cleanup
> > > > error: cannot delete branch 'cleanup' used by worktree at '<my source dir>'
> > > > % git worktree list
> > > > <my source dir>  dc46f6d5e [main]
> > > > % git branch
> > > >   cleanup
> > > > * main
> > >
> > > Is this error persistent once it arises? That is, if you invoke `git
> > > branch -d cleanup` again immediately after (or a little while after)
> > > the above sequence, does the problem persist? Or does it "clear up" on
> > > its own at some point?
> >
> > Yes, the problem is persistent. The branch is never deleted.
>
> I'd guess that there may be some sort of "ref" still pointing at the
> "cleanup" branch which presumably was, at some point, checked out at
> "<my source dir>". Digging through the code[1,2,3] suggests that you
> might have some stale state from a rebase, bisect, or other sequencer
> operation which still references the "cleanup" branch.
>
> [Cc'ing Phillip who is probably much more familiar with this code than am I.]
>
> By the way, it's not clear from your initial report what you mean when
> you say "then the remote deleted the branch". Also, did you fetch
> and/or pull from the remote after that?
>
> [1]: https://github.com/git/git/blob/d6fd04375f91/branch.c#L454
> [2]: https://github.com/git/git/blob/d6fd04375f91/branch.c#L386
> [3]: https://github.com/git/git/blob/d6fd04375f91/sequencer.c#L6551
Thanks, I wasn't aware of the etiquette.

I had used the branch to send a pull request on GitHub, and then deleted the remote branch from GitHub after merging the pull request. Yes, I have fetched from the remote following that. I've also manually removed the remote tracking branch. Symptoms remain unchanged.

If you can suggest where to go looking for stale state, I'm happy to provide more information. I'll take a look at the code references as well.

Phillip Wood· Apr 2, 2024, 10:10 UTC · re: Eric Sunshine · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

Hi Tamir
On 31/03/2024 07:49, Eric Sunshine wrote:
Show 28 quoted lines
> [please reply inline rather than top-posting; I've moved your reply
> inline for this response]
> 
> On Thu, Mar 28, 2024 at 1:40 PM Tamir Duberstein <tamird@fuseenergy.com> wrote:
>> On Thu, Mar 28, 2024 at 5:24 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
>>> On Thu, Mar 28, 2024 at 10:54 AM Tamir Duberstein <tamird@fuseenergy.com> wrote:
>>>> % git branch -d cleanup
>>>> error: cannot delete branch 'cleanup' used by worktree at '<my source dir>'
>>>> % git worktree list
>>>> <my source dir>  dc46f6d5e [main]
>>>> % git branch
>>>>    cleanup
>>>> * main
>>>
>>> Is this error persistent once it arises? That is, if you invoke `git
>>> branch -d cleanup` again immediately after (or a little while after)
>>> the above sequence, does the problem persist? Or does it "clear up" on
>>> its own at some point?
>>
>> Yes, the problem is persistent. The branch is never deleted.
> 
> I'd guess that there may be some sort of "ref" still pointing at the
> "cleanup" branch which presumably was, at some point, checked out at
> "<my source dir>". Digging through the code[1,2,3] suggests that you
> might have some stale state from a rebase, bisect, or other sequencer
> operation which still references the "cleanup" branch.
> 
> [Cc'ing Phillip who is probably much more familiar with this code than am I.]

Thanks Eric. I'd have thought that "git worktree list" would say something about the branch being rebased if there was enough state lying around to prevent the branch being deleted, but lets see. What does

     ls $(git rev-parse --git-path rebase-merge) $(git rev-parse 
--git-path rebase-apply)

show when you run it in <my source dir>? Also is <my source dir> the only worktree?

Best Wishes
Phillip
Show 7 quoted lines
> By the way, it's not clear from your initial report what you mean when
> you say "then the remote deleted the branch". Also, did you fetch
> and/or pull from the remote after that?
> 
> [1]: https://github.com/git/git/blob/d6fd04375f91/branch.c#L454
> [2]: https://github.com/git/git/blob/d6fd04375f91/branch.c#L386
> [3]: https://github.com/git/git/blob/d6fd04375f91/sequencer.c#L6551
Tamir Duberstein· Apr 2, 2024, 10:26 UTC · re: Phillip Wood · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

Hi Phillip
On Tue, Apr 2, 2024 at 11:10 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
Show 42 quoted lines
>
> Hi Tamir
>
> On 31/03/2024 07:49, Eric Sunshine wrote:
> > [please reply inline rather than top-posting; I've moved your reply
> > inline for this response]
> >
> > On Thu, Mar 28, 2024 at 1:40 PM Tamir Duberstein <tamird@fuseenergy.com> wrote:
> >> On Thu, Mar 28, 2024 at 5:24 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
> >>> On Thu, Mar 28, 2024 at 10:54 AM Tamir Duberstein <tamird@fuseenergy.com> wrote:
> >>>> % git branch -d cleanup
> >>>> error: cannot delete branch 'cleanup' used by worktree at '<my source dir>'
> >>>> % git worktree list
> >>>> <my source dir>  dc46f6d5e [main]
> >>>> % git branch
> >>>>    cleanup
> >>>> * main
> >>>
> >>> Is this error persistent once it arises? That is, if you invoke `git
> >>> branch -d cleanup` again immediately after (or a little while after)
> >>> the above sequence, does the problem persist? Or does it "clear up" on
> >>> its own at some point?
> >>
> >> Yes, the problem is persistent. The branch is never deleted.
> >
> > I'd guess that there may be some sort of "ref" still pointing at the
> > "cleanup" branch which presumably was, at some point, checked out at
> > "<my source dir>". Digging through the code[1,2,3] suggests that you
> > might have some stale state from a rebase, bisect, or other sequencer
> > operation which still references the "cleanup" branch.
> >
> > [Cc'ing Phillip who is probably much more familiar with this code than am I.]
>
> Thanks Eric. I'd have thought that "git worktree list" would say
> something about the branch being rebased if there was enough state lying
> around to prevent the branch being deleted, but lets see. What does
>
>      ls $(git rev-parse --git-path rebase-merge) $(git rev-parse
> --git-path rebase-apply)
>
> show when you run it in <my source dir>? Also is <my source dir> the
> only worktree?

% ls $(git rev-parse --git-path rebase-merge) $(git rev-parse --git-path rebase-apply) ls: .git/rebase-apply: No such file or directory ls: .git/rebase-merge: No such file or directory

Yes, it's the only worktree.
Show 11 quoted lines
> Best Wishes
>
> Phillip
>
> > By the way, it's not clear from your initial report what you mean when
> > you say "then the remote deleted the branch". Also, did you fetch
> > and/or pull from the remote after that?
> >
> > [1]: https://github.com/git/git/blob/d6fd04375f91/branch.c#L454
> > [2]: https://github.com/git/git/blob/d6fd04375f91/branch.c#L386
> > [3]: https://github.com/git/git/blob/d6fd04375f91/sequencer.c#L6551
Kristoffer Haugsbakk· Apr 2, 2024, 10:38 UTC · re: Tamir Duberstein · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

On Tue, Apr 2, 2024, at 12:26, Tamir Duberstein wrote:
Show 7 quoted lines
>
> % ls $(git rev-parse --git-path rebase-merge) $(git rev-parse
> --git-path rebase-apply)
> ls: .git/rebase-apply: No such file or directory
> ls: .git/rebase-merge: No such file or directory
>
> Yes, it's the only worktree.
Have you tried `git worktree repair`?
-- 
Kristoffer Haugsbakk
phillip.wood123@gmail.com· Apr 2, 2024, 11:28 UTC · re: Tamir Duberstein · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

Hi Tamir
On 02/04/2024 11:26, Tamir Duberstein wrote:
Show 16 quoted lines
> On Tue, Apr 2, 2024 at 11:10 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
>> On 31/03/2024 07:49, Eric Sunshine wrote:
>> Thanks Eric. I'd have thought that "git worktree list" would say
>> something about the branch being rebased if there was enough state lying
>> around to prevent the branch being deleted, but lets see. What does
>>
>>       ls $(git rev-parse --git-path rebase-merge) $(git rev-parse
>> --git-path rebase-apply)
>>
>> show when you run it in <my source dir>? Also is <my source dir> the
>> only worktree?
> 
> % ls $(git rev-parse --git-path rebase-merge) $(git rev-parse
> --git-path rebase-apply)
> ls: .git/rebase-apply: No such file or directory
> ls: .git/rebase-merge: No such file or directory

Thanks for trying that - at least we know it isn't a problem with rebase. Lets check if there is a stale bisect (sorry I forgot about that earlier). What do

     cat .git/BISECT_START
     ls .git | grep -i bisect
show?
> Yes, it's the only worktree.
In that case can you check that
     ls .git/worktrees
fails because the directory is missing or shows an empty directory please.
Thanks
Phillip
Tamir Duberstein· Apr 2, 2024, 11:56 UTC · re: phillip.wood123@gmail.com · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

On Tue, Apr 2, 2024 at 12:29 PM <phillip.wood123@gmail.com> wrote:
Show 29 quoted lines
>
> Hi Tamir
>
> On 02/04/2024 11:26, Tamir Duberstein wrote:
> > On Tue, Apr 2, 2024 at 11:10 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
> >> On 31/03/2024 07:49, Eric Sunshine wrote:
> >> Thanks Eric. I'd have thought that "git worktree list" would say
> >> something about the branch being rebased if there was enough state lying
> >> around to prevent the branch being deleted, but lets see. What does
> >>
> >>       ls $(git rev-parse --git-path rebase-merge) $(git rev-parse
> >> --git-path rebase-apply)
> >>
> >> show when you run it in <my source dir>? Also is <my source dir> the
> >> only worktree?
> >
> > % ls $(git rev-parse --git-path rebase-merge) $(git rev-parse
> > --git-path rebase-apply)
> > ls: .git/rebase-apply: No such file or directory
> > ls: .git/rebase-merge: No such file or directory
>
> Thanks for trying that - at least we know it isn't a problem with
> rebase. Lets check if there is a stale bisect (sorry I forgot about that
> earlier). What do
>
>      cat .git/BISECT_START
>      ls .git | grep -i bisect
>
> show?

This was the culprit! There was indeed a ` .git/BISECT_START` containing "cleanup". After deleting this file I am able to remove the branch.

Show 6 quoted lines
> > Yes, it's the only worktree.
> In that case can you check that
>
>      ls .git/worktrees
>
> fails because the directory is missing or shows an empty directory please.
% cat .git/worktrees
cat: .git/worktrees: No such file or directory
> Thanks
>
> Phillip
I guess the only action item would be to improve the error message.
phillip.wood123@gmail.com· Apr 2, 2024, 15:15 UTC · re: Tamir Duberstein · lore

Re: bug report: spurious "cannot delete branch '%s' used by worktree"

On 02/04/2024 12:56, Tamir Duberstein wrote:
[cc'ing Christian who knows more about "git bisect" than me]
Show 16 quoted lines
> On Tue, Apr 2, 2024 at 12:29 PM <phillip.wood123@gmail.com> wrote:
>> On 02/04/2024 11:26, Tamir Duberstein wrote:
>>> On Tue, Apr 2, 2024 at 11:10 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
>>>> On 31/03/2024 07:49, Eric Sunshine wrote:
>> Thanks for trying that - at least we know it isn't a problem with
>> rebase. Lets check if there is a stale bisect (sorry I forgot about that
>> earlier). What do
>>
>>       cat .git/BISECT_START
>>       ls .git | grep -i bisect
>>
>> show?
> 
> This was the culprit! There was indeed a ` .git/BISECT_START`
> containing "cleanup". After deleting this file I am able to remove the
> branch.
Oh good I'm glad we've found the problem.
> I guess the only action item would be to improve the error message.

Yes, there was some discussion about that relatively recently when the message was changed to say "in use" rather than "checked out". Adding the appropriate command name into the message would help.

When bisecting "git switch" prints a warning but still switches branches, "git checkout" performs the checkout with no warning. I wonder if (a) "git switch" should refuse to run without "--detach" when there is a bisection in progress and (b) if "git bisect" could be more helpful when it finds the broken commit by telling the user that they should run "git bisect reset" to clean up.

Best Wishes
Phillip

← back to recent threads