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.48.1.157.g3b0d05c4a7
10 .TH "GIT\-WORKTREE" "1" "2025-01-29" "Git 2\&.48\&.1\&.157\&.g3b0d05" "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\fR \fBworktree\fR \fBadd\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\fR \fBworktree\fR \fBremove\fR\&.
53 In its simplest form, \fBgit\fR \fBworktree\fR \fBadd\fR \fI<path>\fR automatically creates a new branch whose name is the final component of \fI<path>\fR, which is convenient if you plan to work on a new topic\&. For instance, \fBgit\fR \fBworktree\fR \fBadd\fR \fB\&.\&.\fR\fB/hotfix\fR creates new branch \fBhotfix\fR and checks it out at path \fB\&.\&.\fR\fB/hotfix\fR\&. To instead work on an existing branch in a new worktree, use \fBgit\fR \fBworktree\fR \fBadd\fR \fI<path>\fR \fI<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\fR \fBworktree\fR \fBadd\fR \fB\-d\fR \fI<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\fR \fBworktree\fR \fBremove\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\fR \fBworktree\fR \fBprune\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\fR \fBworktree\fR \fBlock\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 \fI<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 \fI<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 \fI<branch>\fR) named after
120 \fB$\fR(\fBbasename\fR
123 doesn\(cqt exist, a new branch based on
125 is automatically created as if
130 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
139 is used, and there are no valid local branches (or remote branches if
140 \fB\-\-guess\-remote\fR
141 is specified) then, as a convenience, the new worktree is associated with a new unborn branch named
144 \fB$\fR(\fBbasename\fR
145 \fI<path>\fR) if neither
151 was passed to the command\&. In the event the repository has a remote and
152 \fB\-\-guess\-remote\fR
153 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
154 \fB\-f/\-\-force\fR)\&.
159 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
166 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
172 Move a worktree to a new location\&. Note that the main worktree or linked worktrees containing submodules cannot be moved with this command\&. (The
176 command, however, can reestablish the connection with linked worktrees if you move the main worktree manually\&.)
181 Prune worktree information in
182 \fB$GIT_DIR/worktrees\fR\&.
187 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
188 \fB\-\-force\fR\&. The main worktree cannot be removed\&.
191 repair [<path>\&...\:]
193 Repair worktree administrative files, if possible, if they have become corrupted or outdated due to external factors\&.
195 For instance, if the main worktree (or bare repository) is moved, linked worktrees will be unable to locate it\&. Running
197 in the main worktree will reestablish the connection from linked worktrees back to the main worktree\&.
199 Similarly, if the working tree for a linked worktree is moved without using
202 \fBmove\fR, the main worktree (or bare repository) will be unable to locate it\&. Running
204 within the recently\-moved worktree will reestablish the connection\&. If multiple linked worktrees are moved, running
206 from any worktree with each tree\(cqs new
208 as an argument, will reestablish the connection to all the specified paths\&.
210 If both the main worktree and linked worktrees have been moved or copied manually, then running
212 in the main worktree and specifying the new
214 of each linked worktree will reestablish all connections in both directions\&.
219 Unlock a worktree, allowing it to be pruned, moved or deleted\&.
227 refuses to create a new worktree when
229 is a branch name and is already checked out by another worktree, or if
231 is already assigned to some worktree but is missing (for instance, if
233 was deleted manually)\&. This option overrides these safeguards\&. To add a missing but locked worktree path, specify
238 refuses to move a locked worktree unless
240 is specified twice\&. If the destination is already assigned to some other worktree but is missing (for instance, if
242 was deleted manually), then
244 allows the move to proceed; use
246 twice if the destination is locked\&.
249 refuses to remove an unclean worktree unless
251 is used\&. To remove a locked worktree, specify
256 \-b <new\-branch>, \-B <new\-branch>
259 \fBadd\fR, create a new branch named
262 \fI<commit\-ish>\fR, and check out
264 into the new worktree\&. If
266 is omitted, it defaults to
267 \fBHEAD\fR\&. By default,
269 refuses to create a new branch if it already exists\&.
271 overrides this safeguard, resetting
274 \fI<commit\-ish>\fR\&.
282 in the new worktree\&. See "DETACHED HEAD" in
283 \fBgit-checkout\fR(1)\&.
291 \fI<commit\-ish>\fR, however,
292 \fB\-\-no\-checkout\fR
293 can be used to suppress checkout in order to make customizations, such as configuring sparse\-checkout\&. See "Sparse checkout" in
294 \fBgit-read-tree\fR(1)\&.
297 \-\-[no\-]guess\-remote
302 \fI<path>\fR, without
303 \fI<commit\-ish>\fR, instead of creating a new branch from
304 \fBHEAD\fR, if there exists a tracking branch in exactly one remote matching the basename of
305 \fI<path>\fR, base the new branch on the remote\-tracking branch, and mark the remote\-tracking branch as "upstream" from the new branch\&.
307 This can also be set up as the default behaviour by using the
308 \fBworktree\&.guessRemote\fR
312 \-\-[no\-]relative\-paths
314 Link worktrees using relative paths or absolute paths (default)\&. Overrides the
315 \fBworktree\&.useRelativePaths\fR
317 \fBgit-config\fR(1)\&.
320 \fBrepair\fR, the linking files will be updated if there\(cqs an absolute/relative mismatch, even if the links are correct\&.
325 When creating a new branch, if
327 is a branch, mark it as "upstream" from the new branch\&. This is the default if
329 is a remote\-tracking branch\&. See
338 Keep the worktree locked after creation\&. This is the equivalent of
345 \fBadd\fR, but without a race condition\&.
351 \fBprune\fR, do not remove anything; just report what it would remove\&.
357 \fBadd\fR, make the new worktree and index empty, associating the worktree with a new unborn branch named
358 \fI<new\-branch>\fR\&.
364 \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
365 \fB\-z\fR\&. See below for details\&.
370 Terminate each line with a NUL rather than a newline when
373 \fBlist\fR\&. This makes it possible to parse the output when a worktree path contains a newline character\&.
379 \fBadd\fR, suppress feedback messages\&.
385 \fBprune\fR, report all removals\&.
388 \fBlist\fR, output additional information about worktrees (see below)\&.
394 \fBprune\fR, only expire unused worktrees older than
398 \fBlist\fR, annotate missing worktrees as prunable if they are older than
408 \fB\-\-lock\fR, an explanation why the worktree is locked\&.
413 Worktrees can be identified by path, either relative or absolute\&.
415 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
418 \fB/abc/def/ggg\fR, then
422 is enough to point to the former worktree\&.
426 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\&.
428 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\&.
430 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\&.
432 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\&.
434 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\&.
435 .SH "CONFIGURATION FILE"
437 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\&.
439 In order to have worktree\-specific configuration, you can turn on the \fBworktreeConfig\fR extension, e\&.g\&.:
445 $ git config extensions\&.worktreeConfig true
451 In this mode, specific configuration stays in the path pointed by \fBgit\fR \fBrev\-parse\fR \fB\-\-git\-path\fR \fBconfig\&.worktree\fR\&. You can add or update configuration in this file with \fBgit\fR \fBconfig\fR \fB\-\-worktree\fR\&. Older Git versions will refuse to access repositories with this extension\&.
453 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:
463 \fBcore\&.worktree\fR
464 should never be shared\&.
476 should not be shared if the value is
477 \fBcore\&.bare=true\fR\&.
488 \fBcore\&.sparseCheckout\fR
489 should not be shared, unless you are sure you always use sparse checkout for all worktrees\&.
492 See the documentation of \fBextensions\&.worktreeConfig\fR in \fBgit-config\fR(1) for more details\&.
495 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\fR \fBworktree\fR \fBadd\fR \fB/path/other/test\-next\fR \fBnext\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)\&.
497 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 \&.\fBgit\fR file located at the top directory of the linked worktree\&.
499 Path resolution via \fBgit\fR \fBrev\-parse\fR \fB\-\-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\fR \fBrev\-parse\fR \fB\-\-git\-path\fR \fBHEAD\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\fR \fBrev\-parse\fR \fB\-\-git\-path\fR \fBrefs/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\&.
501 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\fR \fBrev\-parse\fR \fB\-\-git\-path\fR to get the final path\&.
503 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 \&.\fBgit\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\fR \fBworktree\fR \fBrepair\fR to reestablish the connection automatically\&.
505 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\fR \fBworktree\fR \fBlock\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 \&.\fBgit\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\&.
507 When \fBextensions\&.worktreeConfig\fR is enabled, the config file \&.\fBgit/worktrees/\fR\fI<id>\fR\fB/config\&.worktree\fR is read after \&.\fBgit/config\fR is\&.
508 .SH "LIST OUTPUT FORMAT"
510 The \fBworktree\fR \fBlist\fR command has two output formats\&. The default format shows the details on a single line with columns\&. For example:
517 /path/to/bare\-source (bare)
518 /path/to/linked\-worktree abcd1234 [master]
519 /path/to/other\-linked\-worktree 1234abc (detached HEAD)
525 The command also shows annotations for each worktree, according to its state\&. These annotations are:
535 \fBlocked\fR, if the worktree is locked\&.
546 \fBprunable\fR, if the worktree can be pruned via
557 /path/to/linked\-worktree abcd1234 [master]
558 /path/to/locked\-worktree acbd5678 (brancha) locked
559 /path/to/prunable\-worktree 5678abc (detached HEAD) prunable
565 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\&.
571 $ git worktree list \-\-verbose
572 /path/to/linked\-worktree abcd1234 [master]
573 /path/to/locked\-worktree\-no\-reason abcd5678 (detached HEAD) locked
574 /path/to/locked\-worktree\-with\-reason 1234abcd (brancha)
575 locked: worktree path is mounted on a portable device
576 /path/to/prunable\-worktree 5678abc1 (detached HEAD)
577 prunable: gitdir file points to non\-existent location
583 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\&.
584 .SS "Porcelain Format"
586 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:
592 $ git worktree list \-\-porcelain
593 worktree /path/to/bare\-source
596 worktree /path/to/linked\-worktree
597 HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
598 branch refs/heads/master
600 worktree /path/to/other\-linked\-worktree
601 HEAD 1234abc1234abc1234abc1234abc1234abc1234a
604 worktree /path/to/linked\-worktree\-locked\-no\-reason
605 HEAD 5678abc5678abc5678abc5678abc5678abc5678c
606 branch refs/heads/locked\-no\-reason
609 worktree /path/to/linked\-worktree\-locked\-with\-reason
610 HEAD 3456def3456def3456def3456def3456def3456b
611 branch refs/heads/locked\-with\-reason
612 locked reason why is locked
614 worktree /path/to/linked\-worktree\-prunable
615 HEAD 1233def1234def1234def1234def1234def1234b
617 prunable gitdir file points to non\-existent location
623 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:
629 $ git worktree list \-\-porcelain
631 locked "reason\enwhy is locked"
639 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\&.
645 $ git worktree add \-b emergency\-fix \&.\&./temp master
647 # \&.\&.\&. hack hack hack \&.\&.\&.
648 $ git commit \-a \-m \*(Aqemergency fix for boss\*(Aq
650 $ git worktree remove \&.\&./temp
657 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\&.
660 Part of the \fBgit\fR(1) suite