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), and "locked" if the worktree is locked.
93
94 lock
95 If a working tree is on a portable device or network share which is
96 not always mounted, lock it to prevent its administrative files
97 from being pruned automatically. This also prevents it from being
98 moved or deleted. Optionally, specify a reason for the lock with
99 --reason.
100
101 move
102 Move a working tree to a new location. Note that the main working
103 tree or linked working trees containing submodules cannot be moved
104 with this command. (The git worktree repair command, however, can
105 reestablish the connection with linked working trees if you move
106 the main working tree manually.)
107
108 prune
109 Prune working tree information in $GIT_DIR/worktrees.
110
111 remove
112 Remove a working tree. Only clean working trees (no untracked files
113 and no modification in tracked files) can be removed. Unclean
114 working trees or ones with submodules can be removed with --force.
115 The main working tree cannot be removed.
116
117 repair [<path>...]
118 Repair working tree administrative files, if possible, if they have
119 become corrupted or outdated due to external factors.
120
121 For instance, if the main working tree (or bare repository) is
122 moved, linked working trees will be unable to locate it. Running
123 repair in the main working tree will reestablish the connection
124 from linked working trees back to the main working tree.
125
126 Similarly, if a linked working tree is moved without using git
127 worktree move, the main working tree (or bare repository) will be
128 unable to locate it. Running repair within the recently-moved
129 working tree will reestablish the connection. If multiple linked
130 working trees are moved, running repair from any working tree with
131 each tree’s new <path> as an argument, will reestablish the
132 connection to all the specified paths.
133
134 unlock
135 Unlock a working tree, allowing it to be pruned, moved or deleted.
136
138 -f, --force
139 By default, add refuses to create a new working tree when
140 <commit-ish> is a branch name and is already checked out by another
141 working tree, or if <path> is already assigned to some working tree
142 but is missing (for instance, if <path> was deleted manually). This
143 option overrides these safeguards. To add a missing but locked
144 working tree path, specify --force twice.
145
146 move refuses to move a locked working tree unless --force is
147 specified twice. If the destination is already assigned to some
148 other working tree but is missing (for instance, if <new-path> was
149 deleted manually), then --force allows the move to proceed; use
150 --force twice if the destination is locked.
151
152 remove refuses to remove an unclean working tree unless --force is
153 used. To remove a locked working tree, specify --force twice.
154
155 -b <new-branch>, -B <new-branch>
156 With add, create a new branch named <new-branch> starting at
157 <commit-ish>, and check out <new-branch> into the new working tree.
158 If <commit-ish> is omitted, it defaults to HEAD. By default, -b
159 refuses to create a new branch if it already exists. -B overrides
160 this safeguard, resetting <new-branch> to <commit-ish>.
161
162 -d, --detach
163 With add, detach HEAD in the new working tree. See "DETACHED HEAD"
164 in git-checkout(1).
165
166 --[no-]checkout
167 By default, add checks out <commit-ish>, however, --no-checkout can
168 be used to suppress checkout in order to make customizations, such
169 as configuring sparse-checkout. See "Sparse checkout" in git-read-
170 tree(1).
171
172 --[no-]guess-remote
173 With worktree add <path>, without <commit-ish>, instead of creating
174 a new branch from HEAD, if there exists a tracking branch in
175 exactly one remote matching the basename of <path>, base the new
176 branch on the remote-tracking branch, and mark the remote-tracking
177 branch as "upstream" from the new branch.
178
179 This can also be set up as the default behaviour by using the
180 worktree.guessRemote config option.
181
182 --[no-]track
183 When creating a new branch, if <commit-ish> is a branch, mark it as
184 "upstream" from the new branch. This is the default if <commit-ish>
185 is a remote-tracking branch. See --track in git-branch(1) for
186 details.
187
188 --lock
189 Keep the working tree locked after creation. This is the equivalent
190 of git worktree lock after git worktree add, but without a race
191 condition.
192
193 -n, --dry-run
194 With prune, do not remove anything; just report what it would
195 remove.
196
197 --porcelain
198 With list, output in an easy-to-parse format for scripts. This
199 format will remain stable across Git versions and regardless of
200 user configuration. See below for details.
201
202 -q, --quiet
203 With add, suppress feedback messages.
204
205 -v, --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
219 among working trees, it can be used to identify a working tree. For
220 example if you only have two working trees, at /abc/def/ghi and
221 /abc/def/ggg, then ghi or def/ghi is enough to point to the former
222 working tree.
223
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
227 for each working tree. This section is about the sharing rules and how
228 to access 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 git-rev-parse(1) or git-update-ref(1) which will
250 handle refs correctly.
251
253 By default, the repository config file is shared across all working
254 trees. If the config variables core.bare or core.worktree are already
255 present in the config file, they will be applied to the main working
256 trees only.
257
258 In order to have configuration specific to working trees, you can turn
259 on the worktreeConfig extension, e.g.:
260
261 $ git config extensions.worktreeConfig true
262
263 In this mode, specific configuration stays in the path pointed by git
264 rev-parse --git-path config.worktree. You can add or update
265 configuration in this file with git config --worktree. Older Git
266 versions will refuse to access repositories with this extension.
267
268 Note that in this file, the exception for core.bare and core.worktree
269 is gone. If they exist in $GIT_DIR/config, you must move them to the
270 config.worktree of the main working tree. You may also take this
271 opportunity to review and move other configuration that you do not want
272 to share to all working trees:
273
274 · core.worktree and core.bare should never be shared
275
276 · core.sparseCheckout is recommended per working tree, unless you are
277 sure you always use sparse checkout for all working trees.
278
280 Each linked working tree has a private sub-directory in the
281 repository’s $GIT_DIR/worktrees directory. The private sub-directory’s
282 name is usually the base name of the linked working tree’s path,
283 possibly appended with a number to make it unique. For example, when
284 $GIT_DIR=/path/main/.git the command git worktree add
285 /path/other/test-next next creates the linked working tree in
286 /path/other/test-next and also creates a $GIT_DIR/worktrees/test-next
287 directory (or $GIT_DIR/worktrees/test-next1 if test-next is already
288 taken).
289
290 Within a linked working tree, $GIT_DIR is set to point to this private
291 directory (e.g. /path/main/.git/worktrees/test-next in the example) and
292 $GIT_COMMON_DIR is set to point back to the main working tree’s
293 $GIT_DIR (e.g. /path/main/.git). These settings are made in a .git file
294 located at the top directory of the linked working tree.
295
296 Path resolution via git rev-parse --git-path uses either $GIT_DIR or
297 $GIT_COMMON_DIR depending on the path. For example, in the linked
298 working tree git rev-parse --git-path HEAD returns
299 /path/main/.git/worktrees/test-next/HEAD (not
300 /path/other/test-next/.git/HEAD or /path/main/.git/HEAD) while git
301 rev-parse --git-path refs/heads/master uses $GIT_COMMON_DIR and returns
302 /path/main/.git/refs/heads/master, since refs are shared across all
303 working trees, except refs/bisect and refs/worktree.
304
305 See gitrepository-layout(5) for more information. The rule of thumb is
306 do not make any assumption about whether a path belongs to $GIT_DIR or
307 $GIT_COMMON_DIR when you need to directly access something inside
308 $GIT_DIR. Use git rev-parse --git-path to get the final path.
309
310 If you manually move a linked working tree, you need to update the
311 gitdir file in the entry’s directory. For example, if a linked working
312 tree is moved to /newpath/test-next and its .git file points to
313 /path/main/.git/worktrees/test-next, then update
314 /path/main/.git/worktrees/test-next/gitdir to reference
315 /newpath/test-next instead. Better yet, run git worktree repair to
316 reestablish the connection automatically.
317
318 To prevent a $GIT_DIR/worktrees entry from being pruned (which can be
319 useful in some situations, such as when the entry’s working tree is
320 stored on a portable device), use the git worktree lock command, which
321 adds a file named locked to the entry’s directory. The file contains
322 the reason in plain text. For example, if a linked working tree’s .git
323 file points to /path/main/.git/worktrees/test-next then a file named
324 /path/main/.git/worktrees/test-next/locked will prevent the test-next
325 entry from being pruned. See gitrepository-layout(5) for details.
326
327 When extensions.worktreeConfig is enabled, the config file
328 .git/worktrees/<id>/config.worktree is read after .git/config is.
329
331 The worktree list command has two output formats. The default format
332 shows the details on a single line with columns. For example:
333
334 $ git worktree list
335 /path/to/bare-source (bare)
336 /path/to/linked-worktree abcd1234 [master]
337 /path/to/other-linked-worktree 1234abc (detached HEAD)
338
339 Porcelain Format
340 The porcelain format has a line per attribute. Attributes are listed
341 with a label and value separated by a single space. Boolean attributes
342 (like bare and detached) are listed as a label only, and are present
343 only if the value is true. The first attribute of a working tree is
344 always worktree, an empty line indicates the end of the record. For
345 example:
346
347 $ git worktree list --porcelain
348 worktree /path/to/bare-source
349 bare
350
351 worktree /path/to/linked-worktree
352 HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
353 branch refs/heads/master
354
355 worktree /path/to/other-linked-worktree
356 HEAD 1234abc1234abc1234abc1234abc1234abc1234a
357 detached
358
360 You are in the middle of a refactoring session and your boss comes in
361 and demands that you fix something immediately. You might typically use
362 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
364 removed files, and other bits and pieces strewn around) that you don’t
365 want to risk disturbing any of it. Instead, you create a temporary
366 linked working tree to make the emergency fix, remove it when done, and
367 then resume your earlier refactoring session.
368
369 $ git worktree add -b emergency-fix ../temp master
370 $ pushd ../temp
371 # ... hack hack hack ...
372 $ git commit -a -m 'emergency fix for boss'
373 $ popd
374 $ git worktree remove ../temp
375
377 Multiple checkout in general is still experimental, and the support for
378 submodules is incomplete. It is NOT recommended to make multiple
379 checkouts of a superproject.
380
382 Part of the git(1) suite
383
384
385
386Git 2.30.2 2021-03-08 GIT-WORKTREE(1)