uTools-Manuals/docs/git/git worktree.html
2019-05-07 10:40:55 +08:00

15 lines
15 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="c-markdown doc-markdown"><div class="doc-postil"><div class="c-markdown"><h2>名称</h2></div></div><div class="doc-postil"><div class="c-markdown"><p>git-worktree  - 管理多个工作树</p></div></div><div class="doc-postil"><div class="c-markdown"><h2>概要</h2></div></div><div class="doc-postil"><div class="c-markdown"><pre><code class="language-Bash">git worktree add [-f] [--detach] [--checkout] [--lock] [-b &lt;new-branch&gt;] &lt;path&gt; [&lt;branch&gt;]git worktree list [--porcelain]git worktree lock [--reason &lt;string&gt;] &lt;worktree&gt;git worktree prune [-n] [-v] [--expire &lt;expire&gt;]git worktree unlock &lt;worktree&gt;</code></pre></div></div><div class="doc-postil"><div class="c-markdown"><h2>描述</h2></div></div><div class="doc-postil"><div class="c-markdown"><p>管理连接到同一存储库的多个工作树。</p></div></div><div class="doc-postil"><div class="c-markdown"><p>一个 git 仓库可以支持多个工作树,允许你一次检出多个分支。随着<code>git worktree add</code>新的工作树与库相关联。这个新的工作树称为“链接工作树”而不是由“git init”或“git clone”准备的“主工作树”。存储库具有一个主工作树如果它不是裸存储库以及零个或多个链接的工作树。</p></div></div><div class="doc-postil"><div class="c-markdown"><p>当你完成一个链接的工作树时你可以简单地删除它。存储库中的工作树管理文件请参阅下面的“详细信息”最终会自动删除请参照在git-config [1]中<code>gc.worktreePruneExpire</code>),或者您可以在主或任何链接的工作树中运行<code>git worktree prune</code>以清除任何陈旧的管理文件。</p></div></div><div class="doc-postil"><div class="c-markdown"><p>If you move a linked working tree, you need to manually update the administrative files so that they do not get pruned automatically. See section "DETAILS" for more information.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>If a linked working tree is stored on a portable device or network share which is not always mounted, you can prevent its administrative files from being pruned by issuing the <code>git worktree lock</code> command, optionally specifying <code>--reason</code> to explain why the working tree is locked.</p></div></div><div class="doc-postil"><div class="c-markdown"><h2>Commands</h2></div></div><div class="doc-postil"><div class="c-markdown"><p>add &lt;path&gt; &lt;branch&gt;</p></div></div><div class="doc-postil"><div class="c-markdown"><p>Create <code>&lt;path&gt;</code> and checkout <code>&lt;branch&gt;</code> into it. The new working directory is linked to the current repository, sharing everything except working directory specific files such as HEAD, index, etc. <code>-</code> may also be specified as <code>&lt;branch&gt;</code>; it is synonymous with <code>@{-1}</code>.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>If <code>&lt;branch&gt;</code> is omitted and neither <code>-b</code> nor <code>-B</code> nor <code>--detach</code> used, then, as a convenience, a new branch based at HEAD is created automatically, as if <code>-b $(basename &lt;path&gt;)</code> was specified.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>list</p></div></div><div class="doc-postil"><div class="c-markdown"><p>List details of each worktree. The main worktree is listed first, followed by each of the linked worktrees. The output details include if the worktree is bare, the revision currently checked out, and the branch currently checked out (or <code>detached HEAD</code> if none).</p></div></div><div class="doc-postil"><div class="c-markdown"><p>lock</p></div></div><div class="doc-postil"><div class="c-markdown"><p>If a working tree is on a portable device or network share which is not always mounted, lock it to prevent its administrative files from being pruned automatically. This also prevents it from being moved or deleted. Optionally, specify a reason for the lock with <code>--reason</code>.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>prune</p></div></div><div class="doc-postil"><div class="c-markdown"><p>Prune working tree information in $GIT_DIR/worktrees.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>unlock</p></div></div><div class="doc-postil"><div class="c-markdown"><p>Unlock a working tree, allowing it to be pruned, moved or deleted.</p></div></div><div class="doc-postil"><div class="c-markdown"><h2>Options</h2></div></div><div class="doc-postil"><div class="c-markdown"><p>-f   --force</p></div></div><div class="doc-postil"><div class="c-markdown"><p>By default, <code>add</code> refuses to create a new working tree when <code>&lt;branch&gt;</code> is already checked out by another working tree. This option overrides that safeguard.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>-b &lt;new-branch&gt;   -B &lt;new-branch&gt;</p></div></div><div class="doc-postil"><div class="c-markdown"><p>With <code>add</code>, create a new branch named <code>&lt;new-branch&gt;</code> starting at <code>&lt;branch&gt;</code>, and check out <code>&lt;new-branch&gt;</code> into the new working tree. If <code>&lt;branch&gt;</code> is omitted, it defaults to HEAD. By default, <code>-b</code> refuses to create a new branch if it already exists. <code>-B</code> overrides this safeguard, resetting <code>&lt;new-branch&gt;</code> to <code>&lt;branch&gt;</code>.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>--detach</p></div></div><div class="doc-postil"><div class="c-markdown"><p>With <code>add</code>, detach HEAD in the new working tree. See "DETACHED HEAD" in git-checkout[1].</p></div></div><div class="doc-postil"><div class="c-markdown"><p>--no-checkout</p></div></div><div class="doc-postil"><div class="c-markdown"><p>By default, <code>add</code> checks out <code>&lt;branch&gt;</code>, however, <code>--no-checkout</code> can be used to suppress checkout in order to make customizations, such as configuring sparse-checkout. See "Sparse checkout" in git-read-tree[1].</p></div></div><div class="doc-postil"><div class="c-markdown"><p>--lock</p></div></div><div class="doc-postil"><div class="c-markdown"><p>Keep the working tree locked after creation. This is the equivalent of <code>git worktree lock</code> after <code>git worktree add</code>, but without race condition.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>-n   --dry-run</p></div></div><div class="doc-postil"><div class="c-markdown"><p>With <code>prune</code>, do not remove anything; just report what it would remove.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>--porcelain</p></div></div><div class="doc-postil"><div class="c-markdown"><p>With <code>list</code>, output in an easy-to-parse format for scripts. This format will remain stable across Git versions and regardless of user configuration. See below for details.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>-v   --verbose</p></div></div><div class="doc-postil"><div class="c-markdown"><p>With <code>prune</code>, report all removals.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>--expire &lt;time&gt;</p></div></div><div class="doc-postil"><div class="c-markdown"><p>With <code>prune</code>, only expire unused working trees older than &lt;time&gt;.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>--reason &lt;string&gt;</p></div></div><div class="doc-postil"><div class="c-markdown"><p>With <code>lock</code>, an explanation why the working tree is locked.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>&lt;worktree&gt;</p></div></div><div class="doc-postil"><div class="c-markdown"><p>Working trees can be identified by path, either relative or absolute.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>If the last path components in the working trees path is unique among working trees, it can be used to identify worktrees. For example if you only have two working trees, at "/abc/def/ghi" and "/abc/def/ggg", then "ghi" or "def/ghi" is enough to point to the former working tree.</p></div></div><div class="doc-postil"><div class="c-markdown"><h2>Details</h2></div></div><div class="doc-postil"><div class="c-markdown"><p>Each linked working tree has a private sub-directory in the repositorys $GIT_DIR/worktrees directory. The private sub-directorys name is usually the base name of the linked working trees path, possibly appended with a number to make it unique. For example, when <code>$GIT_DIR=/path/main/.git</code> the command <code>git worktree add /path/other/test-next next</code> creates the linked working tree in <code>/path/other/test-next</code> and also creates a <code>$GIT_DIR/worktrees/test-next</code> directory (or <code>$GIT_DIR/worktrees/test-next1</code> if <code>test-next</code> is already taken).</p></div></div><div class="doc-postil"><div class="c-markdown"><p>Within a linked working tree, $GIT_DIR is set to point to this private directory (e.g. <code>/path/main/.git/worktrees/test-next</code> in the example) and $GIT_COMMON_DIR is set to point back to the main working trees $GIT_DIR (e.g. <code>/path/main/.git</code>). These settings are made in a <code>.git</code> file located at the top directory of the linked working tree.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>Path resolution via <code>git rev-parse --git-path</code> uses either $GIT_DIR or $GIT_COMMON_DIR depending on the path. For example, in the linked working tree <code>git rev-parse --git-path HEAD</code> returns <code>/path/main/.git/worktrees/test-next/HEAD</code> (not <code>/path/other/test-next/.git/HEAD</code> or <code>/path/main/.git/HEAD</code>) while <code>git rev-parse --git-path refs/heads/master</code> uses $GIT_COMMON_DIR and returns <code>/path/main/.git/refs/heads/master</code>, since refs are shared across all working trees.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>See gitrepository-layout[5] for more information. The rule of thumb is do not make any assumption about whether a path belongs to $GIT_DIR or $GIT_COMMON_DIR when you need to directly access something inside $GIT_DIR. Use <code>git rev-parse --git-path</code> to get the final path.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>If you move a linked working tree, you need to update the <code>gitdir</code> file in the entrys directory. For example, if a linked working tree is moved to <code>/newpath/test-next</code> and its <code>.git</code> file points to <code>/path/main/.git/worktrees/test-next</code>, then update <code>/path/main/.git/worktrees/test-next/gitdir</code> to reference <code>/newpath/test-next</code> instead.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>To prevent a $GIT_DIR/worktrees entry from being pruned (which can be useful in some situations, such as when the entrys working tree is stored on a portable device), use the <code>git worktree lock</code> command, which adds a file named <code>locked</code> to the entrys directory. The file contains the reason in plain text. For example, if a linked working trees <code>.git</code> file points to <code>/path/main/.git/worktrees/test-next</code> then a file named <code>/path/main/.git/worktrees/test-next/locked</code> will prevent the <code>test-next</code> entry from being pruned. See gitrepository-layout[5] for details.</p></div></div><div class="doc-postil"><div class="c-markdown"><h2>List output format</h2></div></div><div class="doc-postil"><div class="c-markdown"><p>The worktree list command has two output formats. The default format shows the details on a single line with columns. For example:</p></div></div><div class="doc-postil"><div class="c-markdown"><pre><code class="language-Bash">S git worktree list/path/to/bare-source            (bare)/path/to/linked-worktree        abcd1234 [master]/path/to/other-linked-worktree  1234abc  (detached HEAD)</code></pre></div></div><div class="doc-postil"><div class="c-markdown"><h3>Porcelain Format</h3></div></div><div class="doc-postil"><div class="c-markdown"><p>The porcelain format has a line per attribute. Attributes are listed with a label and value separated by a single space. Boolean attributes (like <code>bare</code> and <code>detached</code>) are listed as a label only, and are only present if and only if the value is true. An empty line indicates the end of a worktree. For example:</p></div></div><div class="doc-postil"><div class="c-markdown"><pre><code class="language-Bash">S git worktree list --porcelain
worktree /path/to/bare-source
bare
worktree /path/to/linked-worktree
HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
branch refs/heads/master
worktree /path/to/other-linked-worktree
HEAD 1234abc1234abc1234abc1234abc1234abc1234a
detached</code></pre></div></div><div class="doc-postil"><div class="c-markdown"><h2>Examples</h2></div></div><div class="doc-postil"><div class="c-markdown"><p>You are in the middle of a refactoring session and your boss comes in and demands that you fix something immediately. You might typically use git-stash[1] to store your changes away temporarily, however, your working tree is in such a state of disarray (with new, moved, and removed files, and other bits and pieces strewn around) that you dont want to risk disturbing any of it. Instead, you create a temporary linked working tree to make the emergency fix, remove it when done, and then resume your earlier refactoring session.</p></div></div><div class="doc-postil"><div class="c-markdown"><pre><code class="language-Bash">$ git worktree add -b emergency-fix ../temp master
$ pushd ../temp
# ... hack hack hack ...$ git commit -a -m 'emergency fix for boss'$ popd
$ rm -rf ../temp
$ git worktree prune</code></pre></div></div><div class="doc-postil"><div class="c-markdown"><h2>Bugs</h2></div></div><div class="doc-postil"><div class="c-markdown"><p>Multiple checkout in general is still experimental, and the support for submodules is incomplete. It is NOT recommended to make multiple checkouts of a superproject.</p></div></div><div class="doc-postil"><div class="c-markdown"><p>git-worktree could provide more automation for tasks currently performed manually, such as:</p></div></div><div class="doc-postil"><div class="c-markdown"><ul class="ul-level-0 list-paddingleft-2" style="margin: 10px 0px 10px 20px;"><li><p><code>remove</code> to remove a linked working tree and its administrative files (and warn if the working tree is dirty)</p></li></ul></div></div><div class="doc-postil"><div class="c-markdown"><ul class="ul-level-0 list-paddingleft-2" style="margin: 10px 0px 10px 20px;"><li><p><code>mv</code> to move or rename a working tree and update its administrative files</p></li></ul></div></div><div class="doc-postil"><div class="c-markdown"><pre class="prism-token token language-js"> © 20052017 Linus Torvalds and others</pre></div></div><div class="doc-postil"><div class="c-markdown"><p>Licensed under the GNU General Public License version 2.</p></div></div></div>