I used git worktree add
to create a new worktree. I noticed that is has created a new branch in the repo with the same name as the worktree. What is this branch for?
I have checked out an other, pre-existing branch in the second worktree. Am I free to delete the branch that git worktree add
created?
To remove a locked worktree, specify --force twice. With add , create a new branch named <new-branch> starting at <commit-ish> , and check out <new-branch> into the new worktree. If <commit-ish> is omitted, it defaults to HEAD . By default, -b refuses to create a new branch if it already exists.
Git Worktrees are a feature that allow you to have a single repository with multiple checked out working branches at the same time.
Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.
add worktree.Add a config option worktree. guessRemote that allows users to configure the default behaviour for themselves. With add , if no branch argument, and neither of -b nor -B nor --detach are given, the command defaults to creating a new branch from HEAD. If worktree.
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.
From Git 2.17.0, you can safely run this all-in-one command
git worktree remove <path>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With