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