1git-worktree(1) 2=============== 3 4NAME 5---- 6git-worktree - Manage multiple worktrees 7 8 9SYNOPSIS 10-------- 11[verse] 12'git worktree add' [-f] [--detach] [-b <new-branch>] <path> <branch> 13'git worktree prune' [-n] [-v] [--expire <expire>] 14 15DESCRIPTION 16----------- 17 18Manage multiple worktrees attached to the same repository. 19 20A git repository can support multiple working trees, allowing you to check 21out more than one branch at a time. With `git checkout --to` a new working 22tree is associated with the repository. This new working tree is called a 23"linked working tree" as opposed to the "main working tree" prepared by "git 24init" or "git clone". A repository has one main working tree (if it's not a 25bare repository) and zero or more linked working trees. 26 27When you are done with a linked working tree you can simply delete it. 28The working tree's administrative files in the repository (see 29"DETAILS" below) will eventually be removed automatically (see 30`gc.pruneworktreesexpire` in linkgit::git-config[1]), or you can run 31`git worktree prune` in the main or any linked working tree to 32clean up any stale administrative files. 33 34If you move a linked working directory to another file system, or 35within a file system that does not support hard links, you need to run 36at least one git command inside the linked working directory 37(e.g. `git status`) in order to update its administrative files in the 38repository so that they do not get automatically pruned. 39 40If a linked working tree is stored on a portable device or network share 41which is not always mounted, you can prevent its administrative files from 42being pruned by creating a file named 'lock' alongside the other 43administrative files, optionally containing a plain text reason that 44pruning should be suppressed. See section "DETAILS" for more information. 45 46COMMANDS 47-------- 48add <path> <branch>:: 49 50Create `<path>` and checkout `<branch>` into it. The new working directory 51is linked to the current repository, sharing everything except working 52directory specific files such as HEAD, index, etc. 53 54prune:: 55 56Prune working tree information in $GIT_DIR/worktrees. 57 58OPTIONS 59------- 60 61-f:: 62--force:: 63 By default, `add` refuses to create a new worktree when `<branch>` 64 is already checked out by another worktree. This option overrides 65 that safeguard. 66 67-b <new-branch>:: 68-B <new-branch>:: 69 With `add`, create a new branch named `<new-branch>` starting at 70 `<branch>`, and check out `<new-branch>` into the new worktree. 71 If `<branch>` is omitted, it defaults to HEAD. 72 By default, `-b` refuses to create a new branch if it already 73 exists. `-B` overrides this safeguard, resetting `<new-branch>` to 74 `<branch>`. 75 76--detach:: 77 With `add`, detach HEAD in the new worktree. See "DETACHED HEAD" in 78 linkgit:git-checkout[1]. 79 80-n:: 81--dry-run:: 82 With `prune`, do not remove anything; just report what it would 83 remove. 84 85-v:: 86--verbose:: 87 With `prune`, report all removals. 88 89--expire <time>:: 90 With `prune`, only expire unused worktrees older than <time>. 91 92DETAILS 93------- 94Each linked working tree has a private sub-directory in the repository's 95$GIT_DIR/worktrees directory. The private sub-directory's name is usually 96the base name of the linked working tree's path, possibly appended with a 97number to make it unique. For example, when `$GIT_DIR=/path/main/.git` the 98command `git checkout --to /path/other/test-next next` creates the linked 99working tree in `/path/other/test-next` and also creates a 100`$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1` 101if `test-next` is already taken). 102 103Within a linked working tree, $GIT_DIR is set to point to this private 104directory (e.g. `/path/main/.git/worktrees/test-next` in the example) and 105$GIT_COMMON_DIR is set to point back to the main working tree's $GIT_DIR 106(e.g. `/path/main/.git`). These settings are made in a `.git` file located at 107the top directory of the linked working tree. 108 109Path resolution via `git rev-parse --git-path` uses either 110$GIT_DIR or $GIT_COMMON_DIR depending on the path. For example, in the 111linked working tree `git rev-parse --git-path HEAD` returns 112`/path/main/.git/worktrees/test-next/HEAD` (not 113`/path/other/test-next/.git/HEAD` or `/path/main/.git/HEAD`) while `git 114rev-parse --git-path refs/heads/master` uses 115$GIT_COMMON_DIR and returns `/path/main/.git/refs/heads/master`, 116since refs are shared across all working trees. 117 118See linkgit:gitrepository-layout[5] for more information. The rule of 119thumb is do not make any assumption about whether a path belongs to 120$GIT_DIR or $GIT_COMMON_DIR when you need to directly access something 121inside $GIT_DIR. Use `git rev-parse --git-path` to get the final path. 122 123To prevent a $GIT_DIR/worktrees entry from from being pruned (which 124can be useful in some situations, such as when the 125entry's working tree is stored on a portable device), add a file named 126'locked' to the entry's directory. The file contains the reason in 127plain text. For example, if a linked working tree's `.git` file points 128to `/path/main/.git/worktrees/test-next` then a file named 129`/path/main/.git/worktrees/test-next/locked` will prevent the 130`test-next` entry from being pruned. See 131linkgit:gitrepository-layout[5] for details. 132 133EXAMPLES 134-------- 135You are in the middle of a refactoring session and your boss comes in and 136demands that you fix something immediately. You might typically use 137linkgit:git-stash[1] to store your changes away temporarily, however, your 138worktree is in such a state of disarray (with new, moved, and removed files, 139and other bits and pieces strewn around) that you don't want to risk 140disturbing any of it. Instead, you create a temporary linked worktree to 141make the emergency fix, remove it when done, and then resume your earlier 142refactoring session. 143 144------------ 145$ git worktree add -b emergency-fix ../temp master 146$ pushd ../temp 147# ... hack hack hack ... 148$ git commit -a -m 'emergency fix for boss' 149$ popd 150$ rm -rf ../temp 151$ git worktree prune 152------------ 153 154BUGS 155---- 156Multiple checkout support for submodules is incomplete. It is NOT 157recommended to make multiple checkouts of a superproject. 158 159git-worktree could provide more automation for tasks currently 160performed manually, such as: 161 162- `remove` to remove a linked worktree and its administrative files (and 163 warn if the worktree is dirty) 164- `mv` to move or rename a worktree and update its administrative files 165- `list` to list linked worktrees 166- `lock` to prevent automatic pruning of administrative files (for instance, 167 for a worktree on a portable device) 168 169GIT 170--- 171Part of the linkgit:git[1] suite