Home Explore Blog CI



git

6th chunk of `Documentation/git-worktree.adoc`
2187cfead5f6402d10fcdb663deff0c8b02926a54eb97a9b0000000100000c52
 back to the main worktree's `$GIT_DIR`
(e.g. `/path/main/.git`). These settings are made in a `.git` file located at
the top directory of the linked worktree.

Path resolution via `git rev-parse --git-path` uses either
`$GIT_DIR` or `$GIT_COMMON_DIR` depending on the path. For example, in the
linked worktree `git rev-parse --git-path HEAD` returns
`/path/main/.git/worktrees/test-next/HEAD` (not
`/path/other/test-next/.git/HEAD` or `/path/main/.git/HEAD`) while `git
rev-parse --git-path refs/heads/master` uses
`$GIT_COMMON_DIR` and returns `/path/main/.git/refs/heads/master`,
since refs are shared across all worktrees, except `refs/bisect`,
`refs/worktree` and `refs/rewritten`.

See linkgit:gitrepository-layout[5] for more information. The rule of
thumb is do not make any assumption about whether a path belongs to
`$GIT_DIR` or `$GIT_COMMON_DIR` when you need to directly access something
inside `$GIT_DIR`. Use `git rev-parse --git-path` to get the final path.

If you manually move a linked worktree, you need to update the `gitdir` file
in the entry's directory. For example, if a linked worktree is moved
to `/newpath/test-next` and its `.git` file points to
`/path/main/.git/worktrees/test-next`, then update
`/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next`
instead. Better yet, run `git worktree repair` to reestablish the connection
automatically.

To prevent a `$GIT_DIR/worktrees` entry from being pruned (which
can be useful in some situations, such as when the
entry's worktree is stored on a portable device), use the
`git worktree lock` command, which adds a file named
`locked` to the entry's directory. The file contains the reason in
plain text. For example, if a linked worktree's `.git` file points
to `/path/main/.git/worktrees/test-next` then a file named
`/path/main/.git/worktrees/test-next/locked` will prevent the
`test-next` entry from being pruned.  See
linkgit:gitrepository-layout[5] for details.

When `extensions.worktreeConfig` is enabled, the config file
`.git/worktrees/<id>/config.worktree` is read after `.git/config` is.

LIST OUTPUT FORMAT
------------------
The `worktree list` command has two output formats. The default format shows the
details on a single line with columns.  For example:

------------
$ git worktree list
/path/to/bare-source            (bare)
/path/to/linked-worktree        abcd1234 [master]
/path/to/other-linked-worktree  1234abc  (detached HEAD)
------------

The command also shows annotations for each worktree, according to its state.
These annotations are:

 * `locked`, if the worktree is locked.
 * `prunable`, if the worktree can be pruned via `git worktree prune`.

------------
$ git worktree list
/path/to/linked-worktree    abcd1234 [master]
/path/to/locked-worktree    acbd5678 (brancha) locked
/path/to/prunable-worktree  5678abc  (detached HEAD) prunable
------------

For these annotations, a reason might also be available and this can be
seen using the verbose mode. The annotation is then moved to the next line
indented followed by the additional information.

------------
$ git worktree list --verbose
/path/to/linked-worktree 

Title: Git Worktree Management and Configuration
Summary
Git worktrees have specific configuration and management options, including updating the gitdir file when a worktree is moved, using git worktree repair to reestablish connections, and utilizing git worktree lock to prevent pruning, with various output formats available for the worktree list command, including default and verbose modes with annotations for locked and prunable worktrees