Worktree branches in git show unmerged even after merging

232 views Asked by At

I often use git add worktree wtname to create separate branches. When I merge the changes on the branch wtname to the master, I expect that git branch --no-merged should not list wtname branch. However, it does. Worse, even after removing the worktree:

rm -rf wtname
git worktree remove wtname

I can't delete this branch unless I force delete it. After the above written commands, if I run git branch -d wtname, it throws the following error:

error: The branch 'wtname' is not fully merged.
If you are sure you want to delete it, run 'git branch -D wtname'.

Why is this the case?

2

There are 2 answers

0
jthill On

When I merge the changes on the branch wtname to the master

That tops my suspect list. When you let git worktree add default-create a branch, it doesn't track anything. When you git branch -d a branch, if it doesn't track anything, its safety check is against your current checkout.

0
matt On

You have not shown us your real code, but one counterexample is sufficient to disprove the hypothesis, so here is a little playlet in which we create a new branch in a worktree, work on that branch, and merge it — and then remove the worktree and the branch. Whatever you are doing different from this, to the extent of the difference, is likely wrong. Perhaps you are creating the worktree incorrectly? Watch carefully what I do:

$ mkdir git
$ cd git
$ git init

$ echo a > a.txt; git add .; git commit -m'a'
$ echo b > a.txt; git add .; git commit -m'b'

$ git worktree add -b br ../br
$ cd ../br

$ echo c > a.txt; git add .; git commit -m'c'
$ echo d > a.txt; git add .; git commit -m'd'
$ cd ../git

$ echo e > e.txt; git add .; git commit -m'e'
$ git merge br

$ git branch --no-merged # nothing
$ git worktree remove br
$ git branch -d br