With separate Git metadata, git worktree list reports a path derived from
the metadata directory as the main worktree, even when core.worktree
specifies the actual checkout. Is that the intended behavior?
System information:
Linux 7.3.6-arch1-1 x86_64
git version 2.56.1
cpu: x86_64
built from commit: 03b41523c576f26ad34085378717a0c1b287470f
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
rust: enabled
feature: fsmonitor--daemon
gettext: enabled
libcurl: 8.23.0
OpenSSL: OpenSSL 4.1.0 23 Oct 2026
zlib-ng: 2.3.3
SHA-1: SHA1_DC
SHA-256: SHA256_BLK
default-ref-format: files
default-hash: sha1
Reproduction:
Run with sh. The script creates two temporary repositories and removes
them on exit. It uses the physical temp path so the assertions also work
when TMPDIR is a symlink.
set -eu
fixture=$(cd "$(mktemp -d)" && pwd -P)
trap 'rm -rf -- "$fixture"' EXIT HUP INT TERM
fixture_git () {
env -i PATH="$PATH" HOME="$fixture" GIT_CONFIG_GLOBAL=/dev/null \
GIT_CONFIG_NOSYSTEM=1 git -c core.hooksPath=/dev/null "$@"
}
seed_fixture () {
fixture_git init -q -b main --separate-git-dir "$2" "$1"
test "$(fixture_git -C "$1" rev-parse --show-toplevel)" = "$1"
test "$(fixture_git -C "$1" rev-parse --absolute-git-dir)" = "$2"
fixture_git -C "$1" -c user.name=Fixture \
-c user.email=fixt…@example.invalid \
commit -q --allow-empty -m fixture
fixture_git -C "$1" worktree add -q --detach "$3"
}
seed_fixture "$fixture/primary" "$fixture/metadata" "$fixture/linked"
fixture_git -C "$fixture/primary" worktree list --porcelain
fixture_git -C "$fixture/linked" worktree list --porcelain
fixture_git -C "$fixture/primary" config core.worktree "$fixture/primary"
fixture_git -C "$fixture/linked" worktree list --porcelain
fixture_git -C "$fixture/metadata" rev-parse --show-toplevel
fixture_git -C "$fixture/linked" worktree list --porcelain -z |
tr '\000' '\n' | sed -n '1p'
variant="$fixture/variant"
mkdir -p "$variant/store"
seed_fixture "$variant/primary" "$variant/store/.git" "$variant/linked"
fixture_git -C "$variant/linked" worktree list --porcelain
fixture_git -C "$variant/store" rev-parse --show-toplevel
fixture_git -C "$variant/primary" config core.worktree "$variant/primary"
fixture_git -C "$variant/linked" worktree list --porcelain
fixture_git -C "$variant/store" rev-parse --show-toplevel
Expected behavior:
The git-worktree manual says the main worktree is listed first. I'd
expect the first record to report the checkout path when it is known from
core.worktree, or the manual to explain how the listed path is chosen
when the Git directory is separate.
Actual behavior:
The first three listings begin with "worktree <fixture>/metadata". The
linked record correctly names "<fixture>/linked". After setting
core.worktree, rev-parse prints "<fixture>/primary" and the first
NUL-delimited record still says "worktree <fixture>/metadata".
In the variant, both listings begin with "worktree <fixture>/variant/store".
Before core.worktree is set, rev-parse run in store also prints
"<fixture>/variant/store". Afterward it prints "<fixture>/variant/primary".
This means that checking whether the listed path is itself a Git
top level can still identify the wrong directory.
Source and prior search:
get_main_worktree() takes the real path of repo_get_common_dir() and
strips any trailing /.git on both master and next. The common directory
has no back-pointer to the main checkout, so Git appears to infer the
path from the metadata location. In these fixtures core.worktree is the
explicit record of the checkout path once configured.
https://github.com/git/git/blob/master/worktree.c
https://github.com/git/git/blob/next/worktree.c
https://git-scm.com/docs/git-worktree
Searches of Git/GitGitGadget issues and pull requests, indexed Git list
discussions, and the 2.54 through 2.56.1 release notes found no matching
correction. PR 2208 concerns repo info path keys:
https://github.com/git/git/pull/2208
The search could have missed an earlier discussion. The branch comparison
is from source inspection. The reproduction above ran on Git 2.56.1.
Should git worktree list honor core.worktree for the main entry when set?
If the entry is intended to keep its current meaning, what command
should consumers use to identify the main checkout in this layout?
Main worktree path reported by git worktree list with --separate-git-dir
- From
- keit…@gmail.com
- Organization
- Usenet Network
- Message-ID
- <CAOJ…@mail.gmail.com>
- Date
- Newsgroups
- linux.kernel.git