Why does git worktree add create a branch, and can I delete it?

As the other guys answer this question, I put commands to delete the folder, delete worktree and delete branch here:

first, list all of your worktrees to double check...

$ git worktree list

then, delete the folder of the worktree

$ rm -rf path/to/worktree

after that, delete the worktree itself

$ git worktree prune

in case you have more than one worktree, the above command only prune the worktree that its path doesn't exist anymore, so don't worry!

finally, delete the branch (same branch-name as the worktree)

$ git branch -D <branch-name>

The branch is necessary because you cannot have the same branch checked out in different worktrees at the same time.

So if you do not specify a branch when adding the worktree, then git will add one automatically, based on your current branch and with the name of the worktree directory.

You may ask, why cannot I have the same branch checkout out twice? Think of what will happen to worktree A when you commit to B, if they both share the branch... the worktree A will see the commit in B as a local difference, but in reverse! just as if you did git reset --soft HEAD^... That would be quite dangerous.

BTW, that is the same reason why you cannot push to a branch of a non-bare repository that is checked out.

About your last question: can you delete the branch? Of course, that branch is in no way special. You can delete it as long as it is not checked out anywhere.