worktree: move subcommand
[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] [-b <new-branch>] <path> [<branch>]
13 'git worktree list' [--porcelain]
14 'git worktree lock' [--reason <string>] <worktree>
15 'git worktree move' <worktree> <path>
16 'git worktree prune' [-n] [-v] [--expire <expire>]
17 'git worktree unlock' <worktree>
18
19 DESCRIPTION
20 -----------
21
22 Manage multiple working trees attached to the same repository.
23
24 A git repository can support multiple working trees, allowing you to check
25 out more than one branch at a time.  With `git worktree add` a new working
26 tree is associated with the repository.  This new working tree is called a
27 "linked working tree" as opposed to the "main working tree" prepared by "git
28 init" or "git clone".  A repository has one main working tree (if it's not a
29 bare repository) and zero or more linked working trees.
30
31 When you are done with a linked working tree you can simply delete it.
32 The working tree's administrative files in the repository (see
33 "DETAILS" below) will eventually be removed automatically (see
34 `gc.worktreePruneExpire` in linkgit:git-config[1]), or you can run
35 `git worktree prune` in the main or any linked working tree to
36 clean up any stale administrative files.
37
38 To move a linked working tree to a different location, you can use
39 `git worktree move`, which takes care of updating the administrative files
40 as well. If you move one manually, you need to manually update the
41 administrative files so that they do not get pruned automatically. See
42 section "DETAILS" for more information.
43
44 If a linked working tree is stored on a portable device or network share
45 which is not always mounted, you can prevent its administrative files from
46 being pruned by issuing the `git worktree lock` command, optionally
47 specifying `--reason` to explain why the working tree is locked.
48
49 COMMANDS
50 --------
51 add <path> [<branch>]::
52
53 Create `<path>` and checkout `<branch>` into it. The new working directory
54 is linked to the current repository, sharing everything except working
55 directory specific files such as HEAD, index, etc. `-` may also be
56 specified as `<branch>`; it is synonymous with `@{-1}`.
57 +
58 If `<branch>` is omitted and neither `-b` nor `-B` nor `--detach` used,
59 then, as a convenience, a new branch based at HEAD is created automatically,
60 as if `-b $(basename <path>)` was specified.
61
62 list::
63
64 List details of each worktree.  The main worktree is listed first, followed by
65 each of the linked worktrees.  The output details include if the worktree is
66 bare, the revision currently checked out, and the branch currently checked out
67 (or 'detached HEAD' if none).
68
69 lock::
70
71 If a working tree is on a portable device or network share which
72 is not always mounted, lock it to prevent its administrative
73 files from being pruned automatically. This also prevents it from
74 being moved or deleted. Optionally, specify a reason for the lock
75 with `--reason`.
76
77 move::
78
79 Move a worktree from its current location to a new path, updating the
80 administrative files as necessary. The new path must currently reside
81 on the same filesystem as the old one, per the limitations of the
82 `rename(2)` function.
83
84 prune::
85
86 Prune working tree information in $GIT_DIR/worktrees.
87
88 unlock::
89
90 Unlock a working tree, allowing it to be pruned, moved or deleted.
91
92 OPTIONS
93 -------
94
95 -f::
96 --force::
97         By default, `add` refuses to create a new working tree when `<branch>`
98         is already checked out by another working tree. This option overrides
99         that safeguard.
100
101 -b <new-branch>::
102 -B <new-branch>::
103         With `add`, create a new branch named `<new-branch>` starting at
104         `<branch>`, and check out `<new-branch>` into the new working tree.
105         If `<branch>` is omitted, it defaults to HEAD.
106         By default, `-b` refuses to create a new branch if it already
107         exists. `-B` overrides this safeguard, resetting `<new-branch>` to
108         `<branch>`.
109
110 --detach::
111         With `add`, detach HEAD in the new working tree. See "DETACHED HEAD"
112         in linkgit:git-checkout[1].
113
114 --[no-]checkout::
115         By default, `add` checks out `<branch>`, however, `--no-checkout` can
116         be used to suppress checkout in order to make customizations,
117         such as configuring sparse-checkout. See "Sparse checkout"
118         in linkgit:git-read-tree[1].
119
120 -n::
121 --dry-run::
122         With `prune`, do not remove anything; just report what it would
123         remove.
124
125 --porcelain::
126         With `list`, output in an easy-to-parse format for scripts.
127         This format will remain stable across Git versions and regardless of user
128         configuration.  See below for details.
129
130 -v::
131 --verbose::
132         With `prune`, report all removals.
133
134 --expire <time>::
135         With `prune`, only expire unused working trees older than <time>.
136
137 --reason <string>::
138         With `lock`, an explanation why the working tree is locked.
139
140 <worktree>::
141         Working trees can be identified by path, either relative or
142         absolute.
143 +
144 If the last path components in the working tree's path is unique among
145 working trees, it can be used to identify worktrees. For example if
146 you only have two working trees, at "/abc/def/ghi" and "/abc/def/ggg",
147 then "ghi" or "def/ghi" is enough to point to the former working tree.
148
149 DETAILS
150 -------
151 Each linked working tree has a private sub-directory in the repository's
152 $GIT_DIR/worktrees directory.  The private sub-directory's name is usually
153 the base name of the linked working tree's path, possibly appended with a
154 number to make it unique.  For example, when `$GIT_DIR=/path/main/.git` the
155 command `git worktree add /path/other/test-next next` creates the linked
156 working tree in `/path/other/test-next` and also creates a
157 `$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1`
158 if `test-next` is already taken).
159
160 Within a linked working tree, $GIT_DIR is set to point to this private
161 directory (e.g. `/path/main/.git/worktrees/test-next` in the example) and
162 $GIT_COMMON_DIR is set to point back to the main working tree's $GIT_DIR
163 (e.g. `/path/main/.git`). These settings are made in a `.git` file located at
164 the top directory of the linked working tree.
165
166 Path resolution via `git rev-parse --git-path` uses either
167 $GIT_DIR or $GIT_COMMON_DIR depending on the path. For example, in the
168 linked working tree `git rev-parse --git-path HEAD` returns
169 `/path/main/.git/worktrees/test-next/HEAD` (not
170 `/path/other/test-next/.git/HEAD` or `/path/main/.git/HEAD`) while `git
171 rev-parse --git-path refs/heads/master` uses
172 $GIT_COMMON_DIR and returns `/path/main/.git/refs/heads/master`,
173 since refs are shared across all working trees.
174
175 See linkgit:gitrepository-layout[5] for more information. The rule of
176 thumb is do not make any assumption about whether a path belongs to
177 $GIT_DIR or $GIT_COMMON_DIR when you need to directly access something
178 inside $GIT_DIR. Use `git rev-parse --git-path` to get the final path.
179
180 If you move a linked working tree, you need to update the 'gitdir' file
181 in the entry's directory. For example, if a linked working tree is moved
182 to `/newpath/test-next` and its `.git` file points to
183 `/path/main/.git/worktrees/test-next`, then update
184 `/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next`
185 instead.
186
187 To prevent a $GIT_DIR/worktrees entry from being pruned (which
188 can be useful in some situations, such as when the
189 entry's working tree is stored on a portable device), use the
190 `git worktree lock` command, which adds a file named
191 'locked' to the entry's directory. The file contains the reason in
192 plain text. For example, if a linked working tree's `.git` file points
193 to `/path/main/.git/worktrees/test-next` then a file named
194 `/path/main/.git/worktrees/test-next/locked` will prevent the
195 `test-next` entry from being pruned.  See
196 linkgit:gitrepository-layout[5] for details.
197
198 LIST OUTPUT FORMAT
199 ------------------
200 The worktree list command has two output formats.  The default format shows the
201 details on a single line with columns.  For example:
202
203 ------------
204 S git worktree list
205 /path/to/bare-source            (bare)
206 /path/to/linked-worktree        abcd1234 [master]
207 /path/to/other-linked-worktree  1234abc  (detached HEAD)
208 ------------
209
210 Porcelain Format
211 ~~~~~~~~~~~~~~~~
212 The porcelain format has a line per attribute.  Attributes are listed with a
213 label and value separated by a single space.  Boolean attributes (like 'bare'
214 and 'detached') are listed as a label only, and are only present if and only
215 if the value is true.  An empty line indicates the end of a worktree.  For
216 example:
217
218 ------------
219 S git worktree list --porcelain
220 worktree /path/to/bare-source
221 bare
222
223 worktree /path/to/linked-worktree
224 HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
225 branch refs/heads/master
226
227 worktree /path/to/other-linked-worktree
228 HEAD 1234abc1234abc1234abc1234abc1234abc1234a
229 detached
230
231 ------------
232
233 EXAMPLES
234 --------
235 You are in the middle of a refactoring session and your boss comes in and
236 demands that you fix something immediately. You might typically use
237 linkgit:git-stash[1] to store your changes away temporarily, however, your
238 working tree is in such a state of disarray (with new, moved, and removed
239 files, and other bits and pieces strewn around) that you don't want to risk
240 disturbing any of it. Instead, you create a temporary linked working tree to
241 make the emergency fix, remove it when done, and then resume your earlier
242 refactoring session.
243
244 ------------
245 $ git worktree add -b emergency-fix ../temp master
246 $ pushd ../temp
247 # ... hack hack hack ...
248 $ git commit -a -m 'emergency fix for boss'
249 $ popd
250 $ rm -rf ../temp
251 $ git worktree prune
252 ------------
253
254 BUGS
255 ----
256 Multiple checkout in general is still experimental, and the support
257 for submodules is incomplete. It is NOT recommended to make multiple
258 checkouts of a superproject.
259
260 git-worktree could provide more automation for tasks currently
261 performed manually, such as:
262
263 - `remove` to remove a linked working tree and its administrative files (and
264   warn if the working tree is dirty)
265 - `mv` to move or rename a working tree and update its administrative files
266
267 GIT
268 ---
269 Part of the linkgit:git[1] suite