Re: [PATCH] worktree: allow sharing root files with linked worktrees
- From
- Bhushan Gaikwad <bhushan@alfredscholar.com>
- Date
- Dec 1, 2026, 04:56 UTC
- Message-ID
- <010601a22ff63cd4-ad6646b6-c34d-414e-ad82-1338e97b7d96-000000@ap-northeast-1.amazonses.com>
- In-Reply-To
- <CALnO6CAX0hNzQ5t99wTwvbrruH5XCu3mbOAM6vdy4ZLMRHWdcg@mail.gmail.com>
Hi Ben,
Thanks for looking at this, and for the pointer to the "Branch Awareness" thread. That thread is the same need from another angle: people carry local, untracked config (settings.py, .env made from .env.example) next to tracked code. includeIf "onbranch:" solves the ignore side, but nothing gets the file into a new worktree.
D. Ben Knoble wrote:
> On what platforms do we not support symlinks?
platform_has_symlinks() is true everywhere except the MinGW build, where it is true only with MSYS=winsymlinks:nativestrict. Separately, clone and init set core.symlinks=false on file systems that cannot store links (FAT and similar). v1 handles this badly: with worktree.sharedFile set and no symlink support, the whole "worktree add" fails and the new worktree is removed. That alone is a good reason to change the default.
> Why reject nested paths?
Only to keep v1 small. With nested paths, a leading directory could be a symlink (tracked or not) and send the file outside the worktree. That is solvable: reject ".." and absolute paths, refuse a symlinked leading directory in the source and the destination, and create missing leading directories. I plan to allow api/.env in v2.
> Why use absolute symlinks?
No security reason. A relative link breaks when "worktree move" changes the relative position of the two worktrees, and an absolute link breaks when the primary worktree moves. Git already lets users pick with worktree.useRelativePaths, so v2 should follow that setting instead of hard-coding absolute links.
> I wonder if instead we could just copy the paths
I agree that copy is the better default. It works on every platform, and it lets a worktree keep its own settings. My plan for v2:
worktree.sharedFile multi-valued path, nested paths allowed worktree.sharedFileMode "copy" (default) or "symlink"
The file is copied or linked after checkout and before the post-checkout hook. An existing path is never overwritten.
> I am somewhat negative on making Git do this automatically
Fair. It is opt-in: nothing happens unless a user sets the key, and it is local config, so it is never forced on anyone through the repo. The alternative today is a post-checkout hook that every user writes again, which has to work out where the primary worktree is and must skip normal branch switches. If the list prefers that route, I am happy to document a hook recipe in git-worktree(1) instead.
Before I send v2, does copy-by-default with an opt-in symlink mode address your concerns? I would also welcome other opinions on the config names.
Thanks, Bhushan