2 .\" Title: git-worktree
3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
7 .\" Source: Git 2.45.0.31.gd4cc1ec35f
10 .TH "GIT\-WORKTREE" "1" "2024\-04\-30" "Git 2\&.45\&.0\&.31\&.gd4cc1ec" "Git Manual"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 git-worktree \- Manage multiple working trees
35 \fIgit worktree add\fR [\-f] [\-\-detach] [\-\-checkout] [\-\-lock [\-\-reason <string>]]
36 [\-\-orphan] [(\-b | \-B) <new\-branch>] <path> [<commit\-ish>]
37 \fIgit worktree list\fR [\-v | \-\-porcelain [\-z]]
38 \fIgit worktree lock\fR [\-\-reason <string>] <worktree>
39 \fIgit worktree move\fR <worktree> <new\-path>
40 \fIgit worktree prune\fR [\-n] [\-v] [\-\-expire <expire>]
41 \fIgit worktree remove\fR [\-f] <worktree>
42 \fIgit worktree repair\fR [<path>\&...]
43 \fIgit worktree unlock\fR <worktree>
48 Manage multiple working trees attached to the same repository\&.
50 A git repository can support multiple working trees, allowing you to check out more than one branch at a time\&. With \fBgit worktree add\fR a new working tree is associated with the repository, along with additional metadata that differentiates that working tree from others in the same repository\&. The working tree, along with this metadata, is called a "worktree"\&.
52 This new worktree is called a "linked worktree" as opposed to the "main worktree" prepared by \fBgit-init\fR(1) or \fBgit-clone\fR(1)\&. A repository has one main worktree (if it\(cqs not a bare repository) and zero or more linked worktrees\&. When you are done with a linked worktree, remove it with \fBgit worktree remove\fR\&.
54 In its simplest form, \fBgit worktree add <path>\fR automatically creates a new branch whose name is the final component of \fB<path>\fR, which is convenient if you plan to work on a new topic\&. For instance, \fBgit worktree add \&.\&./hotfix\fR creates new branch \fBhotfix\fR and checks it out at path \fB\&.\&./hotfix\fR\&. To instead work on an existing branch in a new worktree, use \fBgit worktree add <path> <branch>\fR\&. On the other hand, if you just plan to make some experimental changes or do testing without disturbing existing development, it is often convenient to create a \fIthrowaway\fR worktree not associated with any branch\&. For instance, \fBgit worktree add \-d <path>\fR creates a new worktree with a detached \fBHEAD\fR at the same commit as the current branch\&.
56 If a working tree is deleted without using \fBgit worktree remove\fR, then its associated administrative files, which reside in the repository (see "DETAILS" below), will eventually be removed automatically (see \fBgc\&.worktreePruneExpire\fR in \fBgit-config\fR(1)), or you can run \fBgit worktree prune\fR in the main or any linked worktree to clean up any stale administrative files\&.
58 If the working tree for a linked worktree 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 \fBgit worktree lock\fR command, optionally specifying \fB\-\-reason\fR to explain why the worktree is locked\&.
61 add <path> [<commit\-ish>]
67 into it\&. The new worktree is linked to the current repository, sharing everything except per\-worktree files such as
69 \fBindex\fR, etc\&. As a convenience,
71 may be a bare "\fB\-\fR", which is synonymous with
76 is a branch name (call it
77 \fB<branch>\fR) and is not found, and neither
83 are used, but there does exist a tracking branch in exactly one remote (call it
84 \fB<remote>\fR) with a matching name, treat as equivalent to:
90 $ git worktree add \-\-track \-b <branch> <path> <remote>/<branch>
96 If the branch exists in multiple remotes and one of them is named by the
97 \fBcheckout\&.defaultRemote\fR
98 configuration variable, we\(cqll use that one for the purposes of disambiguation, even if the
100 isn\(cqt unique across all remotes\&. Set it to e\&.g\&.
101 \fBcheckout\&.defaultRemote=origin\fR
102 to always checkout remote branches from there if
104 is ambiguous but exists on the
107 \fBcheckout\&.defaultRemote\fR
109 \fBgit-config\fR(1)\&.
113 is omitted and neither
119 used, then, as a convenience, the new worktree is associated with a branch (call it
120 \fB<branch>\fR) named after
121 \fB$(basename <path>)\fR\&. If
123 doesn\(cqt exist, a new branch based on
125 is automatically created as if
129 does exist, it will be checked out in the new worktree, if it\(cqs not checked out anywhere else, otherwise the command will refuse to create the worktree (unless
138 is used, and there are no valid local branches (or remote branches if
139 \fB\-\-guess\-remote\fR
140 is specified) then, as a convenience, the new worktree is associated with a new unborn branch named
143 \fB$(basename <path>)\fR
150 was passed to the command\&. In the event the repository has a remote and
151 \fB\-\-guess\-remote\fR
152 is used, but no remote or local branches exist, then the command fails with a warning reminding the user to fetch from their remote first (or override by using
153 \fB\-f/\-\-force\fR)\&.
158 List details of each worktree\&. The main worktree is listed first, followed by each of the linked worktrees\&. The output details include whether the worktree is bare, the revision currently checked out, the branch currently checked out (or "detached HEAD" if none), "locked" if the worktree is locked, "prunable" if the worktree can be pruned by the
165 If a worktree 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
171 Move a worktree to a new location\&. Note that the main worktree or linked worktrees containing submodules cannot be moved with this command\&. (The
172 \fBgit worktree repair\fR
173 command, however, can reestablish the connection with linked worktrees if you move the main worktree manually\&.)
178 Prune worktree information in
179 \fB$GIT_DIR/worktrees\fR\&.
184 Remove a worktree\&. Only clean worktrees (no untracked files and no modification in tracked files) can be removed\&. Unclean worktrees or ones with submodules can be removed with
185 \fB\-\-force\fR\&. The main worktree cannot be removed\&.
190 Repair worktree administrative files, if possible, if they have become corrupted or outdated due to external factors\&.
192 For instance, if the main worktree (or bare repository) is moved, linked worktrees will be unable to locate it\&. Running
194 in the main worktree will reestablish the connection from linked worktrees back to the main worktree\&.
196 Similarly, if the working tree for a linked worktree is moved without using
197 \fBgit worktree move\fR, the main worktree (or bare repository) will be unable to locate it\&. Running
199 within the recently\-moved worktree will reestablish the connection\&. If multiple linked worktrees are moved, running
201 from any worktree with each tree\(cqs new
203 as an argument, will reestablish the connection to all the specified paths\&.
205 If both the main worktree and linked worktrees have been moved manually, then running
207 in the main worktree and specifying the new
209 of each linked worktree will reestablish all connections in both directions\&.
214 Unlock a worktree, allowing it to be pruned, moved or deleted\&.
222 refuses to create a new worktree when
224 is a branch name and is already checked out by another worktree, or if
226 is already assigned to some worktree but is missing (for instance, if
228 was deleted manually)\&. This option overrides these safeguards\&. To add a missing but locked worktree path, specify
233 refuses to move a locked worktree unless
235 is specified twice\&. If the destination is already assigned to some other worktree but is missing (for instance, if
237 was deleted manually), then
239 allows the move to proceed; use
241 twice if the destination is locked\&.
244 refuses to remove an unclean worktree unless
246 is used\&. To remove a locked worktree, specify
251 \-b <new\-branch>, \-B <new\-branch>
254 \fBadd\fR, create a new branch named
257 \fB<commit\-ish>\fR, and check out
259 into the new worktree\&. If
261 is omitted, it defaults to
262 \fBHEAD\fR\&. By default,
264 refuses to create a new branch if it already exists\&.
266 overrides this safeguard, resetting
269 \fB<commit\-ish>\fR\&.
277 in the new worktree\&. See "DETACHED HEAD" in
278 \fBgit-checkout\fR(1)\&.
286 \fB<commit\-ish>\fR, however,
287 \fB\-\-no\-checkout\fR
288 can be used to suppress checkout in order to make customizations, such as configuring sparse\-checkout\&. See "Sparse checkout" in
289 \fBgit-read-tree\fR(1)\&.
292 \-\-[no\-]guess\-remote
295 \fBworktree add <path>\fR, without
296 \fB<commit\-ish>\fR, instead of creating a new branch from
297 \fBHEAD\fR, if there exists a tracking branch in exactly one remote matching the basename of
298 \fB<path>\fR, base the new branch on the remote\-tracking branch, and mark the remote\-tracking branch as "upstream" from the new branch\&.
300 This can also be set up as the default behaviour by using the
301 \fBworktree\&.guessRemote\fR
307 When creating a new branch, if
309 is a branch, mark it as "upstream" from the new branch\&. This is the default if
311 is a remote\-tracking branch\&. See
320 Keep the worktree locked after creation\&. This is the equivalent of
321 \fBgit worktree lock\fR
323 \fBgit worktree add\fR, but without a race condition\&.
329 \fBprune\fR, do not remove anything; just report what it would remove\&.
335 \fBadd\fR, make the new worktree and index empty, associating the worktree with a new unborn branch named
336 \fB<new\-branch>\fR\&.
342 \fBlist\fR, output in an easy\-to\-parse format for scripts\&. This format will remain stable across Git versions and regardless of user configuration\&. It is recommended to combine this with
343 \fB\-z\fR\&. See below for details\&.
348 Terminate each line with a NUL rather than a newline when
351 \fBlist\fR\&. This makes it possible to parse the output when a worktree path contains a newline character\&.
357 \fBadd\fR, suppress feedback messages\&.
363 \fBprune\fR, report all removals\&.
366 \fBlist\fR, output additional information about worktrees (see below)\&.
372 \fBprune\fR, only expire unused worktrees older than
376 \fBlist\fR, annotate missing worktrees as prunable if they are older than
385 \fBadd \-\-lock\fR, an explanation why the worktree is locked\&.
390 Worktrees can be identified by path, either relative or absolute\&.
392 If the last path components in the worktree\(cqs path is unique among worktrees, it can be used to identify a worktree\&. For example if you only have two worktrees, at
395 \fB/abc/def/ggg\fR, then
399 is enough to point to the former worktree\&.
403 When using multiple worktrees, some refs are shared between all worktrees, but others are specific to an individual worktree\&. One example is \fBHEAD\fR, which is different for each worktree\&. This section is about the sharing rules and how to access refs of one worktree from another\&.
405 In general, all pseudo refs are per\-worktree and all refs starting with \fBrefs/\fR are shared\&. Pseudo refs are ones like \fBHEAD\fR which are directly under \fB$GIT_DIR\fR instead of inside \fB$GIT_DIR/refs\fR\&. There are exceptions, however: refs inside \fBrefs/bisect\fR, \fBrefs/worktree\fR and \fBrefs/rewritten\fR are not shared\&.
407 Refs that are per\-worktree can still be accessed from another worktree via two special paths, \fBmain\-worktree\fR and \fBworktrees\fR\&. The former gives access to per\-worktree refs of the main worktree, while the latter to all linked worktrees\&.
409 For example, \fBmain\-worktree/HEAD\fR or \fBmain\-worktree/refs/bisect/good\fR resolve to the same value as the main worktree\(cqs \fBHEAD\fR and \fBrefs/bisect/good\fR respectively\&. Similarly, \fBworktrees/foo/HEAD\fR or \fBworktrees/bar/refs/bisect/bad\fR are the same as \fB$GIT_COMMON_DIR/worktrees/foo/HEAD\fR and \fB$GIT_COMMON_DIR/worktrees/bar/refs/bisect/bad\fR\&.
411 To access refs, it\(cqs best not to look inside \fB$GIT_DIR\fR directly\&. Instead use commands such as \fBgit-rev-parse\fR(1) or \fBgit-update-ref\fR(1) which will handle refs correctly\&.
412 .SH "CONFIGURATION FILE"
414 By default, the repository \fBconfig\fR file is shared across all worktrees\&. If the config variables \fBcore\&.bare\fR or \fBcore\&.worktree\fR are present in the common config file and \fBextensions\&.worktreeConfig\fR is disabled, then they will be applied to the main worktree only\&.
416 In order to have worktree\-specific configuration, you can turn on the \fBworktreeConfig\fR extension, e\&.g\&.:
422 $ git config extensions\&.worktreeConfig true
429 In this mode, specific configuration stays in the path pointed by \fBgit rev\-parse \-\-git\-path config\&.worktree\fR\&. You can add or update configuration in this file with \fBgit config \-\-worktree\fR\&. Older Git versions will refuse to access repositories with this extension\&.
431 Note that in this file, the exception for \fBcore\&.bare\fR and \fBcore\&.worktree\fR is gone\&. If they exist in \fB$GIT_DIR/config\fR, you must move them to the \fBconfig\&.worktree\fR of the main worktree\&. You may also take this opportunity to review and move other configuration that you do not want to share to all worktrees:
441 \fBcore\&.worktree\fR
442 should never be shared\&.
454 should not be shared if the value is
455 \fBcore\&.bare=true\fR\&.
466 \fBcore\&.sparseCheckout\fR
467 should not be shared, unless you are sure you always use sparse checkout for all worktrees\&.
470 See the documentation of \fBextensions\&.worktreeConfig\fR in \fBgit-config\fR(1) for more details\&.
473 Each linked worktree has a private sub\-directory in the repository\(cqs \fB$GIT_DIR/worktrees\fR directory\&. The private sub\-directory\(cqs name is usually the base name of the linked worktree\(cqs path, possibly appended with a number to make it unique\&. For example, when \fB$GIT_DIR=/path/main/\&.git\fR the command \fBgit worktree add /path/other/test\-next next\fR creates the linked worktree in \fB/path/other/test\-next\fR and also creates a \fB$GIT_DIR/worktrees/test\-next\fR directory (or \fB$GIT_DIR/worktrees/test\-next1\fR if \fBtest\-next\fR is already taken)\&.
475 Within a linked worktree, \fB$GIT_DIR\fR is set to point to this private directory (e\&.g\&. \fB/path/main/\&.git/worktrees/test\-next\fR in the example) and \fB$GIT_COMMON_DIR\fR is set to point back to the main worktree\(cqs \fB$GIT_DIR\fR (e\&.g\&. \fB/path/main/\&.git\fR)\&. These settings are made in a \fB\&.git\fR file located at the top directory of the linked worktree\&.
477 Path resolution via \fBgit rev\-parse \-\-git\-path\fR uses either \fB$GIT_DIR\fR or \fB$GIT_COMMON_DIR\fR depending on the path\&. For example, in the linked worktree \fBgit rev\-parse \-\-git\-path HEAD\fR returns \fB/path/main/\&.git/worktrees/test\-next/HEAD\fR (not \fB/path/other/test\-next/\&.git/HEAD\fR or \fB/path/main/\&.git/HEAD\fR) while \fBgit rev\-parse \-\-git\-path refs/heads/master\fR uses \fB$GIT_COMMON_DIR\fR and returns \fB/path/main/\&.git/refs/heads/master\fR, since refs are shared across all worktrees, except \fBrefs/bisect\fR, \fBrefs/worktree\fR and \fBrefs/rewritten\fR\&.
479 See \fBgitrepository-layout\fR(5) for more information\&. The rule of thumb is do not make any assumption about whether a path belongs to \fB$GIT_DIR\fR or \fB$GIT_COMMON_DIR\fR when you need to directly access something inside \fB$GIT_DIR\fR\&. Use \fBgit rev\-parse \-\-git\-path\fR to get the final path\&.
481 If you manually move a linked worktree, you need to update the \fBgitdir\fR file in the entry\(cqs directory\&. For example, if a linked worktree is moved to \fB/newpath/test\-next\fR and its \fB\&.git\fR file points to \fB/path/main/\&.git/worktrees/test\-next\fR, then update \fB/path/main/\&.git/worktrees/test\-next/gitdir\fR to reference \fB/newpath/test\-next\fR instead\&. Better yet, run \fBgit worktree repair\fR to reestablish the connection automatically\&.
483 To prevent a \fB$GIT_DIR/worktrees\fR entry from being pruned (which can be useful in some situations, such as when the entry\(cqs worktree is stored on a portable device), use the \fBgit worktree lock\fR command, which adds a file named \fBlocked\fR to the entry\(cqs directory\&. The file contains the reason in plain text\&. For example, if a linked worktree\(cqs \fB\&.git\fR file points to \fB/path/main/\&.git/worktrees/test\-next\fR then a file named \fB/path/main/\&.git/worktrees/test\-next/locked\fR will prevent the \fBtest\-next\fR entry from being pruned\&. See \fBgitrepository-layout\fR(5) for details\&.
485 When \fBextensions\&.worktreeConfig\fR is enabled, the config file \fB\&.git/worktrees/<id>/config\&.worktree\fR is read after \fB\&.git/config\fR is\&.
486 .SH "LIST OUTPUT FORMAT"
488 The \fBworktree list\fR command has two output formats\&. The default format shows the details on a single line with columns\&. For example:
495 /path/to/bare\-source (bare)
496 /path/to/linked\-worktree abcd1234 [master]
497 /path/to/other\-linked\-worktree 1234abc (detached HEAD)
504 The command also shows annotations for each worktree, according to its state\&. These annotations are:
514 \fBlocked\fR, if the worktree is locked\&.
525 \fBprunable\fR, if the worktree can be pruned via
526 \fBgit worktree prune\fR\&.
534 /path/to/linked\-worktree abcd1234 [master]
535 /path/to/locked\-worktree acbd5678 (brancha) locked
536 /path/to/prunable\-worktree 5678abc (detached HEAD) prunable
543 For these annotations, a reason might also be available and this can be seen using the verbose mode\&. The annotation is then moved to the next line indented followed by the additional information\&.
549 $ git worktree list \-\-verbose
550 /path/to/linked\-worktree abcd1234 [master]
551 /path/to/locked\-worktree\-no\-reason abcd5678 (detached HEAD) locked
552 /path/to/locked\-worktree\-with\-reason 1234abcd (brancha)
553 locked: worktree path is mounted on a portable device
554 /path/to/prunable\-worktree 5678abc1 (detached HEAD)
555 prunable: gitdir file points to non\-existent location
562 Note that the annotation is moved to the next line if the additional information is available, otherwise it stays on the same line as the worktree itself\&.
563 .SS "Porcelain Format"
565 The porcelain format has a line per attribute\&. If \fB\-z\fR is given then the lines are terminated with NUL rather than a newline\&. Attributes are listed with a label and value separated by a single space\&. Boolean attributes (like \fBbare\fR and \fBdetached\fR) are listed as a label only, and are present only if the value is true\&. Some attributes (like \fBlocked\fR) can be listed as a label only or with a value depending upon whether a reason is available\&. The first attribute of a worktree is always \fBworktree\fR, an empty line indicates the end of the record\&. For example:
571 $ git worktree list \-\-porcelain
572 worktree /path/to/bare\-source
575 worktree /path/to/linked\-worktree
576 HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
577 branch refs/heads/master
579 worktree /path/to/other\-linked\-worktree
580 HEAD 1234abc1234abc1234abc1234abc1234abc1234a
583 worktree /path/to/linked\-worktree\-locked\-no\-reason
584 HEAD 5678abc5678abc5678abc5678abc5678abc5678c
585 branch refs/heads/locked\-no\-reason
588 worktree /path/to/linked\-worktree\-locked\-with\-reason
589 HEAD 3456def3456def3456def3456def3456def3456b
590 branch refs/heads/locked\-with\-reason
591 locked reason why is locked
593 worktree /path/to/linked\-worktree\-prunable
594 HEAD 1233def1234def1234def1234def1234def1234b
596 prunable gitdir file points to non\-existent location
603 Unless \fB\-z\fR is used any "unusual" characters in the lock reason such as newlines are escaped and the entire reason is quoted as explained for the configuration variable \fBcore\&.quotePath\fR (see \fBgit-config\fR(1))\&. For Example:
609 $ git worktree list \-\-porcelain
611 locked "reason\enwhy is locked"
620 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 \fBgit-stash\fR(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 don\(cqt want to risk disturbing any of it\&. Instead, you create a temporary linked worktree to make the emergency fix, remove it when done, and then resume your earlier refactoring session\&.
626 $ git worktree add \-b emergency\-fix \&.\&./temp master
628 # \&.\&.\&. hack hack hack \&.\&.\&.
629 $ git commit \-a \-m \*(Aqemergency fix for boss\*(Aq
631 $ git worktree remove \&.\&./temp
639 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\&.
642 Part of the \fBgit\fR(1) suite