1GIT-WORKTREE(1) Git Manual GIT-WORKTREE(1)
2
3
4
6 git-worktree - Manage multiple working trees
7
9 git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]
10 [-b <new-branch>] <path> [<commit-ish>]
11 git worktree list [-v | --porcelain [-z]]
12 git worktree lock [--reason <string>] <worktree>
13 git worktree move <worktree> <new-path>
14 git worktree prune [-n] [-v] [--expire <expire>]
15 git worktree remove [-f] <worktree>
16 git worktree repair [<path>...]
17 git worktree unlock <worktree>
18
20 Manage multiple working trees attached to the same repository.
21
22 A git repository can support multiple working trees, allowing you to
23 check out more than one branch at a time. With git worktree add a new
24 working tree is associated with the repository, along with additional
25 metadata that differentiates that working tree from others in the same
26 repository. The working tree, along with this metadata, is called a
27 "worktree".
28
29 This new worktree is called a "linked worktree" as opposed to the "main
30 worktree" prepared by git-init(1) or git-clone(1). A repository has one
31 main worktree (if it’s not a bare repository) and zero or more linked
32 worktrees. When you are done with a linked worktree, remove it with git
33 worktree remove.
34
35 In its simplest form, git worktree add <path> automatically creates a
36 new branch whose name is the final component of <path>, which is
37 convenient if you plan to work on a new topic. For instance, git
38 worktree add ../hotfix creates new branch hotfix and checks it out at
39 path ../hotfix. To instead work on an existing branch in a new
40 worktree, use git worktree add <path> <branch>. On the other hand, if
41 you just plan to make some experimental changes or do testing without
42 disturbing existing development, it is often convenient to create a
43 throwaway worktree not associated with any branch. For instance, git
44 worktree add -d <path> creates a new worktree with a detached HEAD at
45 the same commit as the current branch.
46
47 If a working tree is deleted without using git worktree remove, then
48 its associated administrative files, which reside in the repository
49 (see "DETAILS" below), will eventually be removed automatically (see
50 gc.worktreePruneExpire in git-config(1)), or you can run git worktree
51 prune in the main or any linked worktree to clean up any stale
52 administrative files.
53
54 If the working tree for a linked worktree is stored on a portable
55 device or network share which is not always mounted, you can prevent
56 its administrative files from being pruned by issuing the git worktree
57 lock command, optionally specifying --reason to explain why the
58 worktree is locked.
59
61 add <path> [<commit-ish>]
62 Create a worktree at <path> and checkout <commit-ish> into it. The
63 new worktree is linked to the current repository, sharing
64 everything except per-worktree files such as HEAD, index, etc. As a
65 convenience, <commit-ish> may be a bare "-", which is synonymous
66 with @{-1}.
67
68 If <commit-ish> is a branch name (call it <branch>) and is not
69 found, and neither -b nor -B nor --detach are used, but there does
70 exist a tracking branch in exactly one remote (call it <remote>)
71 with a matching name, treat as equivalent to:
72
73 $ git worktree add --track -b <branch> <path> <remote>/<branch>
74
75 If the branch exists in multiple remotes and one of them is named
76 by the checkout.defaultRemote configuration variable, we’ll use
77 that one for the purposes of disambiguation, even if the <branch>
78 isn’t unique across all remotes. Set it to e.g.
79 checkout.defaultRemote=origin to always checkout remote branches
80 from there if <branch> is ambiguous but exists on the origin
81 remote. See also checkout.defaultRemote in git-config(1).
82
83 If <commit-ish> is omitted and neither -b nor -B nor --detach used,
84 then, as a convenience, the new worktree is associated with a
85 branch (call it <branch>) named after $(basename <path>). If
86 <branch> doesn’t exist, a new branch based on HEAD is automatically
87 created as if -b <branch> was given. If <branch> does exist, it
88 will be checked out in the new worktree, if it’s not checked out
89 anywhere else, otherwise the command will refuse to create the
90 worktree (unless --force is used).
91
92 list
93 List details of each worktree. The main worktree is listed first,
94 followed by each of the linked worktrees. The output details
95 include whether the worktree is bare, the revision currently
96 checked out, the branch currently checked out (or "detached HEAD"
97 if none), "locked" if the worktree is locked, "prunable" if the
98 worktree can be pruned by the prune command.
99
100 lock
101 If a worktree is on a portable device or network share which is not
102 always mounted, lock it to prevent its administrative files from
103 being pruned automatically. This also prevents it from being moved
104 or deleted. Optionally, specify a reason for the lock with
105 --reason.
106
107 move
108 Move a worktree to a new location. Note that the main worktree or
109 linked worktrees containing submodules cannot be moved with this
110 command. (The git worktree repair command, however, can reestablish
111 the connection with linked worktrees if you move the main worktree
112 manually.)
113
114 prune
115 Prune worktree information in $GIT_DIR/worktrees.
116
117 remove
118 Remove a worktree. Only clean worktrees (no untracked files and no
119 modification in tracked files) can be removed. Unclean worktrees or
120 ones with submodules can be removed with --force. The main worktree
121 cannot be removed.
122
123 repair [<path>...]
124 Repair worktree administrative files, if possible, if they have
125 become corrupted or outdated due to external factors.
126
127 For instance, if the main worktree (or bare repository) is moved,
128 linked worktrees will be unable to locate it. Running repair in the
129 main worktree will reestablish the connection from linked worktrees
130 back to the main worktree.
131
132 Similarly, if the working tree for a linked worktree is moved
133 without using git worktree move, the main worktree (or bare
134 repository) will be unable to locate it. Running repair within the
135 recently-moved worktree will reestablish the connection. If
136 multiple linked worktrees are moved, running repair from any
137 worktree with each tree’s new <path> as an argument, will
138 reestablish the connection to all the specified paths.
139
140 If both the main worktree and linked worktrees have been moved
141 manually, then running repair in the main worktree and specifying
142 the new <path> of each linked worktree will reestablish all
143 connections in both directions.
144
145 unlock
146 Unlock a worktree, allowing it to be pruned, moved or deleted.
147
149 -f, --force
150 By default, add refuses to create a new worktree when <commit-ish>
151 is a branch name and is already checked out by another worktree, or
152 if <path> is already assigned to some worktree but is missing (for
153 instance, if <path> was deleted manually). This option overrides
154 these safeguards. To add a missing but locked worktree path,
155 specify --force twice.
156
157 move refuses to move a locked worktree unless --force is specified
158 twice. If the destination is already assigned to some other
159 worktree but is missing (for instance, if <new-path> was deleted
160 manually), then --force allows the move to proceed; use --force
161 twice if the destination is locked.
162
163 remove refuses to remove an unclean worktree unless --force is
164 used. To remove a locked worktree, specify --force twice.
165
166 -b <new-branch>, -B <new-branch>
167 With add, create a new branch named <new-branch> starting at
168 <commit-ish>, and check out <new-branch> into the new worktree. If
169 <commit-ish> is omitted, it defaults to HEAD. By default, -b
170 refuses to create a new branch if it already exists. -B overrides
171 this safeguard, resetting <new-branch> to <commit-ish>.
172
173 -d, --detach
174 With add, detach HEAD in the new worktree. See "DETACHED HEAD" in
175 git-checkout(1).
176
177 --[no-]checkout
178 By default, add checks out <commit-ish>, however, --no-checkout can
179 be used to suppress checkout in order to make customizations, such
180 as configuring sparse-checkout. See "Sparse checkout" in git-read-
181 tree(1).
182
183 --[no-]guess-remote
184 With worktree add <path>, without <commit-ish>, instead of creating
185 a new branch from HEAD, if there exists a tracking branch in
186 exactly one remote matching the basename of <path>, base the new
187 branch on the remote-tracking branch, and mark the remote-tracking
188 branch as "upstream" from the new branch.
189
190 This can also be set up as the default behaviour by using the
191 worktree.guessRemote config option.
192
193 --[no-]track
194 When creating a new branch, if <commit-ish> is a branch, mark it as
195 "upstream" from the new branch. This is the default if <commit-ish>
196 is a remote-tracking branch. See --track in git-branch(1) for
197 details.
198
199 --lock
200 Keep the worktree locked after creation. This is the equivalent of
201 git worktree lock after git worktree add, but without a race
202 condition.
203
204 -n, --dry-run
205 With prune, do not remove anything; just report what it would
206 remove.
207
208 --porcelain
209 With list, output in an easy-to-parse format for scripts. This
210 format will remain stable across Git versions and regardless of
211 user configuration. It is recommended to combine this with -z. See
212 below for details.
213
214 -z
215 Terminate each line with a NUL rather than a newline when
216 --porcelain is specified with list. This makes it possible to parse
217 the output when a worktree path contains a newline character.
218
219 -q, --quiet
220 With add, suppress feedback messages.
221
222 -v, --verbose
223 With prune, report all removals.
224
225 With list, output additional information about worktrees (see
226 below).
227
228 --expire <time>
229 With prune, only expire unused worktrees older than <time>.
230
231 With list, annotate missing worktrees as prunable if they are older
232 than <time>.
233
234 --reason <string>
235 With lock or with add --lock, an explanation why the worktree is
236 locked.
237
238 <worktree>
239 Worktrees can be identified by path, either relative or absolute.
240
241 If the last path components in the worktree’s path is unique among
242 worktrees, it can be used to identify a worktree. For example if
243 you only have two worktrees, at /abc/def/ghi and /abc/def/ggg, then
244 ghi or def/ghi is enough to point to the former worktree.
245
247 When using multiple worktrees, some refs are shared between all
248 worktrees, but others are specific to an individual worktree. One
249 example is HEAD, which is different for each worktree. This section is
250 about the sharing rules and how to access refs of one worktree from
251 another.
252
253 In general, all pseudo refs are per-worktree and all refs starting with
254 refs/ are shared. Pseudo refs are ones like HEAD which are directly
255 under $GIT_DIR instead of inside $GIT_DIR/refs. There are exceptions,
256 however: refs inside refs/bisect and refs/worktree are not shared.
257
258 Refs that are per-worktree can still be accessed from another worktree
259 via two special paths, main-worktree and worktrees. The former gives
260 access to per-worktree refs of the main worktree, while the latter to
261 all linked worktrees.
262
263 For example, main-worktree/HEAD or main-worktree/refs/bisect/good
264 resolve to the same value as the main worktree’s HEAD and
265 refs/bisect/good respectively. Similarly, worktrees/foo/HEAD or
266 worktrees/bar/refs/bisect/bad are the same as
267 $GIT_COMMON_DIR/worktrees/foo/HEAD and
268 $GIT_COMMON_DIR/worktrees/bar/refs/bisect/bad.
269
270 To access refs, it’s best not to look inside $GIT_DIR directly. Instead
271 use commands such as git-rev-parse(1) or git-update-ref(1) which will
272 handle refs correctly.
273
275 By default, the repository config file is shared across all worktrees.
276 If the config variables core.bare or core.worktree are present in the
277 common config file and extensions.worktreeConfig is disabled, then they
278 will be applied to the main worktree only.
279
280 In order to have worktree-specific configuration, you can turn on the
281 worktreeConfig extension, e.g.:
282
283 $ git config extensions.worktreeConfig true
284
285 In this mode, specific configuration stays in the path pointed by git
286 rev-parse --git-path config.worktree. You can add or update
287 configuration in this file with git config --worktree. Older Git
288 versions will refuse to access repositories with this extension.
289
290 Note that in this file, the exception for core.bare and core.worktree
291 is gone. If they exist in $GIT_DIR/config, you must move them to the
292 config.worktree of the main worktree. You may also take this
293 opportunity to review and move other configuration that you do not want
294 to share to all worktrees:
295
296 • core.worktree should never be shared.
297
298 • core.bare should not be shared if the value is core.bare=true.
299
300 • core.sparseCheckout should not be shared, unless you are sure you
301 always use sparse checkout for all worktrees.
302
303 See the documentation of extensions.worktreeConfig in git-config(1) for
304 more details.
305
307 Each linked worktree has a private sub-directory in the repository’s
308 $GIT_DIR/worktrees directory. The private sub-directory’s name is
309 usually the base name of the linked worktree’s path, possibly appended
310 with a number to make it unique. For example, when
311 $GIT_DIR=/path/main/.git the command git worktree add
312 /path/other/test-next next creates the linked worktree in
313 /path/other/test-next and also creates a $GIT_DIR/worktrees/test-next
314 directory (or $GIT_DIR/worktrees/test-next1 if test-next is already
315 taken).
316
317 Within a linked worktree, $GIT_DIR is set to point to this private
318 directory (e.g. /path/main/.git/worktrees/test-next in the example) and
319 $GIT_COMMON_DIR is set to point back to the main worktree’s $GIT_DIR
320 (e.g. /path/main/.git). These settings are made in a .git file located
321 at the top directory of the linked worktree.
322
323 Path resolution via git rev-parse --git-path uses either $GIT_DIR or
324 $GIT_COMMON_DIR depending on the path. For example, in the linked
325 worktree git rev-parse --git-path HEAD returns
326 /path/main/.git/worktrees/test-next/HEAD (not
327 /path/other/test-next/.git/HEAD or /path/main/.git/HEAD) while git
328 rev-parse --git-path refs/heads/master uses $GIT_COMMON_DIR and returns
329 /path/main/.git/refs/heads/master, since refs are shared across all
330 worktrees, except refs/bisect and refs/worktree.
331
332 See gitrepository-layout(5) for more information. The rule of thumb is
333 do not make any assumption about whether a path belongs to $GIT_DIR or
334 $GIT_COMMON_DIR when you need to directly access something inside
335 $GIT_DIR. Use git rev-parse --git-path to get the final path.
336
337 If you manually move a linked worktree, you need to update the gitdir
338 file in the entry’s directory. For example, if a linked worktree is
339 moved to /newpath/test-next and its .git file points to
340 /path/main/.git/worktrees/test-next, then update
341 /path/main/.git/worktrees/test-next/gitdir to reference
342 /newpath/test-next instead. Better yet, run git worktree repair to
343 reestablish the connection automatically.
344
345 To prevent a $GIT_DIR/worktrees entry from being pruned (which can be
346 useful in some situations, such as when the entry’s worktree is stored
347 on a portable device), use the git worktree lock command, which adds a
348 file named locked to the entry’s directory. The file contains the
349 reason in plain text. For example, if a linked worktree’s .git file
350 points to /path/main/.git/worktrees/test-next then a file named
351 /path/main/.git/worktrees/test-next/locked will prevent the test-next
352 entry from being pruned. See gitrepository-layout(5) for details.
353
354 When extensions.worktreeConfig is enabled, the config file
355 .git/worktrees/<id>/config.worktree is read after .git/config is.
356
358 The worktree list command has two output formats. The default format
359 shows the details on a single line with columns. For example:
360
361 $ git worktree list
362 /path/to/bare-source (bare)
363 /path/to/linked-worktree abcd1234 [master]
364 /path/to/other-linked-worktree 1234abc (detached HEAD)
365
366 The command also shows annotations for each worktree, according to its
367 state. These annotations are:
368
369 • locked, if the worktree is locked.
370
371 • prunable, if the worktree can be pruned via git worktree prune.
372
373 $ git worktree list
374 /path/to/linked-worktree abcd1234 [master]
375 /path/to/locked-worktree acbd5678 (brancha) locked
376 /path/to/prunable-worktree 5678abc (detached HEAD) prunable
377
378 For these annotations, a reason might also be available and this can be
379 seen using the verbose mode. The annotation is then moved to the next
380 line indented followed by the additional information.
381
382 $ git worktree list --verbose
383 /path/to/linked-worktree abcd1234 [master]
384 /path/to/locked-worktree-no-reason abcd5678 (detached HEAD) locked
385 /path/to/locked-worktree-with-reason 1234abcd (brancha)
386 locked: worktree path is mounted on a portable device
387 /path/to/prunable-worktree 5678abc1 (detached HEAD)
388 prunable: gitdir file points to non-existent location
389
390 Note that the annotation is moved to the next line if the additional
391 information is available, otherwise it stays on the same line as the
392 worktree itself.
393
394 Porcelain Format
395 The porcelain format has a line per attribute. If -z is given then the
396 lines are terminated with NUL rather than a newline. Attributes are
397 listed with a label and value separated by a single space. Boolean
398 attributes (like bare and detached) are listed as a label only, and are
399 present only if the value is true. Some attributes (like locked) can be
400 listed as a label only or with a value depending upon whether a reason
401 is available. The first attribute of a worktree is always worktree, an
402 empty line indicates the end of the record. For example:
403
404 $ git worktree list --porcelain
405 worktree /path/to/bare-source
406 bare
407
408 worktree /path/to/linked-worktree
409 HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
410 branch refs/heads/master
411
412 worktree /path/to/other-linked-worktree
413 HEAD 1234abc1234abc1234abc1234abc1234abc1234a
414 detached
415
416 worktree /path/to/linked-worktree-locked-no-reason
417 HEAD 5678abc5678abc5678abc5678abc5678abc5678c
418 branch refs/heads/locked-no-reason
419 locked
420
421 worktree /path/to/linked-worktree-locked-with-reason
422 HEAD 3456def3456def3456def3456def3456def3456b
423 branch refs/heads/locked-with-reason
424 locked reason why is locked
425
426 worktree /path/to/linked-worktree-prunable
427 HEAD 1233def1234def1234def1234def1234def1234b
428 detached
429 prunable gitdir file points to non-existent location
430
431 Unless -z is used any "unusual" characters in the lock reason such as
432 newlines are escaped and the entire reason is quoted as explained for
433 the configuration variable core.quotePath (see git-config(1)). For
434 Example:
435
436 $ git worktree list --porcelain
437 ...
438 locked "reason\nwhy is locked"
439 ...
440
442 You are in the middle of a refactoring session and your boss comes in
443 and demands that you fix something immediately. You might typically use
444 git-stash(1) to store your changes away temporarily, however, your
445 working tree is in such a state of disarray (with new, moved, and
446 removed files, and other bits and pieces strewn around) that you don’t
447 want to risk disturbing any of it. Instead, you create a temporary
448 linked worktree to make the emergency fix, remove it when done, and
449 then resume your earlier refactoring session.
450
451 $ git worktree add -b emergency-fix ../temp master
452 $ pushd ../temp
453 # ... hack hack hack ...
454 $ git commit -a -m 'emergency fix for boss'
455 $ popd
456 $ git worktree remove ../temp
457
459 Multiple checkout in general is still experimental, and the support for
460 submodules is incomplete. It is NOT recommended to make multiple
461 checkouts of a superproject.
462
464 Part of the git(1) suite
465
466
467
468Git 2.39.1 2023-01-13 GIT-WORKTREE(1)