Home Explore Blog CI



git

5th chunk of `Documentation/git-worktree.adoc`
32ec24c8e52f0c36ada808578f988497ce3f3315e8cc01a50000000100000fa1
 `$GIT_DIR/refs`. There are exceptions,
however: refs inside `refs/bisect`, `refs/worktree` and `refs/rewritten` are
not shared.

Refs that are per-worktree can still be accessed from another worktree via
two special paths, `main-worktree` and `worktrees`. The former gives
access to per-worktree refs of the main worktree, while the latter to all
linked worktrees.

For example, `main-worktree/HEAD` or `main-worktree/refs/bisect/good`
resolve to the same value as the main worktree's `HEAD` and
`refs/bisect/good` respectively. Similarly, `worktrees/foo/HEAD` or
`worktrees/bar/refs/bisect/bad` are the same as
`$GIT_COMMON_DIR/worktrees/foo/HEAD` and
`$GIT_COMMON_DIR/worktrees/bar/refs/bisect/bad`.

To access refs, it's best not to look inside `$GIT_DIR` directly. Instead
use commands such as linkgit:git-rev-parse[1] or linkgit:git-update-ref[1]
which will handle refs correctly.

CONFIGURATION FILE
------------------
By default, the repository `config` file is shared across all worktrees.
If the config variables `core.bare` or `core.worktree` are present in the
common config file and `extensions.worktreeConfig` is disabled, then they
will be applied to the main worktree only.

In order to have worktree-specific configuration, you can turn on the
`worktreeConfig` extension, e.g.:

------------
$ git config extensions.worktreeConfig true
------------

In this mode, specific configuration stays in the path pointed by `git
rev-parse --git-path config.worktree`. You can add or update
configuration in this file with `git config --worktree`. Older Git
versions will refuse to access repositories with this extension.

Note that in this file, the exception for `core.bare` and `core.worktree`
is gone. If they exist in `$GIT_DIR/config`, you must move
them to the `config.worktree` of the main worktree. You may also take this
opportunity to review and move other configuration that you do not want to
share to all worktrees:

 - `core.worktree` should never be shared.

 - `core.bare` should not be shared if the value is `core.bare=true`.

 - `core.sparseCheckout` should not be shared, unless you are sure you
   always use sparse checkout for all worktrees.

See the documentation of `extensions.worktreeConfig` in
linkgit:git-config[1] for more details.

DETAILS
-------
Each linked worktree has a private sub-directory in the repository's
`$GIT_DIR/worktrees` directory.  The private sub-directory's name is usually
the base name of the linked worktree's path, possibly appended with a
number to make it unique.  For example, when `$GIT_DIR=/path/main/.git` the
command `git worktree add /path/other/test-next next` creates the linked
worktree in `/path/other/test-next` and also creates a
`$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1`
if `test-next` is already taken).

Within a linked worktree, `$GIT_DIR` is set to point to this private
directory (e.g. `/path/main/.git/worktrees/test-next` in the example) and
`$GIT_COMMON_DIR` is set to point 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

Title: Git Worktree Configuration and Directory Structure
Summary
The configuration file for a Git repository can be shared across all worktrees, but specific configuration can be enabled with the worktreeConfig extension, allowing for worktree-specific configuration files, and each linked worktree has a private sub-directory in the repository's $GIT_DIR/worktrees directory, with its own $GIT_DIR and $GIT_COMMON_DIR settings, which can be accessed using git rev-parse --git-path