Git fork
at reftables-rust 540 lines 22 kB view raw
1git-worktree(1) 2=============== 3 4NAME 5---- 6git-worktree - Manage multiple working trees 7 8 9SYNOPSIS 10-------- 11[synopsis] 12git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]] 13 [--orphan] [(-b | -B) <new-branch>] <path> [<commit-ish>] 14git worktree list [-v | --porcelain [-z]] 15git worktree lock [--reason <string>] <worktree> 16git worktree move <worktree> <new-path> 17git worktree prune [-n] [-v] [--expire <expire>] 18git worktree remove [-f] <worktree> 19git worktree repair [<path>...] 20git worktree unlock <worktree> 21 22DESCRIPTION 23----------- 24 25Manage multiple working trees attached to the same repository. 26 27A git repository can support multiple working trees, allowing you to check 28out more than one branch at a time. With `git worktree add` a new working 29tree 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`. 38 39In 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 43path `../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. 50 51If 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 54`gc.worktreePruneExpire` in linkgit:git-config[1]), or you can run 55`git worktree prune` in the main or any linked worktree to clean up any 56stale administrative files. 57 58If 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. 63 64COMMANDS 65-------- 66`add <path> [<commit-ish>]`:: 67 68Create 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}`. 72+ 73If _<commit-ish>_ is a branch name (call it _<branch>_) and is not found, 74and neither `-b` nor `-B` nor `--detach` are used, but there does 75exist a tracking branch in exactly one remote (call it _<remote>_) 76with a matching name, treat as equivalent to: 77+ 78------------ 79$ git worktree add --track -b <branch> <path> <remote>/<branch> 80------------ 81+ 82If 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 88`origin` remote. See also `checkout.defaultRemote` in 89linkgit:git-config[1]. 90+ 91If _<commit-ish>_ is omitted and neither `-b` nor `-B` nor `--detach` used, 92then, 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). 98+ 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 unborn 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`). 108 109`list`:: 110 111List 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 114branch currently checked out (or "detached HEAD" if none), "locked" if 115the worktree is locked, "prunable" if the worktree can be pruned by the 116`prune` command. 117 118`lock`:: 119 120If 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`. 124 125`move`:: 126 127Move 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.) 131 132`prune`:: 133 134Prune worktree information in `$GIT_DIR/worktrees`. 135 136`remove`:: 137 138Remove 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. 142 143`repair [<path>...]`:: 144 145Repair worktree administrative files, if possible, if they have become 146corrupted or outdated due to external factors. 147+ 148For 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. 152+ 153Similarly, 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. 159+ 160If both the main worktree and linked worktrees have been moved or copied manually, 161then running `repair` in the main worktree and specifying the new _<path>_ 162of each linked worktree will reestablish all connections in both 163directions. 164 165`unlock`:: 166 167Unlock a worktree, allowing it to be pruned, moved or deleted. 168 169OPTIONS 170------- 171 172`-f`:: 173`--force`:: 174 By default, `add` refuses to create a new worktree when 175 _<commit-ish>_ is a branch name and is already checked out by 176 another worktree, or if _<path>_ is already assigned to some 177 worktree but is missing (for instance, if _<path>_ was deleted 178 manually). This option overrides these safeguards. To add a missing but 179 locked worktree path, specify `--force` twice. 180+ 181`move` refuses to move a locked worktree unless `--force` is specified 182twice. If the destination is already assigned to some other worktree but is 183missing (for instance, if _<new-path>_ was deleted manually), then `--force` 184allows the move to proceed; use `--force` twice if the destination is locked. 185+ 186`remove` refuses to remove an unclean worktree unless `--force` is used. 187To remove a locked worktree, specify `--force` twice. 188 189`-b <new-branch>`:: 190`-B <new-branch>`:: 191 With `add`, create a new branch named _<new-branch>_ starting at 192 _<commit-ish>_, and check out _<new-branch>_ into the new worktree. 193 If _<commit-ish>_ is omitted, it defaults to `HEAD`. 194 By default, `-b` refuses to create a new branch if it already 195 exists. `-B` overrides this safeguard, resetting _<new-branch>_ to 196 _<commit-ish>_. 197 198`-d`:: 199`--detach`:: 200 With `add`, detach `HEAD` in the new worktree. See "DETACHED HEAD" 201 in linkgit:git-checkout[1]. 202 203`--checkout`:: 204`--no-checkout`:: 205 By default, `add` checks out _<commit-ish>_, however, `--no-checkout` can 206 be used to suppress checkout in order to make customizations, 207 such as configuring sparse-checkout. See "Sparse checkout" 208 in linkgit:git-read-tree[1]. 209 210`--guess-remote`:: 211`--no-guess-remote`:: 212 With `worktree add <path>`, without _<commit-ish>_, instead 213 of creating a new branch from `HEAD`, if there exists a tracking 214 branch in exactly one remote matching the basename of _<path>_, 215 base the new branch on the remote-tracking branch, and mark 216 the remote-tracking branch as "upstream" from the new branch. 217+ 218This can also be set up as the default behaviour by using the 219`worktree.guessRemote` config option. 220 221`--relative-paths`:: 222`--no-relative-paths`:: 223 Link worktrees using relative paths or absolute paths (default). 224 Overrides the `worktree.useRelativePaths` config option, see 225 linkgit:git-config[1]. 226+ 227With `repair`, the linking files will be updated if there's an absolute/relative 228mismatch, even if the links are correct. 229 230`--track`:: 231`--no-track`:: 232 When creating a new branch, if _<commit-ish>_ is a branch, 233 mark it as "upstream" from the new branch. This is the 234 default if _<commit-ish>_ is a remote-tracking branch. See 235 `--track` in linkgit:git-branch[1] for details. 236 237`--lock`:: 238 Keep the worktree locked after creation. This is the 239 equivalent of `git worktree lock` after `git worktree add`, 240 but without a race condition. 241 242`-n`:: 243`--dry-run`:: 244 With `prune`, do not remove anything; just report what it would 245 remove. 246 247`--orphan`:: 248 With `add`, make the new worktree and index empty, associating 249 the worktree with a new unborn branch named _<new-branch>_. 250 251`--porcelain`:: 252 With `list`, output in an easy-to-parse format for scripts. 253 This format will remain stable across Git versions and regardless of user 254 configuration. It is recommended to combine this with `-z`. 255 See below for details. 256 257`-z`:: 258 Terminate each line with a _NUL_ rather than a newline when 259 `--porcelain` is specified with `list`. This makes it possible 260 to parse the output when a worktree path contains a newline 261 character. 262 263`-q`:: 264`--quiet`:: 265 With `add`, suppress feedback messages. 266 267`-v`:: 268`--verbose`:: 269 With `prune`, report all removals. 270+ 271With `list`, output additional information about worktrees (see below). 272 273`--expire <time>`:: 274 With `prune`, only expire unused worktrees older than _<time>_. 275+ 276With `list`, annotate missing worktrees as prunable if they are older than 277_<time>_. 278 279`--reason <string>`:: 280 With `lock` or with `add --lock`, an explanation why the worktree 281 is locked. 282 283_<worktree>_:: 284 Worktrees can be identified by path, either relative or absolute. 285+ 286If the last path components in the worktree's path is unique among 287worktrees, it can be used to identify a worktree. For example if you only 288have two worktrees, at `/abc/def/ghi` and `/abc/def/ggg`, then `ghi` or 289`def/ghi` is enough to point to the former worktree. 290 291REFS 292---- 293When using multiple worktrees, some refs are shared between all worktrees, 294but others are specific to an individual worktree. One example is `HEAD`, 295which is different for each worktree. This section is about the sharing 296rules and how to access refs of one worktree from another. 297 298In general, all pseudo refs are per-worktree and all refs starting with 299`refs/` are shared. Pseudo refs are ones like `HEAD` which are directly 300under `$GIT_DIR` instead of inside `$GIT_DIR/refs`. There are exceptions, 301however: refs inside `refs/bisect`, `refs/worktree` and `refs/rewritten` are 302not shared. 303 304Refs that are per-worktree can still be accessed from another worktree via 305two special paths, `main-worktree` and `worktrees`. The former gives 306access to per-worktree refs of the main worktree, while the latter to all 307linked worktrees. 308 309For example, `main-worktree/HEAD` or `main-worktree/refs/bisect/good` 310resolve to the same value as the main worktree's `HEAD` and 311`refs/bisect/good` respectively. Similarly, `worktrees/foo/HEAD` or 312`worktrees/bar/refs/bisect/bad` are the same as 313`$GIT_COMMON_DIR/worktrees/foo/HEAD` and 314`$GIT_COMMON_DIR/worktrees/bar/refs/bisect/bad`. 315 316To access refs, it's best not to look inside `$GIT_DIR` directly. Instead 317use commands such as linkgit:git-rev-parse[1] or linkgit:git-update-ref[1] 318which will handle refs correctly. 319 320CONFIGURATION FILE 321------------------ 322By default, the repository `config` file is shared across all worktrees. 323If the config variables `core.bare` or `core.worktree` are present in the 324common config file and `extensions.worktreeConfig` is disabled, then they 325will be applied to the main worktree only. 326 327In order to have worktree-specific configuration, you can turn on the 328`worktreeConfig` extension, e.g.: 329 330------------ 331$ git config extensions.worktreeConfig true 332------------ 333 334In this mode, specific configuration stays in the path pointed by `git 335rev-parse --git-path config.worktree`. You can add or update 336configuration in this file with `git config --worktree`. Older Git 337versions will refuse to access repositories with this extension. 338 339Note that in this file, the exception for `core.bare` and `core.worktree` 340is gone. If they exist in `$GIT_DIR/config`, you must move 341them to the `config.worktree` of the main worktree. You may also take this 342opportunity to review and move other configuration that you do not want to 343share to all worktrees: 344 345 - `core.worktree` should never be shared. 346 347 - `core.bare` should not be shared if the value is `core.bare=true`. 348 349 - `core.sparseCheckout` should not be shared, unless you are sure you 350 always use sparse checkout for all worktrees. 351 352See the documentation of `extensions.worktreeConfig` in 353linkgit:git-config[1] for more details. 354 355DETAILS 356------- 357Each linked worktree has a private sub-directory in the repository's 358`$GIT_DIR/worktrees` directory. The private sub-directory's name is usually 359the base name of the linked worktree's path, possibly appended with a 360number to make it unique. For example, when `$GIT_DIR=/path/main/.git` the 361command `git worktree add /path/other/test-next next` creates the linked 362worktree in `/path/other/test-next` and also creates a 363`$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1` 364if `test-next` is already taken). 365 366Within a linked worktree, `$GIT_DIR` is set to point to this private 367directory (e.g. `/path/main/.git/worktrees/test-next` in the example) and 368`$GIT_COMMON_DIR` is set to point back to the main worktree's `$GIT_DIR` 369(e.g. `/path/main/.git`). These settings are made in a `.git` file located at 370the top directory of the linked worktree. 371 372Path resolution via `git rev-parse --git-path` uses either 373`$GIT_DIR` or `$GIT_COMMON_DIR` depending on the path. For example, in the 374linked worktree `git rev-parse --git-path HEAD` returns 375`/path/main/.git/worktrees/test-next/HEAD` (not 376`/path/other/test-next/.git/HEAD` or `/path/main/.git/HEAD`) while `git 377rev-parse --git-path refs/heads/master` uses 378`$GIT_COMMON_DIR` and returns `/path/main/.git/refs/heads/master`, 379since refs are shared across all worktrees, except `refs/bisect`, 380`refs/worktree` and `refs/rewritten`. 381 382See linkgit:gitrepository-layout[5] for more information. The rule of 383thumb is do not make any assumption about whether a path belongs to 384`$GIT_DIR` or `$GIT_COMMON_DIR` when you need to directly access something 385inside `$GIT_DIR`. Use `git rev-parse --git-path` to get the final path. 386 387If you manually move a linked worktree, you need to update the `gitdir` file 388in the entry's directory. For example, if a linked worktree is moved 389to `/newpath/test-next` and its `.git` file points to 390`/path/main/.git/worktrees/test-next`, then update 391`/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next` 392instead. Better yet, run `git worktree repair` to reestablish the connection 393automatically. 394 395To prevent a `$GIT_DIR/worktrees` entry from being pruned (which 396can be useful in some situations, such as when the 397entry's worktree is stored on a portable device), use the 398`git worktree lock` command, which adds a file named 399`locked` to the entry's directory. The file contains the reason in 400plain text. For example, if a linked worktree's `.git` file points 401to `/path/main/.git/worktrees/test-next` then a file named 402`/path/main/.git/worktrees/test-next/locked` will prevent the 403`test-next` entry from being pruned. See 404linkgit:gitrepository-layout[5] for details. 405 406When `extensions.worktreeConfig` is enabled, the config file 407`.git/worktrees/<id>/config.worktree` is read after `.git/config` is. 408 409LIST OUTPUT FORMAT 410------------------ 411The `worktree list` command has two output formats. The default format shows the 412details on a single line with columns. For example: 413 414------------ 415$ git worktree list 416/path/to/bare-source (bare) 417/path/to/linked-worktree abcd1234 [master] 418/path/to/other-linked-worktree 1234abc (detached HEAD) 419------------ 420 421The command also shows annotations for each worktree, according to its state. 422These annotations are: 423 424 * `locked`, if the worktree is locked. 425 * `prunable`, if the worktree can be pruned via `git worktree prune`. 426 427------------ 428$ git worktree list 429/path/to/linked-worktree abcd1234 [master] 430/path/to/locked-worktree acbd5678 (brancha) locked 431/path/to/prunable-worktree 5678abc (detached HEAD) prunable 432------------ 433 434For these annotations, a reason might also be available and this can be 435seen using the verbose mode. The annotation is then moved to the next line 436indented followed by the additional information. 437 438------------ 439$ git worktree list --verbose 440/path/to/linked-worktree abcd1234 [master] 441/path/to/locked-worktree-no-reason abcd5678 (detached HEAD) locked 442/path/to/locked-worktree-with-reason 1234abcd (brancha) 443 locked: worktree path is mounted on a portable device 444/path/to/prunable-worktree 5678abc1 (detached HEAD) 445 prunable: gitdir file points to non-existent location 446------------ 447 448Note that the annotation is moved to the next line if the additional 449information is available, otherwise it stays on the same line as the 450worktree itself. 451 452Porcelain Format 453~~~~~~~~~~~~~~~~ 454The porcelain format has a line per attribute. If `-z` is given then the lines 455are terminated with NUL rather than a newline. Attributes are listed with a 456label and value separated by a single space. Boolean attributes (like `bare` 457and `detached`) are listed as a label only, and are present only 458if the value is true. Some attributes (like `locked`) can be listed as a label 459only or with a value depending upon whether a reason is available. The first 460attribute of a worktree is always `worktree`, an empty line indicates the 461end of the record. For example: 462 463------------ 464$ git worktree list --porcelain 465worktree /path/to/bare-source 466bare 467 468worktree /path/to/linked-worktree 469HEAD abcd1234abcd1234abcd1234abcd1234abcd1234 470branch refs/heads/master 471 472worktree /path/to/other-linked-worktree 473HEAD 1234abc1234abc1234abc1234abc1234abc1234a 474detached 475 476worktree /path/to/linked-worktree-locked-no-reason 477HEAD 5678abc5678abc5678abc5678abc5678abc5678c 478branch refs/heads/locked-no-reason 479locked 480 481worktree /path/to/linked-worktree-locked-with-reason 482HEAD 3456def3456def3456def3456def3456def3456b 483branch refs/heads/locked-with-reason 484locked reason why is locked 485 486worktree /path/to/linked-worktree-prunable 487HEAD 1233def1234def1234def1234def1234def1234b 488detached 489prunable gitdir file points to non-existent location 490 491------------ 492 493Unless `-z` is used any "unusual" characters in the lock reason such as newlines 494are escaped and the entire reason is quoted as explained for the 495configuration variable `core.quotePath` (see linkgit:git-config[1]). 496For Example: 497 498------------ 499$ git worktree list --porcelain 500... 501locked "reason\nwhy is locked" 502... 503------------ 504 505EXAMPLES 506-------- 507You are in the middle of a refactoring session and your boss comes in and 508demands that you fix something immediately. You might typically use 509linkgit:git-stash[1] to store your changes away temporarily, however, your 510working tree is in such a state of disarray (with new, moved, and removed 511files, and other bits and pieces strewn around) that you don't want to risk 512disturbing any of it. Instead, you create a temporary linked worktree to 513make the emergency fix, remove it when done, and then resume your earlier 514refactoring session. 515 516------------ 517$ git worktree add -b emergency-fix ../temp master 518$ pushd ../temp 519# ... hack hack hack ... 520$ git commit -a -m 'emergency fix for boss' 521$ popd 522$ git worktree remove ../temp 523------------ 524 525CONFIGURATION 526------------- 527 528include::includes/cmd-config-section-all.adoc[] 529 530include::config/worktree.adoc[] 531 532BUGS 533---- 534Multiple checkout in general is still experimental, and the support 535for submodules is incomplete. It is NOT recommended to make multiple 536checkouts of a superproject. 537 538GIT 539--- 540Part of the linkgit:git[1] suite