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] [-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, an explanation why the working tree is locked.
225
226 <worktree>
227 Working trees can be identified by path, either relative or
228 absolute.
229
230 If the last path components in the working tree’s path is unique
231 among working trees, it can be used to identify a working tree. For
232 example if you only have two working trees, at /abc/def/ghi and
233 /abc/def/ggg, then ghi or def/ghi is enough to point to the former
234 working tree.
235
237 In multiple working trees, some refs may be shared between all working
238 trees and some refs are local. One example is HEAD which is different
239 for each working tree. This section is about the sharing rules and how
240 to access refs of one working tree from another.
241
242 In general, all pseudo refs are per working tree and all refs starting
243 with refs/ are shared. Pseudo refs are ones like HEAD which are
244 directly under $GIT_DIR instead of inside $GIT_DIR/refs. There are
245 exceptions, however: refs inside refs/bisect and refs/worktree are not
246 shared.
247
248 Refs that are per working tree can still be accessed from another
249 working tree via two special paths, main-worktree and worktrees. The
250 former gives access to per-working tree refs of the main working tree,
251 while the latter to all linked working trees.
252
253 For example, main-worktree/HEAD or main-worktree/refs/bisect/good
254 resolve to the same value as the main working tree’s HEAD and
255 refs/bisect/good respectively. Similarly, worktrees/foo/HEAD or
256 worktrees/bar/refs/bisect/bad are the same as
257 $GIT_COMMON_DIR/worktrees/foo/HEAD and
258 $GIT_COMMON_DIR/worktrees/bar/refs/bisect/bad.
259
260 To access refs, it’s best not to look inside $GIT_DIR directly. Instead
261 use commands such as git-rev-parse(1) or git-update-ref(1) which will
262 handle refs correctly.
263
265 By default, the repository config file is shared across all working
266 trees. If the config variables core.bare or core.worktree are already
267 present in the config file, they will be applied to the main working
268 trees only.
269
270 In order to have configuration specific to working trees, you can turn
271 on the worktreeConfig extension, e.g.:
272
273 $ git config extensions.worktreeConfig true
274
275 In this mode, specific configuration stays in the path pointed by git
276 rev-parse --git-path config.worktree. You can add or update
277 configuration in this file with git config --worktree. Older Git
278 versions will refuse to access repositories with this extension.
279
280 Note that in this file, the exception for core.bare and core.worktree
281 is gone. If they exist in $GIT_DIR/config, you must move them to the
282 config.worktree of the main working tree. You may also take this
283 opportunity to review and move other configuration that you do not want
284 to share to all working trees:
285
286 • core.worktree and core.bare should never be shared
287
288 • core.sparseCheckout is recommended per working tree, unless you are
289 sure you always use sparse checkout for all working trees.
290
292 Each linked working tree has a private sub-directory in the
293 repository’s $GIT_DIR/worktrees directory. The private sub-directory’s
294 name is usually the base name of the linked working tree’s path,
295 possibly appended with a number to make it unique. For example, when
296 $GIT_DIR=/path/main/.git the command git worktree add
297 /path/other/test-next next creates the linked working tree in
298 /path/other/test-next and also creates a $GIT_DIR/worktrees/test-next
299 directory (or $GIT_DIR/worktrees/test-next1 if test-next is already
300 taken).
301
302 Within a linked working tree, $GIT_DIR is set to point to this private
303 directory (e.g. /path/main/.git/worktrees/test-next in the example) and
304 $GIT_COMMON_DIR is set to point back to the main working tree’s
305 $GIT_DIR (e.g. /path/main/.git). These settings are made in a .git file
306 located at the top directory of the linked working tree.
307
308 Path resolution via git rev-parse --git-path uses either $GIT_DIR or
309 $GIT_COMMON_DIR depending on the path. For example, in the linked
310 working tree git rev-parse --git-path HEAD returns
311 /path/main/.git/worktrees/test-next/HEAD (not
312 /path/other/test-next/.git/HEAD or /path/main/.git/HEAD) while git
313 rev-parse --git-path refs/heads/master uses $GIT_COMMON_DIR and returns
314 /path/main/.git/refs/heads/master, since refs are shared across all
315 working trees, except refs/bisect and refs/worktree.
316
317 See gitrepository-layout(5) for more information. The rule of thumb is
318 do not make any assumption about whether a path belongs to $GIT_DIR or
319 $GIT_COMMON_DIR when you need to directly access something inside
320 $GIT_DIR. Use git rev-parse --git-path to get the final path.
321
322 If you manually move a linked working tree, you need to update the
323 gitdir file in the entry’s directory. For example, if a linked working
324 tree is moved to /newpath/test-next and its .git file points to
325 /path/main/.git/worktrees/test-next, then update
326 /path/main/.git/worktrees/test-next/gitdir to reference
327 /newpath/test-next instead. Better yet, run git worktree repair to
328 reestablish the connection automatically.
329
330 To prevent a $GIT_DIR/worktrees entry from being pruned (which can be
331 useful in some situations, such as when the entry’s working tree is
332 stored on a portable device), use the git worktree lock command, which
333 adds a file named locked to the entry’s directory. The file contains
334 the reason in plain text. For example, if a linked working tree’s .git
335 file points to /path/main/.git/worktrees/test-next then a file named
336 /path/main/.git/worktrees/test-next/locked will prevent the test-next
337 entry from being pruned. See gitrepository-layout(5) for details.
338
339 When extensions.worktreeConfig is enabled, the config file
340 .git/worktrees/<id>/config.worktree is read after .git/config is.
341
343 The worktree list command has two output formats. The default format
344 shows the details on a single line with columns. For example:
345
346 $ git worktree list
347 /path/to/bare-source (bare)
348 /path/to/linked-worktree abcd1234 [master]
349 /path/to/other-linked-worktree 1234abc (detached HEAD)
350
351 The command also shows annotations for each working tree, according to
352 its state. These annotations are:
353
354 • locked, if the working tree is locked.
355
356 • prunable, if the working tree can be pruned via git worktree prune.
357
358 $ git worktree list
359 /path/to/linked-worktree abcd1234 [master]
360 /path/to/locked-worktreee acbd5678 (brancha) locked
361 /path/to/prunable-worktree 5678abc (detached HEAD) prunable
362
363 For these annotations, a reason might also be available and this can be
364 seen using the verbose mode. The annotation is then moved to the next
365 line indented followed by the additional information.
366
367 $ git worktree list --verbose
368 /path/to/linked-worktree abcd1234 [master]
369 /path/to/locked-worktree-no-reason abcd5678 (detached HEAD) locked
370 /path/to/locked-worktree-with-reason 1234abcd (brancha)
371 locked: working tree path is mounted on a portable device
372 /path/to/prunable-worktree 5678abc1 (detached HEAD)
373 prunable: gitdir file points to non-existent location
374
375 Note that the annotation is moved to the next line if the additional
376 information is available, otherwise it stays on the same line as the
377 working tree itself.
378
379 Porcelain Format
380 The porcelain format has a line per attribute. Attributes are listed
381 with a label and value separated by a single space. Boolean attributes
382 (like bare and detached) are listed as a label only, and are present
383 only if the value is true. Some attributes (like locked) can be listed
384 as a label only or with a value depending upon whether a reason is
385 available. The first attribute of a working tree is always worktree, an
386 empty line indicates the end of the record. For example:
387
388 $ git worktree list --porcelain
389 worktree /path/to/bare-source
390 bare
391
392 worktree /path/to/linked-worktree
393 HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
394 branch refs/heads/master
395
396 worktree /path/to/other-linked-worktree
397 HEAD 1234abc1234abc1234abc1234abc1234abc1234a
398 detached
399
400 worktree /path/to/linked-worktree-locked-no-reason
401 HEAD 5678abc5678abc5678abc5678abc5678abc5678c
402 branch refs/heads/locked-no-reason
403 locked
404
405 worktree /path/to/linked-worktree-locked-with-reason
406 HEAD 3456def3456def3456def3456def3456def3456b
407 branch refs/heads/locked-with-reason
408 locked reason why is locked
409
410 worktree /path/to/linked-worktree-prunable
411 HEAD 1233def1234def1234def1234def1234def1234b
412 detached
413 prunable gitdir file points to non-existent location
414
415 If the lock reason contains "unusual" characters such as newline, they
416 are escaped and the entire reason is quoted as explained for the
417 configuration variable core.quotePath (see git-config(1)). For Example:
418
419 $ git worktree list --porcelain
420 ...
421 locked "reason\nwhy is locked"
422 ...
423
425 You are in the middle of a refactoring session and your boss comes in
426 and demands that you fix something immediately. You might typically use
427 git-stash(1) to store your changes away temporarily, however, your
428 working tree is in such a state of disarray (with new, moved, and
429 removed files, and other bits and pieces strewn around) that you don’t
430 want to risk disturbing any of it. Instead, you create a temporary
431 linked working tree to make the emergency fix, remove it when done, and
432 then resume your earlier refactoring session.
433
434 $ git worktree add -b emergency-fix ../temp master
435 $ pushd ../temp
436 # ... hack hack hack ...
437 $ git commit -a -m 'emergency fix for boss'
438 $ popd
439 $ git worktree remove ../temp
440
442 Multiple checkout in general is still experimental, and the support for
443 submodules is incomplete. It is NOT recommended to make multiple
444 checkouts of a superproject.
445
447 Part of the git(1) suite
448
449
450
451Git 2.31.1 2021-03-26 GIT-WORKTREE(1)