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