2 .\" Title: git-worktree
3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets v1.79.2 <http://docbook.sf.net/>
7 .\" Source: Git 2.46.0.551.gc5ee8f2d1c
10 .TH "GIT\-WORKTREE" "1" "2024-09-10" "Git 2\&.46\&.0\&.551\&.gc5ee8f" "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>
47 Manage multiple working trees attached to the same repository\&.
49 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"\&.
51 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\&.
53 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\&.
55 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\&.
57 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\&.
60 add <path> [<commit\-ish>]
66 into it\&. The new worktree is linked to the current repository, sharing everything except per\-worktree files such as
68 \fBindex\fR, etc\&. As a convenience,
70 may be a bare "\fB\-\fR", which is synonymous with
75 is a branch name (call it
76 \fB<branch>\fR) and is not found, and neither
82 are used, but there does exist a tracking branch in exactly one remote (call it
83 \fB<remote>\fR) with a matching name, treat as equivalent to:
89 $ git worktree add \-\-track \-b <branch> <path> <remote>/<branch>
95 If the branch exists in multiple remotes and one of them is named by the
96 \fBcheckout\&.defaultRemote\fR
97 configuration variable, we\(cqll use that one for the purposes of disambiguation, even if the
99 isn\(cqt unique across all remotes\&. Set it to e\&.g\&.
100 \fBcheckout\&.defaultRemote=origin\fR
101 to always checkout remote branches from there if
103 is ambiguous but exists on the
106 \fBcheckout\&.defaultRemote\fR
108 \fBgit-config\fR(1)\&.
112 is omitted and neither
118 used, then, as a convenience, the new worktree is associated with a branch (call it
119 \fB<branch>\fR) named after
120 \fB$(basename <path>)\fR\&. If
122 doesn\(cqt exist, a new branch based on
124 is automatically created as if
128 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
137 is used, and there are no valid local branches (or remote branches if
138 \fB\-\-guess\-remote\fR
139 is specified) then, as a convenience, the new worktree is associated with a new unborn branch named
142 \fB$(basename <path>)\fR
149 was passed to the command\&. In the event the repository has a remote and
150 \fB\-\-guess\-remote\fR
151 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
152 \fB\-f/\-\-force\fR)\&.
157 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
164 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
170 Move a worktree to a new location\&. Note that the main worktree or linked worktrees containing submodules cannot be moved with this command\&. (The
171 \fBgit worktree repair\fR
172 command, however, can reestablish the connection with linked worktrees if you move the main worktree manually\&.)
177 Prune worktree information in
178 \fB$GIT_DIR/worktrees\fR\&.
183 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
184 \fB\-\-force\fR\&. The main worktree cannot be removed\&.
187 repair [<path>\&...\:]
189 Repair worktree administrative files, if possible, if they have become corrupted or outdated due to external factors\&.
191 For instance, if the main worktree (or bare repository) is moved, linked worktrees will be unable to locate it\&. Running
193 in the main worktree will reestablish the connection from linked worktrees back to the main worktree\&.
195 Similarly, if the working tree for a linked worktree is moved without using
196 \fBgit worktree move\fR, the main worktree (or bare repository) will be unable to locate it\&. Running
198 within the recently\-moved worktree will reestablish the connection\&. If multiple linked worktrees are moved, running
200 from any worktree with each tree\(cqs new
202 as an argument, will reestablish the connection to all the specified paths\&.
204 If both the main worktree and linked worktrees have been moved manually, then running
206 in the main worktree and specifying the new
208 of each linked worktree will reestablish all connections in both directions\&.
213 Unlock a worktree, allowing it to be pruned, moved or deleted\&.
221 refuses to create a new worktree when
223 is a branch name and is already checked out by another worktree, or if
225 is already assigned to some worktree but is missing (for instance, if
227 was deleted manually)\&. This option overrides these safeguards\&. To add a missing but locked worktree path, specify
232 refuses to move a locked worktree unless
234 is specified twice\&. If the destination is already assigned to some other worktree but is missing (for instance, if
236 was deleted manually), then
238 allows the move to proceed; use
240 twice if the destination is locked\&.
243 refuses to remove an unclean worktree unless
245 is used\&. To remove a locked worktree, specify
250 \-b <new\-branch>, \-B <new\-branch>
253 \fBadd\fR, create a new branch named
256 \fB<commit\-ish>\fR, and check out
258 into the new worktree\&. If
260 is omitted, it defaults to
261 \fBHEAD\fR\&. By default,
263 refuses to create a new branch if it already exists\&.
265 overrides this safeguard, resetting
268 \fB<commit\-ish>\fR\&.
276 in the new worktree\&. See "DETACHED HEAD" in
277 \fBgit-checkout\fR(1)\&.
285 \fB<commit\-ish>\fR, however,
286 \fB\-\-no\-checkout\fR
287 can be used to suppress checkout in order to make customizations, such as configuring sparse\-checkout\&. See "Sparse checkout" in
288 \fBgit-read-tree\fR(1)\&.
291 \-\-[no\-]guess\-remote
294 \fBworktree add <path>\fR, without
295 \fB<commit\-ish>\fR, instead of creating a new branch from
296 \fBHEAD\fR, if there exists a tracking branch in exactly one remote matching the basename of
297 \fB<path>\fR, base the new branch on the remote\-tracking branch, and mark the remote\-tracking branch as "upstream" from the new branch\&.
299 This can also be set up as the default behaviour by using the
300 \fBworktree\&.guessRemote\fR
306 When creating a new branch, if
308 is a branch, mark it as "upstream" from the new branch\&. This is the default if
310 is a remote\-tracking branch\&. See
319 Keep the worktree locked after creation\&. This is the equivalent of
320 \fBgit worktree lock\fR
322 \fBgit worktree add\fR, but without a race condition\&.
328 \fBprune\fR, do not remove anything; just report what it would remove\&.
334 \fBadd\fR, make the new worktree and index empty, associating the worktree with a new unborn branch named
335 \fB<new\-branch>\fR\&.
341 \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
342 \fB\-z\fR\&. See below for details\&.
347 Terminate each line with a NUL rather than a newline when
350 \fBlist\fR\&. This makes it possible to parse the output when a worktree path contains a newline character\&.
356 \fBadd\fR, suppress feedback messages\&.
362 \fBprune\fR, report all removals\&.
365 \fBlist\fR, output additional information about worktrees (see below)\&.
371 \fBprune\fR, only expire unused worktrees older than
375 \fBlist\fR, annotate missing worktrees as prunable if they are older than
384 \fBadd \-\-lock\fR, an explanation why the worktree is locked\&.
389 Worktrees can be identified by path, either relative or absolute\&.
391 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
394 \fB/abc/def/ggg\fR, then
398 is enough to point to the former worktree\&.
402 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\&.
404 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\&.
406 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\&.
408 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\&.
410 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\&.
411 .SH "CONFIGURATION FILE"
413 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\&.
415 In order to have worktree\-specific configuration, you can turn on the \fBworktreeConfig\fR extension, e\&.g\&.:
421 $ git config extensions\&.worktreeConfig true
427 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\&.
429 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:
439 \fBcore\&.worktree\fR
440 should never be shared\&.
452 should not be shared if the value is
453 \fBcore\&.bare=true\fR\&.
464 \fBcore\&.sparseCheckout\fR
465 should not be shared, unless you are sure you always use sparse checkout for all worktrees\&.
468 See the documentation of \fBextensions\&.worktreeConfig\fR in \fBgit-config\fR(1) for more details\&.
471 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)\&.
473 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\&.
475 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\&.
477 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\&.
479 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\&.
481 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\&.
483 When \fBextensions\&.worktreeConfig\fR is enabled, the config file \fB\&.git/worktrees/<id>/config\&.worktree\fR is read after \fB\&.git/config\fR is\&.
484 .SH "LIST OUTPUT FORMAT"
486 The \fBworktree list\fR command has two output formats\&. The default format shows the details on a single line with columns\&. For example:
493 /path/to/bare\-source (bare)
494 /path/to/linked\-worktree abcd1234 [master]
495 /path/to/other\-linked\-worktree 1234abc (detached HEAD)
501 The command also shows annotations for each worktree, according to its state\&. These annotations are:
511 \fBlocked\fR, if the worktree is locked\&.
522 \fBprunable\fR, if the worktree can be pruned via
523 \fBgit worktree prune\fR\&.
531 /path/to/linked\-worktree abcd1234 [master]
532 /path/to/locked\-worktree acbd5678 (brancha) locked
533 /path/to/prunable\-worktree 5678abc (detached HEAD) prunable
539 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\&.
545 $ git worktree list \-\-verbose
546 /path/to/linked\-worktree abcd1234 [master]
547 /path/to/locked\-worktree\-no\-reason abcd5678 (detached HEAD) locked
548 /path/to/locked\-worktree\-with\-reason 1234abcd (brancha)
549 locked: worktree path is mounted on a portable device
550 /path/to/prunable\-worktree 5678abc1 (detached HEAD)
551 prunable: gitdir file points to non\-existent location
557 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\&.
558 .SS "Porcelain Format"
560 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:
566 $ git worktree list \-\-porcelain
567 worktree /path/to/bare\-source
570 worktree /path/to/linked\-worktree
571 HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
572 branch refs/heads/master
574 worktree /path/to/other\-linked\-worktree
575 HEAD 1234abc1234abc1234abc1234abc1234abc1234a
578 worktree /path/to/linked\-worktree\-locked\-no\-reason
579 HEAD 5678abc5678abc5678abc5678abc5678abc5678c
580 branch refs/heads/locked\-no\-reason
583 worktree /path/to/linked\-worktree\-locked\-with\-reason
584 HEAD 3456def3456def3456def3456def3456def3456b
585 branch refs/heads/locked\-with\-reason
586 locked reason why is locked
588 worktree /path/to/linked\-worktree\-prunable
589 HEAD 1233def1234def1234def1234def1234def1234b
591 prunable gitdir file points to non\-existent location
597 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:
603 $ git worktree list \-\-porcelain
605 locked "reason\enwhy is locked"
613 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\&.
619 $ git worktree add \-b emergency\-fix \&.\&./temp master
621 # \&.\&.\&. hack hack hack \&.\&.\&.
622 $ git commit \-a \-m \*(Aqemergency fix for boss\*(Aq
624 $ git worktree remove \&.\&./temp
631 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\&.
634 Part of the \fBgit\fR(1) suite