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

Re: worktree add already exists

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jun 2, 2019, 07:07 UTC
Message-ID
<CAPig+cSsNQDe-3GLnmvA+EPior=RSPETJDjkCc7X1qi6gWSGDQ@mail.gmail.com>
In-Reply-To
<ad2ff81a-d75e-1ac4-07d0-c807a2ed9e36@gmx.de>
On Mon, May 27, 2019 at 11:32 AM Ingo Wolf <ingo.wolf@gmx.de> wrote:
Show 10 quoted lines
> $ ls -a barework
> ./  ../  test.txt
> $ git -C bare worktree add --no-checkout ../barework
> Preparing worktree (new branch 'barework')
> fatal: '../barework' already exists
> $ git -C bare branch
>    barework
> * master
> Why this doesn't just work and if not why is barework branch made then,
> why at all ?

It is by design that "git worktree add" (in general) fails if the target directory already exists and is non-empty. This is consistent with how "git clone" behaves. As for why your particular use-case isn't directly supported, it's likely that nobody has yet asked for it, and nobody thought about this particular case when --no-checkout was added (which came some time after basic "git worktree add" itself was implemented).

It is an accident of implementation that the new branch gets created before "git worktree add" errors out due to the existing non-empty directory (and, likely, nobody complained about it, so it went unnoticed). This particular issue probably can be easily fixed now that the logic for checking if the target directory can be a valid worktree has been factored out of the code which actually creates the new directory[1].

[1]: 45059e6468 (worktree: prepare for more checks of whether path can become worktree, 2018-08-28)

> I would like to attach an existing dir to git (make it a workdir) and
> then update the index with git reset and checkin the differences.

I haven't thought through the possible ramifications, but the actual implementation might be as simple as changing this code in builtin/worktree.c:validate_worktree_add():

    if (file_exists(path) && !is_empty_dir(path))
        die(_("'%s' already exists"), path);
to:
    if (opts->checkout && file_exists(path) && !is_empty_dir(path))
        die(_("'%s' already exists"), path);
or something.
Previous: Ingo WolfNext: Duy Nguyen
Message 2 of 7 in “worktree add already exists”
  1. Ingo WolfMay 27, 2019
  2. Eric SunshineJun 2, 2019
  3. Duy NguyenJun 3, 2019
  4. Eric SunshineJun 3, 2019
  5. Duy NguyenJun 5, 2019
  6. Ingo WolfJun 5, 2019
  7. Duy NguyenJun 6, 2019

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.