blob: a4fbf5e8386d7d4ae58230b17b534a90461ff421 [file] [log] [blame]
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001git-worktree(1)
2===============
3
4NAME
5----
Michael Haggertybc483282015-07-20 01:29:18 -04006git-worktree - Manage multiple working trees
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07007
8
9SYNOPSIS
10--------
11[verse]
Ævar Arnfjörð Bjarmason5af8b612022-10-13 17:39:02 +020012'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason <string>]]
Jacob Abel7ab89182023-05-17 21:48:47 +000013 [--orphan] [(-b | -B) <new-branch>] <path> [<commit-ish>]
Phillip Woodd97eb302022-03-31 16:21:28 +000014'git worktree list' [-v | --porcelain [-z]]
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +070015'git worktree lock' [--reason <string>] <worktree>
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +070016'git worktree move' <worktree> <new-path>
Nguyễn Thái Ngọc Duy7b722d92016-05-22 16:33:53 +070017'git worktree prune' [-n] [-v] [--expire <expire>]
Stefan Bellerd228eea2018-04-17 11:19:39 -070018'git worktree remove' [-f] <worktree>
Eric Sunshineb214ab52020-08-31 02:57:58 -040019'git worktree repair' [<path>...]
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +070020'git worktree unlock' <worktree>
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070021
22DESCRIPTION
23-----------
24
Michael Haggertybc483282015-07-20 01:29:18 -040025Manage multiple working trees attached to the same repository.
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070026
Eric Sunshine93a36492015-07-06 13:30:40 -040027A git repository can support multiple working trees, allowing you to check
Eric Sunshine4d5a3c52015-07-16 18:09:43 -040028out more than one branch at a time. With `git worktree add` a new working
Derrick Stoleec57bf8c2022-02-23 14:29:13 +000029tree is associated with the repository, along with additional metadata
30that differentiates that working tree from others in the same repository.
31The working tree, along with this metadata, is called a "worktree".
32
33This new worktree is called a "linked worktree" as opposed to the "main
34worktree" prepared by linkgit:git-init[1] or linkgit:git-clone[1].
35A repository has one main worktree (if it's not a bare repository) and
36zero or more linked worktrees. When you are done with a linked worktree,
37remove it with `git worktree remove`.
Eric Sunshine93a36492015-07-06 13:30:40 -040038
Eric Sunshinedccadad2020-09-06 20:02:22 -040039In its simplest form, `git worktree add <path>` automatically creates a
40new branch whose name is the final component of `<path>`, which is
41convenient if you plan to work on a new topic. For instance, `git
42worktree add ../hotfix` creates new branch `hotfix` and checks it out at
Derrick Stoleec57bf8c2022-02-23 14:29:13 +000043path `../hotfix`. To instead work on an existing branch in a new worktree,
44use `git worktree add <path> <branch>`. On the other hand, if you just
45plan to make some experimental changes or do testing without disturbing
46existing development, it is often convenient to create a 'throwaway'
47worktree not associated with any branch. For instance,
48`git worktree add -d <path>` creates a new worktree with a detached `HEAD`
49at the same commit as the current branch.
Eric Sunshinedccadad2020-09-06 20:02:22 -040050
Eric Sunshine3f0b42b2018-04-09 03:33:59 -040051If a working tree is deleted without using `git worktree remove`, then
52its associated administrative files, which reside in the repository
53(see "DETAILS" below), will eventually be removed automatically (see
Eric Sunshine5f5f5532015-07-24 00:00:52 -040054`gc.worktreePruneExpire` in linkgit:git-config[1]), or you can run
Derrick Stoleec57bf8c2022-02-23 14:29:13 +000055`git worktree prune` in the main or any linked worktree to clean up any
56stale administrative files.
Eric Sunshine93a36492015-07-06 13:30:40 -040057
Derrick Stoleec57bf8c2022-02-23 14:29:13 +000058If the working tree for a linked worktree is stored on a portable device
59or network share which is not always mounted, you can prevent its
60administrative files from being pruned by issuing the `git worktree lock`
61command, optionally specifying `--reason` to explain why the worktree is
62locked.
Eric Sunshine93a36492015-07-06 13:30:40 -040063
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070064COMMANDS
65--------
Thomas Gummererc4738ae2017-11-26 19:43:52 +000066add <path> [<commit-ish>]::
Eric Sunshinefc563612015-07-06 13:30:50 -040067
Derrick Stoleec57bf8c2022-02-23 14:29:13 +000068Create a worktree at `<path>` and checkout `<commit-ish>` into it. The new worktree
69is linked to the current repository, sharing everything except per-worktree
70files such as `HEAD`, `index`, etc. As a convenience, `<commit-ish>` may
71be a bare "`-`", which is synonymous with `@{-1}`.
Eric Sunshine1eb07d82015-07-06 13:30:59 -040072+
Eric Sunshinee79e3132020-08-03 20:55:31 -040073If `<commit-ish>` is a branch name (call it `<branch>`) and is not found,
Thomas Gummerer4e853332017-11-26 19:43:54 +000074and neither `-b` nor `-B` nor `--detach` are used, but there does
75exist a tracking branch in exactly one remote (call it `<remote>`)
Eric Sunshine661a5a32018-02-16 15:44:51 -050076with a matching name, treat as equivalent to:
Eric Sunshined023df12018-02-16 15:44:52 -050077+
Thomas Gummerer4e853332017-11-26 19:43:54 +000078------------
79$ git worktree add --track -b <branch> <path> <remote>/<branch>
80------------
81+
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +000082If the branch exists in multiple remotes and one of them is named by
83the `checkout.defaultRemote` configuration variable, we'll use that
84one for the purposes of disambiguation, even if the `<branch>` isn't
85unique across all remotes. Set it to
86e.g. `checkout.defaultRemote=origin` to always checkout remote
87branches from there if `<branch>` is ambiguous but exists on the
Eric Sunshinee79e3132020-08-03 20:55:31 -040088`origin` remote. See also `checkout.defaultRemote` in
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +000089linkgit:git-config[1].
90+
Thomas Gummererc4738ae2017-11-26 19:43:52 +000091If `<commit-ish>` is omitted and neither `-b` nor `-B` nor `--detach` used,
Derrick Stolee59970142022-02-23 14:29:14 +000092then, as a convenience, the new worktree is associated with a branch (call
93it `<branch>`) named after `$(basename <path>)`. If `<branch>` doesn't
94exist, a new branch based on `HEAD` is automatically created as if
95`-b <branch>` was given. If `<branch>` does exist, it will be checked out
96in the new worktree, if it's not checked out anywhere else, otherwise the
97command will refuse to create the worktree (unless `--force` is used).
Jacob Abel128e5492023-05-17 21:48:58 +000098+
99If `<commit-ish>` is omitted, neither `--detach`, or `--orphan` is
100used, and there are no valid local branches (or remote branches if
101`--guess-remote` is specified) then, as a convenience, the new worktree is
102associated with a new orphan branch named `<branch>` (after
103`$(basename <path>)` if neither `-b` or `-B` is used) as if `--orphan` was
104passed to the command. In the event the repository has a remote and
105`--guess-remote` is used, but no remote or local branches exist, then the
106command fails with a warning reminding the user to fetch from their remote
107first (or override by using `-f/--force`).
Eric Sunshinefc563612015-07-06 13:30:50 -0400108
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400109list::
110
Derrick Stolee59970142022-02-23 14:29:14 +0000111List details of each worktree. The main worktree is listed first,
112followed by each of the linked worktrees. The output details include
113whether the worktree is bare, the revision currently checked out, the
Rafael Silva9b19a582021-01-27 09:03:09 +0100114branch currently checked out (or "detached HEAD" if none), "locked" if
Derrick Stolee59970142022-02-23 14:29:14 +0000115the worktree is locked, "prunable" if the worktree can be pruned by the
116`prune` command.
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400117
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700118lock::
119
Derrick Stolee59970142022-02-23 14:29:14 +0000120If a worktree is on a portable device or network share which is not always
121mounted, lock it to prevent its administrative files from being pruned
122automatically. This also prevents it from being moved or deleted.
123Optionally, specify a reason for the lock with `--reason`.
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700124
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700125move::
126
Derrick Stolee59970142022-02-23 14:29:14 +0000127Move a worktree to a new location. Note that the main worktree or linked
128worktrees containing submodules cannot be moved with this command. (The
129`git worktree repair` command, however, can reestablish the connection
130with linked worktrees if you move the main worktree manually.)
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700131
Nguyễn Thái Ngọc Duy7b722d92016-05-22 16:33:53 +0700132prune::
133
Derrick Stolee59970142022-02-23 14:29:14 +0000134Prune worktree information in `$GIT_DIR/worktrees`.
Nguyễn Thái Ngọc Duy7b722d92016-05-22 16:33:53 +0700135
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700136remove::
137
Derrick Stolee59970142022-02-23 14:29:14 +0000138Remove a worktree. Only clean worktrees (no untracked files and no
139modification in tracked files) can be removed. Unclean worktrees or ones
140with submodules can be removed with `--force`. The main worktree cannot be
141removed.
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700142
Eric Sunshineb214ab52020-08-31 02:57:58 -0400143repair [<path>...]::
Eric Sunshinee8e1ff22020-08-27 04:21:25 -0400144
Derrick Stolee59970142022-02-23 14:29:14 +0000145Repair worktree administrative files, if possible, if they have become
146corrupted or outdated due to external factors.
Eric Sunshinebdd1f3e2020-08-31 02:57:57 -0400147+
Derrick Stolee59970142022-02-23 14:29:14 +0000148For instance, if the main worktree (or bare repository) is moved, linked
149worktrees will be unable to locate it. Running `repair` in the main
150worktree will reestablish the connection from linked worktrees back to the
151main worktree.
Eric Sunshineb214ab52020-08-31 02:57:58 -0400152+
Derrick Stolee59970142022-02-23 14:29:14 +0000153Similarly, if the working tree for a linked worktree is moved without
154using `git worktree move`, the main worktree (or bare repository) will be
155unable to locate it. Running `repair` within the recently-moved worktree
156will reestablish the connection. If multiple linked worktrees are moved,
157running `repair` from any worktree with each tree's new `<path>` as an
158argument, will reestablish the connection to all the specified paths.
Eric Sunshinecf76bae2020-12-21 03:16:01 -0500159+
Derrick Stolee59970142022-02-23 14:29:14 +0000160If both the main worktree and linked worktrees have been moved manually,
161then running `repair` in the main worktree and specifying the new `<path>`
162of each linked worktree will reestablish all connections in both
163directions.
Eric Sunshinee8e1ff22020-08-27 04:21:25 -0400164
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700165unlock::
166
Derrick Stolee59970142022-02-23 14:29:14 +0000167Unlock a worktree, allowing it to be pruned, moved or deleted.
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700168
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700169OPTIONS
170-------
171
Eric Sunshinef4325442015-07-06 13:30:51 -0400172-f::
173--force::
Derrick Stolee6036be12022-02-23 14:29:15 +0000174 By default, `add` refuses to create a new worktree when
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700175 `<commit-ish>` is a branch name and is already checked out by
Derrick Stolee6036be12022-02-23 14:29:15 +0000176 another worktree, or if `<path>` is already assigned to some
177 worktree but is missing (for instance, if `<path>` was deleted
Eric Sunshinee19831c2018-08-28 17:20:23 -0400178 manually). This option overrides these safeguards. To add a missing but
Derrick Stolee6036be12022-02-23 14:29:15 +0000179 locked worktree path, specify `--force` twice.
Eric Sunshinee19831c2018-08-28 17:20:23 -0400180+
Derrick Stolee6036be12022-02-23 14:29:15 +0000181`move` refuses to move a locked worktree unless `--force` is specified
182twice. If the destination is already assigned to some other worktree but is
Eric Sunshine810382e2020-06-10 02:30:49 -0400183missing (for instance, if `<new-path>` was deleted manually), then `--force`
Eric Sunshinee79e3132020-08-03 20:55:31 -0400184allows the move to proceed; use `--force` twice if the destination is locked.
Eric Sunshine68a6b3a2018-08-28 17:20:24 -0400185+
Derrick Stolee6036be12022-02-23 14:29:15 +0000186`remove` refuses to remove an unclean worktree unless `--force` is used.
187To remove a locked worktree, specify `--force` twice.
Eric Sunshinef4325442015-07-06 13:30:51 -0400188
Eric Sunshinecbdf60f2015-07-06 13:30:53 -0400189-b <new-branch>::
190-B <new-branch>::
191 With `add`, create a new branch named `<new-branch>` starting at
Derrick Stolee6036be12022-02-23 14:29:15 +0000192 `<commit-ish>`, and check out `<new-branch>` into the new worktree.
Eric Sunshinee79e3132020-08-03 20:55:31 -0400193 If `<commit-ish>` is omitted, it defaults to `HEAD`.
Eric Sunshinecbdf60f2015-07-06 13:30:53 -0400194 By default, `-b` refuses to create a new branch if it already
195 exists. `-B` overrides this safeguard, resetting `<new-branch>` to
Thomas Gummererc4738ae2017-11-26 19:43:52 +0000196 `<commit-ish>`.
Eric Sunshinecbdf60f2015-07-06 13:30:53 -0400197
Eric Sunshinec670aa42020-09-06 20:02:21 -0400198-d::
Eric Sunshine39ecb272015-07-06 13:30:52 -0400199--detach::
Derrick Stolee6036be12022-02-23 14:29:15 +0000200 With `add`, detach `HEAD` in the new worktree. See "DETACHED HEAD"
Michael Haggertybc483282015-07-20 01:29:18 -0400201 in linkgit:git-checkout[1].
Eric Sunshine39ecb272015-07-06 13:30:52 -0400202
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000203--[no-]checkout::
Thomas Gummererc4738ae2017-11-26 19:43:52 +0000204 By default, `add` checks out `<commit-ish>`, however, `--no-checkout` can
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000205 be used to suppress checkout in order to make customizations,
206 such as configuring sparse-checkout. See "Sparse checkout"
207 in linkgit:git-read-tree[1].
208
Thomas Gummerer71d66822017-11-29 20:04:50 +0000209--[no-]guess-remote::
210 With `worktree add <path>`, without `<commit-ish>`, instead
Eric Sunshinee79e3132020-08-03 20:55:31 -0400211 of creating a new branch from `HEAD`, if there exists a tracking
Ralf Thielow50fdf7b2018-01-11 19:18:21 +0100212 branch in exactly one remote matching the basename of `<path>`,
Thomas Gummerer71d66822017-11-29 20:04:50 +0000213 base the new branch on the remote-tracking branch, and mark
214 the remote-tracking branch as "upstream" from the new branch.
Thomas Gummerere92445a2017-11-29 20:04:51 +0000215+
216This can also be set up as the default behaviour by using the
217`worktree.guessRemote` config option.
Thomas Gummerer71d66822017-11-29 20:04:50 +0000218
Thomas Gummerere284e892017-11-26 19:43:53 +0000219--[no-]track::
220 When creating a new branch, if `<commit-ish>` is a branch,
221 mark it as "upstream" from the new branch. This is the
222 default if `<commit-ish>` is a remote-tracking branch. See
Eric Sunshinee79e3132020-08-03 20:55:31 -0400223 `--track` in linkgit:git-branch[1] for details.
Thomas Gummerere284e892017-11-26 19:43:53 +0000224
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700225--lock::
Derrick Stolee6036be12022-02-23 14:29:15 +0000226 Keep the worktree locked after creation. This is the
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700227 equivalent of `git worktree lock` after `git worktree add`,
Eric Sunshineff1ce502020-08-03 20:55:33 -0400228 but without a race condition.
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700229
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700230-n::
231--dry-run::
Eric Sunshine4f098252015-07-06 13:30:39 -0400232 With `prune`, do not remove anything; just report what it would
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700233 remove.
234
Jacob Abel7ab89182023-05-17 21:48:47 +0000235--orphan::
236 With `add`, make the new worktree and index empty, associating
237 the worktree with a new orphan/unborn branch named `<new-branch>`.
238
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400239--porcelain::
240 With `list`, output in an easy-to-parse format for scripts.
241 This format will remain stable across Git versions and regardless of user
Phillip Woodd97eb302022-03-31 16:21:28 +0000242 configuration. It is recommended to combine this with `-z`.
243 See below for details.
244
245-z::
246 Terminate each line with a NUL rather than a newline when
247 `--porcelain` is specified with `list`. This makes it possible
248 to parse the output when a worktree path contains a newline
249 character.
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400250
Elia Pinto371979c2018-08-15 20:56:30 +0000251-q::
252--quiet::
Eric Sunshinee79e3132020-08-03 20:55:31 -0400253 With `add`, suppress feedback messages.
Elia Pinto371979c2018-08-15 20:56:30 +0000254
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700255-v::
256--verbose::
Eric Sunshine4f098252015-07-06 13:30:39 -0400257 With `prune`, report all removals.
Rafael Silva076b4442021-01-27 09:03:10 +0100258+
259With `list`, output additional information about worktrees (see below).
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700260
261--expire <time>::
Derrick Stolee6036be12022-02-23 14:29:15 +0000262 With `prune`, only expire unused worktrees older than `<time>`.
Rafael Silva9b19a582021-01-27 09:03:09 +0100263+
Derrick Stolee6036be12022-02-23 14:29:15 +0000264With `list`, annotate missing worktrees as prunable if they are older than
265`<time>`.
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700266
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700267--reason <string>::
Derrick Stolee6036be12022-02-23 14:29:15 +0000268 With `lock` or with `add --lock`, an explanation why the worktree
269 is locked.
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700270
271<worktree>::
Derrick Stolee6036be12022-02-23 14:29:15 +0000272 Worktrees can be identified by path, either relative or absolute.
Nguyễn Thái Ngọc Duy080739b2016-06-13 19:18:26 +0700273+
Derrick Stolee6036be12022-02-23 14:29:15 +0000274If the last path components in the worktree's path is unique among
275worktrees, it can be used to identify a worktree. For example if you only
276have two worktrees, at `/abc/def/ghi` and `/abc/def/ggg`, then `ghi` or
277`def/ghi` is enough to point to the former worktree.
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700278
Nguyễn Thái Ngọc Duy8aff1a92018-09-29 21:10:23 +0200279REFS
280----
Derrick Stoleea777d4c2022-02-23 14:29:16 +0000281When using multiple worktrees, some refs are shared between all worktrees,
282but others are specific to an individual worktree. One example is `HEAD`,
283which is different for each worktree. This section is about the sharing
284rules and how to access refs of one worktree from another.
Nguyễn Thái Ngọc Duy8aff1a92018-09-29 21:10:23 +0200285
Derrick Stoleea777d4c2022-02-23 14:29:16 +0000286In general, all pseudo refs are per-worktree and all refs starting with
287`refs/` are shared. Pseudo refs are ones like `HEAD` which are directly
288under `$GIT_DIR` instead of inside `$GIT_DIR/refs`. There are exceptions,
289however: refs inside `refs/bisect` and `refs/worktree` are not shared.
Nguyễn Thái Ngọc Duy8aff1a92018-09-29 21:10:23 +0200290
Derrick Stoleea777d4c2022-02-23 14:29:16 +0000291Refs that are per-worktree can still be accessed from another worktree via
292two special paths, `main-worktree` and `worktrees`. The former gives
293access to per-worktree refs of the main worktree, while the latter to all
294linked worktrees.
Nguyễn Thái Ngọc Duy3a3b9d82018-10-21 10:08:54 +0200295
Eric Sunshinee79e3132020-08-03 20:55:31 -0400296For example, `main-worktree/HEAD` or `main-worktree/refs/bisect/good`
Derrick Stoleea777d4c2022-02-23 14:29:16 +0000297resolve to the same value as the main worktree's `HEAD` and
Eric Sunshinee79e3132020-08-03 20:55:31 -0400298`refs/bisect/good` respectively. Similarly, `worktrees/foo/HEAD` or
299`worktrees/bar/refs/bisect/bad` are the same as
300`$GIT_COMMON_DIR/worktrees/foo/HEAD` and
301`$GIT_COMMON_DIR/worktrees/bar/refs/bisect/bad`.
Nguyễn Thái Ngọc Duy3a3b9d82018-10-21 10:08:54 +0200302
Eric Sunshinee79e3132020-08-03 20:55:31 -0400303To access refs, it's best not to look inside `$GIT_DIR` directly. Instead
Nguyễn Thái Ngọc Duy14f74d52018-11-03 06:14:18 +0100304use commands such as linkgit:git-rev-parse[1] or linkgit:git-update-ref[1]
Nguyễn Thái Ngọc Duy8aff1a92018-09-29 21:10:23 +0200305which will handle refs correctly.
306
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200307CONFIGURATION FILE
308------------------
Derrick Stolee7b215822022-02-23 14:29:17 +0000309By default, the repository `config` file is shared across all worktrees.
310If the config variables `core.bare` or `core.worktree` are present in the
311common config file and `extensions.worktreeConfig` is disabled, then they
312will be applied to the main worktree only.
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200313
Derrick Stolee7b215822022-02-23 14:29:17 +0000314In order to have worktree-specific configuration, you can turn on the
315`worktreeConfig` extension, e.g.:
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200316
317------------
318$ git config extensions.worktreeConfig true
319------------
320
321In this mode, specific configuration stays in the path pointed by `git
322rev-parse --git-path config.worktree`. You can add or update
323configuration in this file with `git config --worktree`. Older Git
324versions will refuse to access repositories with this extension.
325
326Note that in this file, the exception for `core.bare` and `core.worktree`
Eric Sunshineff1ce502020-08-03 20:55:33 -0400327is gone. If they exist in `$GIT_DIR/config`, you must move
Derrick Stolee7b215822022-02-23 14:29:17 +0000328them to the `config.worktree` of the main worktree. You may also take this
329opportunity to review and move other configuration that you do not want to
330share to all worktrees:
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200331
Derrick Stolee5c11c0d2022-02-07 21:32:58 +0000332 - `core.worktree` should never be shared.
333
334 - `core.bare` should not be shared if the value is `core.bare=true`.
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200335
Derrick Stolee7b215822022-02-23 14:29:17 +0000336 - `core.sparseCheckout` should not be shared, unless you are sure you
337 always use sparse checkout for all worktrees.
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200338
Derrick Stolee5c11c0d2022-02-07 21:32:58 +0000339See the documentation of `extensions.worktreeConfig` in
340linkgit:git-config[1] for more details.
341
Eric Sunshineaf189b42015-07-06 13:30:42 -0400342DETAILS
343-------
Derrick Stoleef13a1462022-02-23 14:29:18 +0000344Each linked worktree has a private sub-directory in the repository's
Eric Sunshinee79e3132020-08-03 20:55:31 -0400345`$GIT_DIR/worktrees` directory. The private sub-directory's name is usually
Derrick Stoleef13a1462022-02-23 14:29:18 +0000346the base name of the linked worktree's path, possibly appended with a
Eric Sunshineaf189b42015-07-06 13:30:42 -0400347number to make it unique. For example, when `$GIT_DIR=/path/main/.git` the
Eric Sunshine4d5a3c52015-07-16 18:09:43 -0400348command `git worktree add /path/other/test-next next` creates the linked
Derrick Stoleef13a1462022-02-23 14:29:18 +0000349worktree in `/path/other/test-next` and also creates a
Eric Sunshineaf189b42015-07-06 13:30:42 -0400350`$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1`
351if `test-next` is already taken).
352
Derrick Stoleef13a1462022-02-23 14:29:18 +0000353Within a linked worktree, `$GIT_DIR` is set to point to this private
Eric Sunshineaf189b42015-07-06 13:30:42 -0400354directory (e.g. `/path/main/.git/worktrees/test-next` in the example) and
Derrick Stoleef13a1462022-02-23 14:29:18 +0000355`$GIT_COMMON_DIR` is set to point back to the main worktree's `$GIT_DIR`
Eric Sunshineaf189b42015-07-06 13:30:42 -0400356(e.g. `/path/main/.git`). These settings are made in a `.git` file located at
Derrick Stoleef13a1462022-02-23 14:29:18 +0000357the top directory of the linked worktree.
Eric Sunshineaf189b42015-07-06 13:30:42 -0400358
359Path resolution via `git rev-parse --git-path` uses either
Eric Sunshinee79e3132020-08-03 20:55:31 -0400360`$GIT_DIR` or `$GIT_COMMON_DIR` depending on the path. For example, in the
Derrick Stoleef13a1462022-02-23 14:29:18 +0000361linked worktree `git rev-parse --git-path HEAD` returns
Eric Sunshineaf189b42015-07-06 13:30:42 -0400362`/path/main/.git/worktrees/test-next/HEAD` (not
363`/path/other/test-next/.git/HEAD` or `/path/main/.git/HEAD`) while `git
364rev-parse --git-path refs/heads/master` uses
Eric Sunshinee79e3132020-08-03 20:55:31 -0400365`$GIT_COMMON_DIR` and returns `/path/main/.git/refs/heads/master`,
Derrick Stoleef13a1462022-02-23 14:29:18 +0000366since refs are shared across all worktrees, except `refs/bisect` and
Eric Sunshinee79e3132020-08-03 20:55:31 -0400367`refs/worktree`.
Eric Sunshineaf189b42015-07-06 13:30:42 -0400368
369See linkgit:gitrepository-layout[5] for more information. The rule of
370thumb is do not make any assumption about whether a path belongs to
Eric Sunshinee79e3132020-08-03 20:55:31 -0400371`$GIT_DIR` or `$GIT_COMMON_DIR` when you need to directly access something
372inside `$GIT_DIR`. Use `git rev-parse --git-path` to get the final path.
Eric Sunshineaf189b42015-07-06 13:30:42 -0400373
Derrick Stoleef13a1462022-02-23 14:29:18 +0000374If you manually move a linked worktree, you need to update the `gitdir` file
375in the entry's directory. For example, if a linked worktree is moved
Nguyễn Thái Ngọc Duy618244e2016-01-22 15:35:49 +0700376to `/newpath/test-next` and its `.git` file points to
377`/path/main/.git/worktrees/test-next`, then update
378`/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next`
Eric Sunshineb214ab52020-08-31 02:57:58 -0400379instead. Better yet, run `git worktree repair` to reestablish the connection
380automatically.
Nguyễn Thái Ngọc Duy618244e2016-01-22 15:35:49 +0700381
Eric Sunshinee79e3132020-08-03 20:55:31 -0400382To prevent a `$GIT_DIR/worktrees` entry from being pruned (which
Eric Sunshinea8ba5dd2015-07-06 13:30:43 -0400383can be useful in some situations, such as when the
Derrick Stoleef13a1462022-02-23 14:29:18 +0000384entry's worktree is stored on a portable device), use the
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700385`git worktree lock` command, which adds a file named
Eric Sunshinee79e3132020-08-03 20:55:31 -0400386`locked` to the entry's directory. The file contains the reason in
Derrick Stoleef13a1462022-02-23 14:29:18 +0000387plain text. For example, if a linked worktree's `.git` file points
Eric Sunshinea8ba5dd2015-07-06 13:30:43 -0400388to `/path/main/.git/worktrees/test-next` then a file named
389`/path/main/.git/worktrees/test-next/locked` will prevent the
390`test-next` entry from being pruned. See
391linkgit:gitrepository-layout[5] for details.
392
Eric Sunshinee79e3132020-08-03 20:55:31 -0400393When `extensions.worktreeConfig` is enabled, the config file
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200394`.git/worktrees/<id>/config.worktree` is read after `.git/config` is.
395
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400396LIST OUTPUT FORMAT
397------------------
Eric Sunshinee79e3132020-08-03 20:55:31 -0400398The `worktree list` command has two output formats. The default format shows the
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400399details on a single line with columns. For example:
400
401------------
Eric Sunshine22d11a62018-04-09 03:34:00 -0400402$ git worktree list
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400403/path/to/bare-source (bare)
404/path/to/linked-worktree abcd1234 [master]
405/path/to/other-linked-worktree 1234abc (detached HEAD)
406------------
407
Derrick Stolee07d85382022-02-23 14:29:19 +0000408The command also shows annotations for each worktree, according to its state.
Rafael Silva9b19a582021-01-27 09:03:09 +0100409These annotations are:
410
Derrick Stolee07d85382022-02-23 14:29:19 +0000411 * `locked`, if the worktree is locked.
412 * `prunable`, if the worktree can be pruned via `git worktree prune`.
Rafael Silva9b19a582021-01-27 09:03:09 +0100413
414------------
415$ git worktree list
416/path/to/linked-worktree abcd1234 [master]
Andrei Rybak98c76562021-06-25 21:38:51 +0200417/path/to/locked-worktree acbd5678 (brancha) locked
Rafael Silva9b19a582021-01-27 09:03:09 +0100418/path/to/prunable-worktree 5678abc (detached HEAD) prunable
419------------
420
Rafael Silva076b4442021-01-27 09:03:10 +0100421For these annotations, a reason might also be available and this can be
422seen using the verbose mode. The annotation is then moved to the next line
423indented followed by the additional information.
424
425------------
426$ git worktree list --verbose
427/path/to/linked-worktree abcd1234 [master]
428/path/to/locked-worktree-no-reason abcd5678 (detached HEAD) locked
429/path/to/locked-worktree-with-reason 1234abcd (brancha)
Derrick Stolee07d85382022-02-23 14:29:19 +0000430 locked: worktree path is mounted on a portable device
Rafael Silva076b4442021-01-27 09:03:10 +0100431/path/to/prunable-worktree 5678abc1 (detached HEAD)
432 prunable: gitdir file points to non-existent location
433------------
434
435Note that the annotation is moved to the next line if the additional
436information is available, otherwise it stays on the same line as the
Derrick Stolee07d85382022-02-23 14:29:19 +0000437worktree itself.
Rafael Silva076b4442021-01-27 09:03:10 +0100438
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400439Porcelain Format
440~~~~~~~~~~~~~~~~
Phillip Woodd97eb302022-03-31 16:21:28 +0000441The porcelain format has a line per attribute. If `-z` is given then the lines
442are terminated with NUL rather than a newline. Attributes are listed with a
Eric Sunshinee79e3132020-08-03 20:55:31 -0400443label and value separated by a single space. Boolean attributes (like `bare`
Eric Sunshineff1ce502020-08-03 20:55:33 -0400444and `detached`) are listed as a label only, and are present only
Rafael Silva862c7232021-01-27 09:03:08 +0100445if the value is true. Some attributes (like `locked`) can be listed as a label
446only or with a value depending upon whether a reason is available. The first
Derrick Stolee07d85382022-02-23 14:29:19 +0000447attribute of a worktree is always `worktree`, an empty line indicates the
Rafael Silva862c7232021-01-27 09:03:08 +0100448end of the record. For example:
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400449
450------------
Eric Sunshine22d11a62018-04-09 03:34:00 -0400451$ git worktree list --porcelain
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400452worktree /path/to/bare-source
453bare
454
455worktree /path/to/linked-worktree
456HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
457branch refs/heads/master
458
459worktree /path/to/other-linked-worktree
460HEAD 1234abc1234abc1234abc1234abc1234abc1234a
461detached
462
Rafael Silva862c7232021-01-27 09:03:08 +0100463worktree /path/to/linked-worktree-locked-no-reason
464HEAD 5678abc5678abc5678abc5678abc5678abc5678c
465branch refs/heads/locked-no-reason
466locked
467
468worktree /path/to/linked-worktree-locked-with-reason
469HEAD 3456def3456def3456def3456def3456def3456b
470branch refs/heads/locked-with-reason
471locked reason why is locked
472
Rafael Silva9b19a582021-01-27 09:03:09 +0100473worktree /path/to/linked-worktree-prunable
474HEAD 1233def1234def1234def1234def1234def1234b
475detached
476prunable gitdir file points to non-existent location
477
Rafael Silva862c7232021-01-27 09:03:08 +0100478------------
479
Phillip Woodd97eb302022-03-31 16:21:28 +0000480Unless `-z` is used any "unusual" characters in the lock reason such as newlines
Rafael Silva862c7232021-01-27 09:03:08 +0100481are escaped and the entire reason is quoted as explained for the
482configuration variable `core.quotePath` (see linkgit:git-config[1]).
483For Example:
484
485------------
486$ git worktree list --porcelain
487...
488locked "reason\nwhy is locked"
489...
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400490------------
491
Eric Sunshine96454592015-07-06 13:30:44 -0400492EXAMPLES
493--------
494You are in the middle of a refactoring session and your boss comes in and
495demands that you fix something immediately. You might typically use
496linkgit:git-stash[1] to store your changes away temporarily, however, your
Michael Haggertybc483282015-07-20 01:29:18 -0400497working tree is in such a state of disarray (with new, moved, and removed
498files, and other bits and pieces strewn around) that you don't want to risk
Derrick Stolee07d85382022-02-23 14:29:19 +0000499disturbing any of it. Instead, you create a temporary linked worktree to
Eric Sunshine96454592015-07-06 13:30:44 -0400500make the emergency fix, remove it when done, and then resume your earlier
501refactoring session.
502
503------------
Eric Sunshinecbdf60f2015-07-06 13:30:53 -0400504$ git worktree add -b emergency-fix ../temp master
Eric Sunshine96454592015-07-06 13:30:44 -0400505$ pushd ../temp
506# ... hack hack hack ...
507$ git commit -a -m 'emergency fix for boss'
508$ popd
Eric Sunshine3f0b42b2018-04-09 03:33:59 -0400509$ git worktree remove ../temp
Eric Sunshine96454592015-07-06 13:30:44 -0400510------------
511
Eric Sunshine6d3824c2015-07-06 13:30:41 -0400512BUGS
513----
Junio C Hamano18b22db2015-07-16 15:59:48 -0700514Multiple checkout in general is still experimental, and the support
515for submodules is incomplete. It is NOT recommended to make multiple
516checkouts of a superproject.
Eric Sunshine6d3824c2015-07-06 13:30:41 -0400517
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700518GIT
519---
520Part of the linkgit:git[1] suite