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

I used git worktree add to create a new work line. I noticed that this created a new branch in the repo with the same name as worktree. What is this branch for?

I checked another, already existing branch in the second workbook. Is it possible to remove the branch created by git worktree add ?

+6
source share
4 answers

A branch is necessary because you cannot check the same branch at different workstations at the same time.

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

You may ask, why can't I get the same exam? Think about what will happen to worktree A when you pass B if they share a branch ... workgroup A will see the commit in B as a local difference, but vice versa! as if you did git reset --soft HEAD^ ... It would be very dangerous.

By the way, this is the same reason why you cannot click on the branch of the non-poor repository that is uploaded.

About the last question: can you delete a branch? Of course, this industry is by no means special. You can delete it until it is deleted somewhere.

+7
source

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

first, list all your work data to double check ...

 $ git worktree list 

then delete the working folder folder

 $ rm -rf ../path/to/worktree 

after that remove the worktree itself

 $ git worktree prune 

if you have more than one working group, the above command only truncates the working line, that its path no longer exists, so do not worry!

finally delete the branch (same branch name as the working line)

 $ git branch -D <branch-name> 
+4
source

git worktree --help clearly mentions this, as shown below.

 COMMANDS add <path> [<branch>] Create <path> and checkout <branch> into it. The new working directory is linked to the current repository, sharing everything except working directory specific files such as HEAD, index, etc. If <branch> is omitted and neither -b nor -B is used, then, as a convenience, a new branch based at HEAD is created automatically, as if -b $(basename <path>) was specified. prune Prune working tree information in $GIT_DIR/worktrees. 
+3
source

It seems you can work in disconnected mode with --detach , which will not create branches. This can be useful if you do not plan to make changes to the working line, but just perform, for example, running or running tests.

Source: https://stacktoheap.com/blog/2016/01/19/using-multiple-worktrees-with-git/#long-running-tasks

+2
source

All Articles