0

I cannot delete local worktree branch that is gone. The keywords are local, worktree branch, and gone. I.e., the question Cannot delete a Branch that doesn't exist? doesn't match with mine.

It happens when I tried to delete the worktree folder before calling git branch -d. Observe:

MINGW64 /d/Tmp
$ mkdir repoA

MINGW64 /d/Tmp
$ cd repoA

MINGW64 /d/Tmp/repoA
$ git init
Initialized empty Git repository in D:/Tmp/repoA/.git/

MINGW64 /d/Tmp/repoA (master)
$ echo a > a

MINGW64 /d/Tmp/repoA (master)
$ git add .

MINGW64 /d/Tmp/repoA (master)
$ git commit -a -m'added a'
[master (root-commit) 2cff3c3] added a
 1 file changed, 1 insertion(+)
 create mode 100644 a

MINGW64 /d/Tmp/repoA (master)
$ git worktree add -b branch-b ../repoB master
Preparing worktree (new branch 'branch-b')
HEAD is now at 2cff3c3 added a

MINGW64 /d/Tmp/repoA (master)
$ rm -rf ../repoB

MINGW64 /d/Tmp/repoA (master)
$ git branch -d branch-b
error: Cannot delete branch 'branch-b' checked out at 'D:/Tmp/repoB'

MINGW64 /d/Tmp/repoA (master)
$ git branch -d branch-b -f
error: Cannot delete branch 'branch-b' checked out at 'D:/Tmp/repoB'

MINGW64 /d/Tmp/repoA (master)
$ git --version
git version 2.19.1.windows.1

enter image description here

UPDATE:

git worktree prune can remove the worktree, but the branch-b is still there:

$ git worktree list
D:/Tmp/repoA  2cff3c3 [master]
D:/Tmp/repoB  2cff3c3 [branch-b]

$ git worktree prune

$ git worktree list
D:/Tmp/repoA  2cff3c3 [master]

$ git branch -vv
  branch-b 2cff3c3 added a
* master   2cff3c3 added a

How to remove the branch-b that I don't want any more?

xpt
  • 20,363
  • 37
  • 127
  • 216
  • You are deleting the directory behind git's back but for git the branch is still checked out. What would you expect it to do? I guess you have to run `git rm repoB` so that it works as expected. – eftshift0 May 23 '19 at 13:55
  • https://stackoverflow.com/search?q=%5Bgit-worktree%5D+delete – phd May 23 '19 at 14:03
  • `git worktree prune` – phd May 23 '19 at 14:04
  • @phd, thanks! that works. would you like to answer, or I delete this? – xpt May 23 '19 at 14:05
  • I think the answer at the linked answer is good enough. As for delete — it's up to you. – phd May 23 '19 at 14:06
  • Ok. I'll leave it as-is as another entry for people to find the answer. Thx again. – xpt May 23 '19 at 14:14
  • @phd, new update added to OP. that "[Possible duplicate](https://stackoverflow.com/questions/41545293/branch-is-already-checked-out-at-other-location-in-git-worktrees)" doesn't work for my question, please remove. thx. – xpt May 23 '19 at 14:49
  • `git branch -D branch-b` – phd May 23 '19 at 16:02
  • Ah, gotya, thanks @phd! – xpt May 23 '19 at 18:03

0 Answers0