git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: `git worktree list` when bare repository is named `.git`

From
Caleb White <cdwhite3@pm.me>
Date
Oct 11, 2024, 03:52 UTC
Message-ID
<D4SO70M9Z1QI.1AC4QF9ZG8T4L@pm.me>
Show 22 quoted lines
> It seems that `.git` is always stripped from the `git worktree list` output (including in `--porcelain` mode). This becomes relevant with bare repositories. Here is a bare repository functioning as expected:
> 
> $ mkdir bare1
> $ cd bare1
> $ git init --bare
> Initialized empty Git repository in /private/tmp/bare1/
> $ git worktree list
> /private/tmp/bare1  (bare)
> 
> But if we create a bare repository in a directory named `.git`, `git worktree list` displays the parent directory as the worktree, even though `git status` doesn't recognize it as a worktree:
> 
> $ mkdir -p bare2/.git
> $ cd bare2/.git
> $ git init --bare
> Initialized empty Git repository in /private/tmp/bare2/.git/
> $ git worktree list
> /private/tmp/bare2  (bare)
> $ cd /tmp/bare2
> $ git status
> fatal: this operation must be run in a work tree
> 
> However, Git _will_ recognize the parent directory (/tmp/bare2) as a Git repository for the purposes of commands like `git rev-parse --git-dir`. I suspect this can be fixed in `get_main_worktree` by only stripping a `.git` suffix from the path if the main worktree is not bare.

The behavior you are seeing is correct and expected (and I use bare repositories with worktrees like this).

The actual bare repository can be the directory, in a .git directory, or in any other directory (as long as it has a .git file that points to the actual bare repository). The directory will still be considered a bare repository. For example:

    mkdir -p repo/.bare
    cd repo/.bare
    Initialized empty Git repository in /private/tmp/repo/.bare/
    git init --bare
    cd ..
    echo 'gitdir: .bare' >.git
    git worktree list
    git worktree add master
    /private/tmp/repo  (bare)
    /private/tmp/repo/master

Bare repositories by definition do not have a working tree, and therefore it is expected that `git status` fails (can only be executed in a worktree) while other commands succeed.

For the purposes of `git worktree list`, the `.git` suffix **should not** be shown. You should never try to use `git worktree list` to get the actual path of the repository, the docs give the following warning:

    See 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.
Best,
Next: Rebecca Turner
Message 1 of 7 in “Re: `git worktree list` when bare repository is named `.git`”
  1. Caleb WhiteOct 11, 2024
  2. Rebecca TurnerOct 11, 2024
  3. Caleb WhiteOct 11, 2024
  4. Rebecca TurnerOct 11, 2024
  5. Caleb WhiteOct 11, 2024
  6. Patrick CallahanOct 11, 2024
  7. Rebecca TurnerOct 21, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.