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

11 messages from 2024-03-28 to 2024-04-02. Participants: Tamir Duberstein, Eric Sunshine, Phillip Wood, Kristoffer Haugsbakk, phillip.wood123@gmail.com.
Thread: https://gitlist.dev/t/61218

## Tamir Duberstein, 2024-03-28 14:53

Subject: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <CAEYvaskGHYrQgke=gf1sXYhrwbd+SeTpcjGF0fpxK3hQbyPFKg@mail.gmail.com>
URL: https://gitlist.dev/e/CAEYvaskGHYrQgke%3Dgf1sXYhrwbd%2BSeTpcjGF0fpxK3hQbyPFKg%40mail.gmail.com

```
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, 2024-03-28 17:24

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <CAPig+cQWW1sLXyTBvk6D+1h15sZCtQO1opfhtFfiHr_kX0y82g@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQWW1sLXyTBvk6D%2B1h15sZCtQO1opfhtFfiHr_kX0y82g%40mail.gmail.com
In-Reply-To: <CAEYvaskGHYrQgke=gf1sXYhrwbd+SeTpcjGF0fpxK3hQbyPFKg@mail.gmail.com>

```
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?

```

## Tamir Duberstein, 2024-03-28 17:39

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <CAEYvaskXRyxNTLNeRPPyawFrBVRgCbSnJiuF7D7cOGiaDq=V2Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAEYvaskXRyxNTLNeRPPyawFrBVRgCbSnJiuF7D7cOGiaDq%3DV2Q%40mail.gmail.com
In-Reply-To: <CAPig+cQWW1sLXyTBvk6D+1h15sZCtQO1opfhtFfiHr_kX0y82g@mail.gmail.com>

```
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:
>
> 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, 2024-03-31 06:49

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <CAPig+cQdFi5zBkDQWTEWXCTt5h9gVFNJv7obf=tWCkOvsaEHEA@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQdFi5zBkDQWTEWXCTt5h9gVFNJv7obf%3DtWCkOvsaEHEA%40mail.gmail.com
In-Reply-To: <CAEYvaskXRyxNTLNeRPPyawFrBVRgCbSnJiuF7D7cOGiaDq=V2Q@mail.gmail.com>

```
[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

```

## Tamir Duberstein, 2024-03-31 07:45

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <CAEYvas=baW9DWQY4_MJ2-Hh0=yYwH2tGeOOwkcJr3wETCKpy4w@mail.gmail.com>
URL: https://gitlist.dev/e/CAEYvas%3DbaW9DWQY4_MJ2-Hh0%3DyYwH2tGeOOwkcJr3wETCKpy4w%40mail.gmail.com
In-Reply-To: <CAPig+cQdFi5zBkDQWTEWXCTt5h9gVFNJv7obf=tWCkOvsaEHEA@mail.gmail.com>

```
On Sun, Mar 31, 2024 at 7:49 AM Eric Sunshine <sunshine@sunshineco.com> 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.]
>
> 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, 2024-04-02 10:10

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <4041487e-d8d8-481c-b490-884e31f533a8@gmail.com>
URL: https://gitlist.dev/e/4041487e-d8d8-481c-b490-884e31f533a8%40gmail.com
In-Reply-To: <CAPig+cQdFi5zBkDQWTEWXCTt5h9gVFNJv7obf=tWCkOvsaEHEA@mail.gmail.com>

```
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?

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

```

## Tamir Duberstein, 2024-04-02 10:26

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <CAEYvasmb1TjwWpSbfSAogbOiB64sZQiHVoUhxvY+NoLmXnRuHA@mail.gmail.com>
URL: https://gitlist.dev/e/CAEYvasmb1TjwWpSbfSAogbOiB64sZQiHVoUhxvY%2BNoLmXnRuHA%40mail.gmail.com
In-Reply-To: <4041487e-d8d8-481c-b490-884e31f533a8@gmail.com>

```
Hi Phillip

On Tue, Apr 2, 2024 at 11:10 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
>
> 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.

> 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, 2024-04-02 10:38

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <b331feb8-6af3-4361-9822-31c7440e65f8@app.fastmail.com>
URL: https://gitlist.dev/e/b331feb8-6af3-4361-9822-31c7440e65f8%40app.fastmail.com
In-Reply-To: <CAEYvasmb1TjwWpSbfSAogbOiB64sZQiHVoUhxvY+NoLmXnRuHA@mail.gmail.com>

```
On Tue, Apr 2, 2024, at 12:26, Tamir Duberstein wrote:
>
> % 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, 2024-04-02 11:28

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <740d1f1a-40ce-4714-91bd-1ba448e9d672@gmail.com>
URL: https://gitlist.dev/e/740d1f1a-40ce-4714-91bd-1ba448e9d672%40gmail.com
In-Reply-To: <CAEYvasmb1TjwWpSbfSAogbOiB64sZQiHVoUhxvY+NoLmXnRuHA@mail.gmail.com>

```
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?

> 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, 2024-04-02 11:56

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <CAEYvaskG0BBVr+nsQ+iof=AXu3dcQHx0aRRrKRrf8e_NFANL9A@mail.gmail.com>
URL: https://gitlist.dev/e/CAEYvaskG0BBVr%2BnsQ%2Biof%3DAXu3dcQHx0aRRrKRrf8e_NFANL9A%40mail.gmail.com
In-Reply-To: <740d1f1a-40ce-4714-91bd-1ba448e9d672@gmail.com>

```
On Tue, Apr 2, 2024 at 12:29 PM <phillip.wood123@gmail.com> wrote:
>
> 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.

> > 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, 2024-04-02 15:15

Subject: Re: bug report: spurious "cannot delete branch '%s' used by worktree"
Message-ID: <566bc6ac-d4d6-45e2-9875-575e187280a7@gmail.com>
URL: https://gitlist.dev/e/566bc6ac-d4d6-45e2-9875-575e187280a7%40gmail.com
In-Reply-To: <CAEYvaskG0BBVr+nsQ+iof=AXu3dcQHx0aRRrKRrf8e_NFANL9A@mail.gmail.com>

```
On 02/04/2024 12:56, Tamir Duberstein wrote:

[cc'ing Christian who knows more about "git bisect" than me]

> 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

```
