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