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