为什么git worktree add会创建一个分支,我可以删除它吗? [英] Why does git worktree add create a branch, and can I delete it?

查看:247
本文介绍了为什么git worktree add会创建一个分支,我可以删除它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用git worktree add创建了一个新的工作树.我注意到这已经在回购中创建了一个与工作树同名的新分支.这个分支是干什么的?

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?

我已经在第二个工作树中签出了另一个先前存在的分支.我可以删除git worktree add创建的分支吗?

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?

推荐答案

分支是必需的,因为您不能同时在不同的工作树中检出同一分支.

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

因此,如果您在添加工作树时未指定分支,则git将根据您当前的分支并使用工作树目录的名称自动添加一个分支.

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.

您可能会问,为什么我不能两次进行相同的分支结帐?想想当您提交给B时,如果工作树A共享分支,将会对工作树A产生什么影响……工作树A将把B中的提交视为本地差异,但是相反!就像您做了git reset --soft HEAD^ ...那将是非常危险的.

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.

这篇关于为什么git worktree add会创建一个分支,我可以删除它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆