]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-worktree.txt
worktree: introduce "add" command
[thirdparty/git.git] / Documentation / git-worktree.txt
CommitLineData
df0b6cfb
NTND
1git-worktree(1)
2===============
3
4NAME
5----
6git-worktree - Manage multiple worktrees
7
8
9SYNOPSIS
10--------
11[verse]
fc56361f 12'git worktree add' <path> <branch>
df0b6cfb
NTND
13'git worktree prune' [-n] [-v] [--expire <expire>]
14
15DESCRIPTION
16-----------
17
fc56361f 18Manage multiple worktrees attached to the same repository.
df0b6cfb 19
93a36493
ES
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
93a36493 27When you are done with a linked working tree you can simply delete it.
af189b4c
ES
28The working tree's administrative files in the repository (see
29"DETAILS" below) will eventually be removed automatically (see
93a36493
ES
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
af189b4c 32clean up any stale administrative files.
93a36493
ES
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
af189b4c
ES
37(e.g. `git status`) in order to update its administrative files in the
38repository so that they do not get automatically pruned.
93a36493 39
a8ba5dd7
ES
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.
93a36493 45
df0b6cfb
NTND
46COMMANDS
47--------
fc56361f
ES
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
df0b6cfb
NTND
54prune::
55
56Prune working tree information in $GIT_DIR/worktrees.
57
58OPTIONS
59-------
60
61-n::
62--dry-run::
4f09825e 63 With `prune`, do not remove anything; just report what it would
df0b6cfb
NTND
64 remove.
65
66-v::
67--verbose::
4f09825e 68 With `prune`, report all removals.
df0b6cfb
NTND
69
70--expire <time>::
4f09825e 71 With `prune`, only expire unused worktrees older than <time>.
df0b6cfb 72
af189b4c
ES
73DETAILS
74-------
75Each linked working tree has a private sub-directory in the repository's
76$GIT_DIR/worktrees directory. The private sub-directory's name is usually
77the base name of the linked working tree's path, possibly appended with a
78number to make it unique. For example, when `$GIT_DIR=/path/main/.git` the
79command `git checkout --to /path/other/test-next next` creates the linked
80working tree in `/path/other/test-next` and also creates a
81`$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1`
82if `test-next` is already taken).
83
84Within a linked working tree, $GIT_DIR is set to point to this private
85directory (e.g. `/path/main/.git/worktrees/test-next` in the example) and
86$GIT_COMMON_DIR is set to point back to the main working tree's $GIT_DIR
87(e.g. `/path/main/.git`). These settings are made in a `.git` file located at
88the top directory of the linked working tree.
89
90Path resolution via `git rev-parse --git-path` uses either
91$GIT_DIR or $GIT_COMMON_DIR depending on the path. For example, in the
92linked working tree `git rev-parse --git-path HEAD` returns
93`/path/main/.git/worktrees/test-next/HEAD` (not
94`/path/other/test-next/.git/HEAD` or `/path/main/.git/HEAD`) while `git
95rev-parse --git-path refs/heads/master` uses
96$GIT_COMMON_DIR and returns `/path/main/.git/refs/heads/master`,
97since refs are shared across all working trees.
98
99See linkgit:gitrepository-layout[5] for more information. The rule of
100thumb is do not make any assumption about whether a path belongs to
101$GIT_DIR or $GIT_COMMON_DIR when you need to directly access something
102inside $GIT_DIR. Use `git rev-parse --git-path` to get the final path.
103
a8ba5dd7
ES
104To prevent a $GIT_DIR/worktrees entry from from being pruned (which
105can be useful in some situations, such as when the
106entry's working tree is stored on a portable device), add a file named
107'locked' to the entry's directory. The file contains the reason in
108plain text. For example, if a linked working tree's `.git` file points
109to `/path/main/.git/worktrees/test-next` then a file named
110`/path/main/.git/worktrees/test-next/locked` will prevent the
111`test-next` entry from being pruned. See
112linkgit:gitrepository-layout[5] for details.
113
96454597
ES
114EXAMPLES
115--------
116You are in the middle of a refactoring session and your boss comes in and
117demands that you fix something immediately. You might typically use
118linkgit:git-stash[1] to store your changes away temporarily, however, your
119worktree is in such a state of disarray (with new, moved, and removed files,
120and other bits and pieces strewn around) that you don't want to risk
121disturbing any of it. Instead, you create a temporary linked worktree to
122make the emergency fix, remove it when done, and then resume your earlier
123refactoring session.
124
125------------
126$ git branch emergency-fix master
fc56361f 127$ git worktree add ../temp emergency-fix
96454597
ES
128$ pushd ../temp
129# ... hack hack hack ...
130$ git commit -a -m 'emergency fix for boss'
131$ popd
132$ rm -rf ../temp
133$ git worktree prune
134------------
135
6d3824cf
ES
136BUGS
137----
138Multiple checkout support for submodules is incomplete. It is NOT
139recommended to make multiple checkouts of a superproject.
140
141git-worktree could provide more automation for tasks currently
fc56361f 142performed manually, such as:
6d3824cf 143
6d3824cf
ES
144- `remove` to remove a linked worktree and its administrative files (and
145 warn if the worktree is dirty)
146- `mv` to move or rename a worktree and update its administrative files
147- `list` to list linked worktrees
148- `lock` to prevent automatic pruning of administrative files (for instance,
149 for a worktree on a portable device)
150
df0b6cfb
NTND
151GIT
152---
153Part of the linkgit:git[1] suite