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