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