Git 从现有的远程分支添加工作树

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45491328/
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:50:44  来源:igfitidea点击:

Git add a worktree from existing remote branch

gitgit-branchgit-remotegit-worktree

提问by vaheeds

In my remote repository there are 3 branches (master and 2 long running branches):

在我的远程存储库中有 3 个分支(主分支和 2 个长期运行的分支):

master  #the common features are here like Core, DAL,...
north   #customized for A company (long-running)
razavi  #customized for B company (long-running)

At my office PC, I add 2 worktreefor those northand razavibranches:

在我的办公室 PC 上,我worktree为这些northrazavi分支添加了 2 个:

$ git worktree list
C:/Source/nis     a6fb6e1 [master]
C:/Source/north   ebc7670 [north]
C:/Source/razavi  eed08a2 [razavi]

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:

到目前为止一切都很好,我决定也在家中处理这个项目,但是在我的家用 PC 中,当我尝试为这两个分支添加工作树时,它给了我一个错误:

$git worktree add -b north ../north north
fatal: A branch named 'north' already exists.

I remove the -bswitch to not add a new branch, but it doesn't work too.

我删除了-b开关以不添加新分支,但它也不起作用。

How can I add a worktreefrom existing branch that is not local but remote?

如何worktree从不是本地而是远程的现有分支添加一个?

回答by torek

TL;DR: you probably wanted git worktree add ../north north

TL;DR:你可能想要 git worktree add ../north north

First, a reminder (or information for others coming across this question): git worktree addwants to create a new work-tree and, at the same time, make sure that this new work-tree is using a different branch namefrom every other work-tree. This is because, while each added work-tree has its own index and HEAD, the HEADfiles wind up sharingthe 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 howto deal with these—by either educating programmers or providing tools to deal with the problems—git worktreesimply forbids the situation entirely.

首先,提醒(或信息的其它部门这一问题推出):git worktree add要创建一个新的工作树,在同一时间,确保了这款新作树是使用不同的分支名从每一个其他工作树. 这是因为,虽然每个添加的工作树都有自己的索引 和HEAD,但HEAD文件最终会共享共享存储库中的底层分支指针。拥有两个具有独立索引对象但具有相同底层分支的不同工作树会导致用户处理一些棘手的问题。而不是试图弄清楚如何处理这些问题——通过教育程序员或提供工具来处理这些问题——git worktree只是完全禁止这种情况。

Hence, it's pretty typical to want to create a newbranch 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.

这似乎很自然:没有人对此感到惊讶。

You're running git worktree addin 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.

您正在git worktree add以一种类似于 的方式运行git checkout -b,只是结帐发生在新添加的工作树中。但是您已经有一个名为north.

If this existing northbranch is not useful, you can delete it. Now you don't have a local branch named northand you can create a new one.

如果此现有north分支没有用,您可以将其删除。现在您没有命名的本地分支north,您可以创建一个新分支。

If this existing northbranch isuseful, 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 notchecked out in some existing work-tree, you can make a new work-tree that doeshave it checked out; you just need to avoid using the -bflag (and the corresponding branch name):

如果现有的north分支有用的,请不要删除它!如果它已经在某个现有的工作树中签出,请移至该工作树并在那里进行处理。如果它不是在现有的一些工作树签出,你可以做一个新的工作树确实已经签出; 您只需要避免使用-b标志(和相应的分支名称):

git worktree add ../north north

Note that when you're creating a newbranch, you do not have to repeat yourself:

请注意,当您创建分支时,您不必重复自己:

git worktree add -b newbranch ../path

will create a new work-tree in ../path, and use git checkout -b newbranchto populate it. You only need the branch name when:

将在 中创建一个新的工作树../path,并用于git checkout -b newbranch填充它。在以下情况下您只需要分支名称:

  1. you're not using -b, and
  2. the pathargument does not end in the name of the branch.
  1. 你没有使用-b,并且
  2. 路径参数不分支的名称结束。

For instance, if you want to check out the existing branch zorgin a new work-tree in path ../zorg, you can just run:

例如,如果您想zorg在 path 中的新工作树中检出现有分支../zorg,您只需运行:

git worktree add ../zorg

Here, since there is neither a -b zorgnor 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 zorginto the new work-tree.

在这里,由于既没有 a-b zorg也没有 final 参数,Git 通过使用 的最后一部分来计算分支名称../zorg,这当然只是zorg,因此它尝试将现有分支检出zorg到新的工作树中。

回答by vaheeds

For this problem, worktree adddoes need a --checkoutswitch to do so:

对于这个问题,worktree add确实需要一个--checkout开关来做到这一点:

$ git worktree add --checkout ../north north
$ git worktree add --checkout ../razavi razavi

回答by VonC

In addition of git worktree add --checkout, Git 2.16 (Q1 2018) will propose another alternative:

此外git worktree add --checkout,Git 2.16(2018 年第一季度)将提出另一种替代方案:

The way "git worktree add" determines what branch to create from where and checkout in the new worktree has been updated a bit.

方式“ git worktree add”确定从哪里创建哪个分支,并在新工作树中结帐已更新了一点。

See commit e92445a, commit 71d6682(29 Nov 2017), and commit 4e85333, commit e284e89, commit c4738ae, commit 7c85a87(26 Nov 2017) by Thomas Gummerer (tgummerer).
(Merged by Junio C Hamano -- gitster--in commit 66d3f19, 19 Dec 2017)

请参阅Thomas Gummerer ( ) 的commit e92445acommit 71d6682(2017 年 11 月 29 日)和commit 4e85333commit e284e89commit c4738aecommit 7c85a87(2017 年 11 月 26 日(由Junio C Hamano合并-- --提交 66d3f19,2017 年 12 月 19 日)tgummerer
gitster

add worktree.guessRemoteconfig option

Some users might want to have the --guess-remoteoption introduced in the previous commit on by default, so they don't have to type it out every time they create a new worktree.

Add a config option worktree.guessRemotethat allows users to configure the default behaviour for themselves.

添加worktree.guessRemote配置选项

一些用户可能希望--guess-remote默认情况下在上一次提交中引入该选项,因此他们不必每次创建新工作树时都输入它。

添加一个配置选项worktree.guessRemote,允许用户为自己配置默认行为。

The documentation for git confignow reads:

git config文档现在显示为:

worktree.guessRemote::

With add, if no branch argument, and neither of -bnor -Bnor --detachare given, the command defaults to creating a new branch from HEAD.
If worktree.guessRemoteis set to true, worktree addtries to find a remote-tracking branch whose name uniquely matches the new branch name.

  • If such a branch exists, it is checked out and set as "upstream" for the new branch.
  • If no such match can be found, it falls back to creating a new branch from the current HEAD.
worktree.guessRemote::

使用add,如果没有分支参数,并且既没有给出-b-B没有--detach给出,命令默认从 HEAD 创建一个新分支。
如果worktree.guessRemote设置为 true,则worktree add尝试查找名称与新分支名称唯一匹配的远程跟踪分支。

  • 如果存在这样的分支,则将其检出并设置为新分支的“上游”。
  • 如果找不到这样的匹配,它会退回到从当前 HEAD 创建一个新分支。


Actually, Git 2.21 (Q1 2019) clarifies the documentation for this option, which jumped right in with "With add", without explaining that addis a sub-command of "git worktree".

实际上,Git 2.21(2019 年第 1 季度)澄清了此选项的文档,其中直接使用“With add”,但没有说明这add是“git worktree”的子命令。

See commit b4583d5(23 Dec 2018) by Eric Sunshine (sunshineco).
(Merged by Eric Sunshine -- sunshineco--in commit b4583d5, 28 Dec 2018)

请参阅Eric Sunshine ( )提交的 b4583d5(2018 年 12 月 23 日(由Eric Sunshine合并-- --提交 b4583d5 中,2018 年 12 月 28 日)sunshineco
sunshineco

The documentationnow reads:

文档现在显示:

worktree.guessRemote:

If no branch is specified and neither -bnor -Bnor --detachis used, then git worktree adddefaults to creating a new branch from HEAD.

worktree.guessRemote

如果没有指定分支和既不-b也不-B也不--detach被使用,则git worktree add默认为创建从HEAD一个新的分支。

回答by VonC

In addition of "guessing the remote branch", as I explain in my other answer, Git 2.18 (Q2 2018) will offer a new feature:
"git worktree add" learned to check out an existing branch.

除了“猜测远程分支”,因为我的我在其他的答案解释,Git的2.18(Q2 2018)将提供一个新的功能:
git worktree add”学会了检查现有的分支。

See commit f60a7b7, commit 6427f87, commit 2c27002, commit d861d34(24 Apr 2018) by Thomas Gummerer (tgummerer).
Helped-by: Eric Sunshine (sunshineco).
(Merged by Junio C Hamano -- gitster--in commit 10174da, 23 May 2018)

请参阅Thomas Gummerer ( ) 的提交 f60a7b7提交 6427f87提交 2c27002提交 d861d34(2018 年 4 月 24 日。 帮助者:Eric Sunshine ( )(由Junio C Hamano合并-- --commit 10174da,2018 年 5 月 23 日)tgummerer
sunshineco
gitster

worktree: teach "add" to check out existing branches

Currently 'git worktree add <path>' creates a new branch named after the basename of the path by default.
If a branch with that name already exists, the command refuses to do anything, unless the '--force' option is given.

However we can do a little better than that, and check the branch out if it is not checked out anywhere else.
This will help users who just want to check an existing branch out into a new worktree
, and save a few keystrokes.

As the current behaviour is to simply 'die()' when a branch with the name of the basename of the path already exists, there are no backwards compatibility worries here.

We will still 'die()' if the branch is checked out in another worktree, unless the --forceflag is passed.

工作树:教“ add”检查现有分支

当前git worktree add <path>,默认情况下“ ”会创建一个以路径的基本名称命名的新分支。
如果具有该名称的分支已经存在,则该命令拒绝执行任何操作,除非给出了 ' --force' 选项。

然而,我们可以做得比这更好一点,如果在其他任何地方都没有检出分支,就将其检出。
这将帮助那些只想将现有分支检出到新工作树中的用户
,并节省一些按键操作。

由于当前的行为是die()在具有路径基本名称名称的分支已经存在时简单地使用“ ”,因此这里没有向后兼容性问题。

die()如果分支在另一个工作树中检出,我们仍然会 ' ',除非--force标志被传递。

The documentation now states:

文档现在指出

$ git worktree add --track -b <branch> <path> <remote>/<branch>

If <commit-ish>is omitted and neither -bnor -Bnor --detachused, then, as a convenience, the new worktree is associated with a branch (call it <branch>) named after $(basename <path>).

  • If <branch>doesn't exist, a new branch based on HEAD is automatically created as if -b <branch>was given.
  • If <branch>does exist, it will be checked out in the new worktree, if it's not checked out anywhere else, otherwise the command will refuse to create the worktree (unless --forceis used).
$ git worktree add --track -b <branch> <path> <remote>/<branch>

如果<commit-ish>省略被既不-b也不-B也不--detach使用,那么,作为一种方便,新worktree与分支(称之为相关联<branch>)而得名$(basename <path>)

  • 如果<branch>不存在,则会自动创建一个基于 HEAD 的新分支,就像-b <branch>给定的一样。
  • 如果<branch>确实存在,它将在新的工作树中检出,如果没有在其他任何地方检出,否则该命令将拒绝创建工作树(除非--force使用)。