On Sun, Jun 2, 2019 at 2:11 PM Eric Sunshine <sunshine@xxxxxxxxxxxxxx> wrote: > > On Mon, May 27, 2019 at 11:32 AM Ingo Wolf <ingo.wolf@xxxxxx> wrote: > > $ 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. Coming from "git clone" background I would still expect --no-checkout to abort on non-empty directory (i.e. we always start at a good known state). Maybe another option can be used in combination with --no-checkout for this. And do we want the same option in "git clone"? -- Duy