为什么 git worktree add 创建一个分支,我可以删除它吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39707402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:18:51  来源:igfitidea点击:

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

gitgit-worktree

提问by Tor Klingberg

I used git worktree addto 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?

我曾经git worktree add创建一个新的工作树。我注意到它在 repo 中创建了一个与工作树同名的新分支。这个分支有什么用?

I have checked out an other, pre-existing branch in the second worktree. Am I free to delete the branch that git worktree addcreated?

我已经检查了第二个工作树中的另一个预先存在的分支。我可以删除git worktree add创建的分支吗?

采纳答案by rodrigo

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.

因此,如果您在添加工作树时没有指定分支,那么 git 将根据您当前的分支和工作树目录的名称自动添加一个。

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.

你可能会问,为什么我不能在同一个分行结账两次?想想当你提交到 B 时工作树 A 会发生什么,如果它们都共享分支……工作树 A 会将 B 中的提交视为局部差异,但反过来!就好像你这样做了git reset --soft HEAD^……那会很危险。

BTW, that is the same reason why you cannot push toa 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.

关于你的最后一个问题:你可以删除分支吗?当然,那个分支并没有什么特别之处。只要未在任何地方签出,您就可以将其删除。

回答by vaheeds

As the other guys answer this question, I put commands to delete the folder, delete worktreeand delete branchhere:

当其他人回答这个问题时,我在这里放置了删除folder、删除worktree和删除的命令branch

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>

回答by Indra Uprade

git worktree --help clearly mentions this as below.

git worktree --help 清楚地提到了这一点,如下所示。

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.

回答by VonC

git worktreewill add a new branch if none are specified:

git worktree如果没有指定,将添加一个新分支:

If <commit-ish>is omitted and neither -bnor -Bnor --detachused, then, as a convenience, a new branch based at HEAD is created automatically, as if -b $(basename <path>)was specified.

如果<commit-ish>省略被既不-b也不-B也不--detach使用,那么,作为一种方便,将自动创建基于在HEAD一个新的分支,就好像-b $(basename <path>)被指定。

Since Git 2.17, you can delete that branch with git worktree remove.

从 Git 2.17 开始,您可以使用git worktree remove.

But, that same removecommand also included:

但是,同样的remove命令还包括:

Unclean working trees or ones with submodules can be removed with --force.
The main working tree cannot be removed.

不干净的工作树或带有子模块的工作树可以用--force.
无法删除主工作树。

True... except --forcewas not fullyimplemented in Git 2.17.

是的……除了在 Git 2.17 中--force没有完全实现。

With Git 2.18 (Q2 2018), "git worktree remove" learned that "-f" is a shorthand for "--force" option, just like for "git worktree add".

在 Git 2.18(2018 年第二季度)中,“ git worktree remove”了解到“ -f”是“ --force”选项的简写,就像“git worktree add”一样。

See commit d228eea(17 Apr 2018) by Stefan Beller (stefanbeller).
Helped-by: Eric Sunshine (sunshineco).
(Merged by Junio C Hamano -- gitster--in commit 90186fa, 08 May 2018)

请参阅Stefan Beller ( ) 的commit d228eea(2018 年 4 月 17 日。 帮助者:Eric Sunshine ( )(由Junio C Hamano合并-- --commit 90186fa,2018 年 5 月 8 日)stefanbeller
sunshineco
gitster

worktree: accept -fas short for --forcefor removal

Many commands support a "--force" option, frequently abbreviated as "-f".
However, "git worktree remove"'s hand-rolled OPT_BOOLforgets to recognize the short form, despite git-worktree.txtdocumenting "-f" as supported.
Replace OPT_BOOLwith OPT__FORCE, which provides "-f" for free, and makes 'remove' consistent with 'add' option parsing (which also specifies the PARSE_OPT_NOCOMPLETEflag).

worktree:接受-f短的--force去除

许多命令支持“ --force”选项,通常缩写为“ -f”。
然而,尽管将“ ”记录为支持,“ git worktree remove”的手卷OPT_BOOL忘记识别短格式。 替换为,它免费提供“ ”,并使 ' ' 与 ' ' 选项解析(也指定标志)一致。git-worktree.txt-f
OPT_BOOLOPT__FORCE-fremoveaddPARSE_OPT_NOCOMPLETE

回答by Efreeto

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

从 Git 2.17.0 开始,您可以安全地运行此多合一命令

git worktree remove <path>

回答by liberforce

Seems you can run in detached mode with --detach, which won't create branches. This may be useful if you don't plan to do modifications in the worktree, but just run build or run tests for example.

似乎您可以使用 以分离模式运行--detach,这不会创建分支。如果您不打算在工作树中进行修改,而只是运行构建或运行测试,这可能很有用。

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

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