问题描述
在我的远程存储库中,有3个分支(主分支和2个长期运行的分支):
In my remote repository there are 3 branches (master and 2 long running branches):
master #the common features are here like Core, DAL,...
north #customized for A company (long-running)
razavi #customized for B company (long-running)
在我的办公室PC上,为那些north
和razavi
分支添加2个worktree
:
At my office PC, I add 2 worktree
for those north
and razavi
branches:
$ git worktree list
C:/Source/nis a6fb6e1 [master]
C:/Source/north ebc7670 [north]
C:/Source/razavi eed08a2 [razavi]
到目前为止,一切都很好,我决定也从家里进行此项目,但是在我的家用PC上,当我尝试为这两个分支添加工作树时,它给了我一个错误:
Everything is OK so far, I decide to work on this project from my home as well, but in my home PC, when I try to add worktree for those two branches, it gives me an error:
$git worktree add -b north ../north north
fatal: A branch named 'north' already exists.
我删除了-b
开关,以不添加新分支,但这也行不通.
I remove the -b
switch to not add a new branch, but it doesn't work too.
如何从现有分支(不是本地而是远程)添加worktree
?
How can I add a worktree
from existing branch that is not local but remote?
推荐答案
TL; DR:您可能想要git worktree add ../north north
TL;DR: you probably wanted git worktree add ../north north
首先,提醒您(或其他有关此问题的信息):git worktree add
想要创建一个新的工作树和,同时确保该新工作树-树正在使用与其他每个工作树不同的分支名称.这是因为,尽管每个添加的工作树都有其自己的索引和HEAD
,但HEAD
文件在共享存储库中结束了 sharing 的基础分支指针.具有两个具有独立索引对象但相同的基础分支的不同工作树会导致一些棘手的问题供用户处理. git worktree
而不是试图通过教育程序员或提供解决问题的方法来弄清楚如何解决这些问题,而只是完全禁止这种情况.
First, a reminder (or information for others coming across this question): git worktree add
wants to create a new work-tree and, at the same time, make sure that this new work-tree is using a different branch name from every other work-tree. This is because, while each added work-tree has its own index and HEAD
, the HEAD
files wind up sharing the underlying branch pointers in the shared repository. Having two different work-trees with independent index objects but the same underlying branch leads to some tricky problems for users to deal with. Rather than trying to figure out how to deal with these—by either educating programmers or providing tools to deal with the problems—git worktree
simply forbids the situation entirely.
因此,在创建新的工作树时,通常要创建一个 new 分支名称.根据定义,新的分支名称会自动与每个现有分支名称不同:
Hence, it's pretty typical to want to create a new branch name when creating a new work-tree. By definition, a new branch name is automatically different from every existing branch name:
$ git checkout -b newbranch
Switched to a new branch 'newbranch'
$ git checkout -b newbranch
fatal: A branch named 'newbranch' already exists.
这似乎很自然:没有人对此感到惊讶.
This seems pretty natural: no one is ever surprised by this.
您以与git checkout -b
相似的方式运行git worktree add
,不同之处在于,签出发生在新添加的工作树中.但是您已经有一个名为north
的分支.
You're running git worktree add
in a way that is just like git checkout -b
, except that the checkout occurs in the new added work-tree. But you already have a branch named north
.
如果此现有的north
分支无用,则可以将其删除.现在您没有名为north
的本地分支,可以创建一个新分支.
If this existing north
branch is not useful, you can delete it. Now you don't have a local branch named north
and you can create a new one.
如果现有的north
分支有用,请不要将其删除!如果它已经在某些现有工作树中检出,请移至该工作树并在那里进行处理.如果在某些现有工作树中没有签出 ,则可以创建一个新的工作树,以使确实签出;您只需要避免使用-b
标志(以及相应的分支名称):
If this existing north
branch is useful, don't delete it! If it's already checked out in some existing work-tree, move to that work-tree and work on it there. If it's not checked out in some existing work-tree, you can make a new work-tree that does have it checked out; you just need to avoid using the -b
flag (and the corresponding branch name):
git worktree add ../north north
请注意,当您创建 new 分支时,不必重复自己的操作:
Note that when you're creating a new branch, you do not have to repeat yourself:
git worktree add -b newbranch ../path
将在../path
中创建一个新的工作树,并使用git checkout -b newbranch
进行填充.您仅在以下情况下需要分支名称:
will create a new work-tree in ../path
, and use git checkout -b newbranch
to populate it. You only need the branch name when:
- 您没有使用
-b
,并且 - path 参数不以分支名称结尾.
- you're not using
-b
, and - the path argument does not end in the name of the branch.
例如,如果要在路径../zorg
中的新工作树中检出现有分支zorg
,则可以运行:
For instance, if you want to check out the existing branch zorg
in a new work-tree in path ../zorg
, you can just run:
git worktree add ../zorg
在这里,由于既没有-b zorg
也没有最终参数,因此Git通过使用../zorg
的最后一部分(当然只是zorg
)来找出分支名称,因此这会尝试检查出现有分支zorg
进入新的工作树.
Here, since there is neither a -b zorg
nor a final argument, Git figures out the branch name by using the last part of ../zorg
, which is of course just zorg
, so this tries to check out the existing branch zorg
into the new work-tree.
这篇关于Git从现有的远程分支添加工作树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!