Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 659
Tor Klingberg Avatar asked Sep 26 '16 16:09

Tor Klingberg


People also ask

How do I delete a Worktree branch?

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.

What is the purpose of git Worktree?

Git Worktrees are a feature that allow you to have a single repository with multiple checked out working branches at the same time.

How do I delete a branch git?

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.

How do I add a work tree?

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.


3 Answers

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> 
like image 81
vaheeds Avatar answered Sep 20 '22 13:09

vaheeds


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.

like image 36
rodrigo Avatar answered Sep 19 '22 13:09

rodrigo


From Git 2.17.0, you can safely run this all-in-one command

git worktree remove <path>
like image 27
Efreeto Avatar answered Sep 20 '22 13:09

Efreeto