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