* [PATCH] worktree: allow sharing root files with linked worktrees
@ 2026-11-26 18:25 Bhushan Gaikwad via GitGitGadget
2026-11-30 16:08 ` D. Ben Knoble
0 siblings, 1 reply; 5+ messages in thread
From: Bhushan Gaikwad via GitGitGadget @ 2026-11-26 18:25 UTC (permalink / raw)
To: git; +Cc: Bhushan Gaikwad
From: Bhushan Gaikwad <bhushan@alfredscholar.com>
Ignored configuration files such as .env often need to remain identical
across linked worktrees. Today users must copy them manually or install a
post-checkout hook, which is easy to forget and can create divergent local
configuration.
Add a multi-valued worktree.sharedFile setting. For each configured
root-level regular file, worktree add creates an absolute symbolic link
from the new worktree to the primary worktree before running the
post-checkout hook.
Reject nested paths and source symlinks, avoid replacing checkout files,
and fail cleanly when symbolic links are unavailable.
Signed-off-by: Bhushan Gaikwad <bhushan@alfredscholar.com>
---
worktree: allow sharing root files with linked worktrees
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2245%2Fbhushan%2Fworktree-link-shared-files-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2245/bhushan/worktree-link-shared-files-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2245
Documentation/config/worktree.adoc | 13 ++++
builtin/worktree.c | 78 ++++++++++++++++++++++++
t/t2400-worktree-add.sh | 95 ++++++++++++++++++++++++++++++
3 files changed, 186 insertions(+)
diff --git a/Documentation/config/worktree.adoc b/Documentation/config/worktree.adoc
index 0930183b91..ddd423caea 100644
--- a/Documentation/config/worktree.adoc
+++ b/Documentation/config/worktree.adoc
@@ -18,3 +18,16 @@
Note that setting `worktree.useRelativePaths` to "`true`" implies enabling the
`extensions.relativeWorktrees` config (see linkgit:git-config[1]),
thus making it incompatible with older versions of Git.
+
+`worktree.sharedFile`::
+ Specify a file at the root of the primary worktree to create as a
+ symbolic link in each new linked worktree. This is useful for ignored
+ configuration files which should be shared by all worktrees. Paths
+ containing directory separators are rejected. The variable may be
+ specified more than once to share multiple files.
++
+The source must be a regular file, not a symbolic link. Missing or non-regular
+sources are skipped with a warning. Git does not overwrite a path populated by
+checkout, and warns instead. Shared files are linked before the `post-checkout`
+hook runs. The symbolic link uses the absolute source path. Worktree creation
+fails when symbolic links are disabled.
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 77ecd0f71f..23af5f0beb 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -131,6 +131,7 @@ static int verbose;
static int guess_remote;
static int use_relative_paths;
static timestamp_t expire;
+static struct string_list shared_files = STRING_LIST_INIT_DUP;
static int git_worktree_config(const char *var, const char *value,
const struct config_context *ctx, void *cb)
@@ -141,6 +142,14 @@ static int git_worktree_config(const char *var, const char *value,
} else if (!strcmp(var, "worktree.userelativepaths")) {
use_relative_paths = git_config_bool(var, value);
return 0;
+ } else if (!strcmp(var, "worktree.sharedfile")) {
+ if (!value || is_absolute_path(value) ||
+ strchr(value, '/') || strchr(value, '\\') ||
+ !verify_path(value, S_IFREG))
+ return error(_("invalid value for worktree.sharedFile: %s"),
+ value ? value : "");
+ string_list_append(&shared_files, value);
+ return 0;
}
return git_default_config(var, value, ctx, cb);
@@ -455,6 +464,73 @@ static void setup_alternate_ref_dir(struct worktree *wt, const char *wt_git_path
strbuf_release(&sb);
}
+static int link_shared_files(const char *path)
+{
+ struct worktree **worktrees;
+ struct string_list_item *item;
+ struct strbuf source = STRBUF_INIT;
+ struct strbuf destination = STRBUF_INIT;
+ struct strbuf primary = STRBUF_INIT;
+ struct strbuf target = STRBUF_INIT;
+ int ret = 0;
+
+ if (!shared_files.nr)
+ return 0;
+ if (!repo_has_symlinks(the_repository))
+ return error(_("worktree.sharedFile requires symbolic link support"));
+
+ worktrees = get_worktrees_without_reading_head(the_repository);
+ if (worktrees[0]->is_bare) {
+ warning(_("not linking shared files: primary worktree is bare"));
+ goto done;
+ }
+
+ strbuf_realpath(&primary, worktrees[0]->path, 1);
+ strbuf_realpath(&target, path, 1);
+
+ for_each_string_list_item(item, &shared_files) {
+ struct stat st;
+
+ strbuf_reset(&source);
+ strbuf_addf(&source, "%s/%s", primary.buf, item->string);
+ if (lstat(source.buf, &st)) {
+ warning_errno(_("not linking shared file '%s'"), source.buf);
+ continue;
+ }
+ if (!S_ISREG(st.st_mode)) {
+ warning(_("not linking shared file '%s': not a regular file"),
+ source.buf);
+ continue;
+ }
+
+ strbuf_reset(&destination);
+ strbuf_addf(&destination, "%s/%s", target.buf, item->string);
+ if (!lstat(destination.buf, &st)) {
+ warning(_("not linking shared file '%s': destination already exists"),
+ item->string);
+ continue;
+ } else if (errno != ENOENT) {
+ ret = error_errno(_("could not inspect shared file destination '%s'"),
+ destination.buf);
+ goto done;
+ }
+
+ if (symlink(source.buf, destination.buf)) {
+ ret = error_errno(_("could not link shared file '%s'"),
+ destination.buf);
+ goto done;
+ }
+ }
+
+done:
+ free_worktrees(worktrees);
+ strbuf_release(&source);
+ strbuf_release(&destination);
+ strbuf_release(&primary);
+ strbuf_release(&target);
+ return ret;
+}
+
static int add_worktree(const char *path, const char *refname,
const struct add_opts *opts)
{
@@ -591,6 +667,8 @@ static int add_worktree(const char *path, const char *refname,
if (opts->checkout &&
(ret = checkout_worktree(opts, &child_env)))
goto done;
+ if ((ret = link_shared_files(path)))
+ goto done;
is_junk = 0;
FREE_AND_NULL(junk_work_tree);
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index bdcca97633..0aa426bbac 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -1288,4 +1288,99 @@ test_expect_success 'relative worktree sets extension config' '
test_cmp_config -C repo true extensions.relativeworktrees
'
+test_expect_success SYMLINKS 'worktree.sharedFile links files from the primary worktree' '
+ test_when_finished "git worktree remove --force shared-file-wt" &&
+ echo shared-secret >.env &&
+ echo local-secret >.env.local &&
+ test_config worktree.sharedFile .env &&
+ git config --add worktree.sharedFile .env.local &&
+ git worktree add --detach shared-file-wt &&
+ test -L shared-file-wt/.env &&
+ test -L shared-file-wt/.env.local &&
+ test "$(readlink shared-file-wt/.env)" = "$(pwd)/.env" &&
+ test_cmp .env shared-file-wt/.env &&
+ test_cmp .env.local shared-file-wt/.env.local
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile uses the primary worktree as source' '
+ test_when_finished "git worktree remove --force shared-source-wt" &&
+ test_when_finished "git worktree remove --force existing-shared-source" &&
+ git worktree add --detach existing-shared-source &&
+ echo primary >.env &&
+ echo linked >existing-shared-source/.env &&
+ test_config worktree.sharedFile .env &&
+ git -C existing-shared-source worktree add --detach ../shared-source-wt &&
+ echo primary >expect &&
+ test_cmp expect shared-source-wt/.env
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile is available to post-checkout hook' '
+ test_when_finished "git worktree remove --force shared-hook-wt" &&
+ test_when_finished "rm -rf .git/hooks" &&
+ echo hook-secret >.env &&
+ test_config worktree.sharedFile .env &&
+ mkdir .git/hooks &&
+ test_hook post-checkout <<-\EOF &&
+ cat .env >hook-env
+ EOF
+ git worktree add --detach shared-hook-wt &&
+ test_cmp .env shared-hook-wt/hook-env
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile rejects paths outside primary worktree' '
+ test_config worktree.sharedFile ../.env &&
+ test_must_fail git worktree add --detach invalid-shared-file 2>err &&
+ test_grep "invalid value for worktree.sharedFile: ../.env" err &&
+ test_path_is_missing invalid-shared-file
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile rejects nested paths' '
+ test_config worktree.sharedFile nested/.env &&
+ test_must_fail git worktree add --detach nested-shared-file 2>err &&
+ test_grep "invalid value for worktree.sharedFile: nested/.env" err &&
+ test_path_is_missing nested-shared-file
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile does not follow source symlinks' '
+ test_when_finished "git worktree remove --force symlink-source-wt" &&
+ test_when_finished "rm -f ../outside-shared-file shared-symlink" &&
+ echo outside >../outside-shared-file &&
+ ln -s ../outside-shared-file shared-symlink &&
+ test_config worktree.sharedFile shared-symlink &&
+ git worktree add --detach symlink-source-wt 2>err &&
+ test_grep "not a regular file" err &&
+ test_path_is_missing symlink-source-wt/shared-symlink
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile requires symlink support' '
+ echo shared-secret >.env &&
+ test_config worktree.sharedFile .env &&
+ test_config core.symlinks false &&
+ test_must_fail git worktree add --detach no-symlink-wt 2>err &&
+ test_grep "worktree.sharedFile requires symbolic link support" err &&
+ test_path_is_missing no-symlink-wt
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile works with --no-checkout' '
+ test_when_finished "git worktree remove --force no-checkout-shared-wt" &&
+ echo shared-secret >.env &&
+ test_config worktree.sharedFile .env &&
+ git worktree add --detach --no-checkout no-checkout-shared-wt &&
+ test -L no-checkout-shared-wt/.env &&
+ test_cmp .env no-checkout-shared-wt/.env
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile does not replace checkout files' '
+ test_when_finished "git worktree remove --force tracked-shared-file" &&
+ echo tracked >shared &&
+ git add shared &&
+ git commit -m shared-file &&
+ echo primary-only >shared &&
+ test_config worktree.sharedFile shared &&
+ git worktree add --detach tracked-shared-file 2>err &&
+ test_grep "not linking shared file.*already exists" err &&
+ test_path_is_file tracked-shared-file/shared &&
+ test ! -L tracked-shared-file/shared
+'
+
test_done
base-commit: c46c1e37724f0478939de636ab8ea5a89086d532
--
gitgitgadget
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] worktree: allow sharing root files with linked worktrees
2026-11-26 18:25 [PATCH] worktree: allow sharing root files with linked worktrees Bhushan Gaikwad via GitGitGadget
@ 2026-11-30 16:08 ` D. Ben Knoble
2026-12-01 4:56 ` Bhushan Gaikwad
2026-12-01 5:31 ` Eric Sunshine
0 siblings, 2 replies; 5+ messages in thread
From: D. Ben Knoble @ 2026-11-30 16:08 UTC (permalink / raw)
To: Bhushan Gaikwad via GitGitGadget; +Cc: git, Bhushan Gaikwad
See also https://lore.kernel.org/git/CAA8YNQAsyUe275DLPRnB4dym5aq9ZyemA39u+HJciKzat5tNQA@mail.gmail.com/,
perhaps?
On Thu, Nov 26, 2026 at 1:25 PM Bhushan Gaikwad via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Bhushan Gaikwad <bhushan@alfredscholar.com>
>
> Ignored configuration files such as .env often need to remain identical
> across linked worktrees. Today users must copy them manually or install a
> post-checkout hook, which is easy to forget and can create divergent local
> configuration.
>
> Add a multi-valued worktree.sharedFile setting. For each configured
> root-level regular file, worktree add creates an absolute symbolic link
> from the new worktree to the primary worktree before running the
> post-checkout hook.
>
> Reject nested paths and source symlinks, avoid replacing checkout files,
> and fail cleanly when symbolic links are unavailable.
The limitations and design choices seem odd to me:
• On what platforms do we not support symlinks? Wikipedia suggests
Windows has equivalents, but our mingw compatibility layer says they
are disabled unless MSYS contains winsymlinks:nativestrict, and of
course I don't know what other platforms do to customize the build.
• Why reject nested paths? A project at work has api/.env{,.example}
which might benefit.
• Why use absolute symlinks? I suppose that might be annoying when a
directory might have a different absolute path on different machines
(due to NFS or something)? Is it a security thing, something about
relative symlinks becoming a nice target for some kind of exploit?
I wonder if instead we could just copy the paths, which lets them
diverge (in case a worktree wants differing settings!), though at risk
of forgetting the divergence in a long-lived worktree?
I am somewhat negative on making Git do this automatically,
personally, especially since the example ".env" is typically ignored
(i.e., not managed by Git at all). But that's just my opinion, so if
the design is fleshed out and others are positive, ok.
--
D. Ben Knoble
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] worktree: allow sharing root files with linked worktrees
2026-11-30 16:08 ` D. Ben Knoble
@ 2026-12-01 4:56 ` Bhushan Gaikwad
2026-12-01 5:31 ` Eric Sunshine
1 sibling, 0 replies; 5+ messages in thread
From: Bhushan Gaikwad @ 2026-12-01 4:56 UTC (permalink / raw)
To: D. Ben Knoble; +Cc: git
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] worktree: allow sharing root files with linked worktrees
2026-11-30 16:08 ` D. Ben Knoble
2026-12-01 4:56 ` Bhushan Gaikwad
@ 2026-12-01 5:31 ` Eric Sunshine
2026-12-01 13:31 ` Patrick Steinhardt
1 sibling, 1 reply; 5+ messages in thread
From: Eric Sunshine @ 2026-12-01 5:31 UTC (permalink / raw)
To: D. Ben Knoble; +Cc: Bhushan Gaikwad via GitGitGadget, git, Bhushan Gaikwad
On Mon, Nov 30, 2026 at 11:08 AM D. Ben Knoble <ben.knoble@gmail.com> wrote:
> On Thu, Nov 26, 2026 at 1:25 PM Bhushan Gaikwad via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
> > Ignored configuration files such as .env often need to remain identical
> > across linked worktrees. Today users must copy them manually or install a
> > post-checkout hook, which is easy to forget and can create divergent local
> > configuration.
> >
> > Add a multi-valued worktree.sharedFile setting. For each configured
> > root-level regular file, worktree add creates an absolute symbolic link
> > from the new worktree to the primary worktree before running the
> > post-checkout hook.
> >
> > Reject nested paths and source symlinks, avoid replacing checkout files,
> > and fail cleanly when symbolic links are unavailable.
>
> The limitations and design choices seem odd to me:
>
> • On what platforms do we not support symlinks? Wikipedia suggests
> Windows has equivalents, but our mingw compatibility layer says they
> are disabled unless MSYS contains winsymlinks:nativestrict, and of
> course I don't know what other platforms do to customize the build.
> • Why reject nested paths? A project at work has api/.env{,.example}
> which might benefit.
> • Why use absolute symlinks? I suppose that might be annoying when a
> directory might have a different absolute path on different machines
> (due to NFS or something)? Is it a security thing, something about
> relative symlinks becoming a nice target for some kind of exploit?
For what it's worth, these are the exact same questions/oddities which
came to my mind upon reading the commit message.
> I am somewhat negative on making Git do this automatically,
> personally, especially since the example ".env" is typically ignored
> (i.e., not managed by Git at all). But that's just my opinion, so if
> the design is fleshed out and others are positive, ok.
I am similarly negative on this proposal. While I understand that it
addresses a real need, I can't help but feel that it is too
specialized and narrowly focused. It feels quite out of place for the
otherwise generic worktree mechanism. I could imagine having a more
favorable reaction to a proposal for a more generally applicable
solution, however, a very general mechanism already exists in the form
of hooks. (And, yes, I am aware that the commit message mentions the
potential downside of employing a hook for this case.)
Bhushan's alternative proposal[*] of instead documenting a hook recipe
in git-worktree(1), on the other hand, seems reasonable. Adding a
sample hook to .git/hooks/ might be even better.
[*]: https://lore.kernel.org/git/010601a22ff63cd4-ad6646b6-c34d-414e-ad82-1338e97b7d96-000000@ap-northeast-1.amazonses.com/
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] worktree: allow sharing root files with linked worktrees
2026-12-01 5:31 ` Eric Sunshine
@ 2026-12-01 13:31 ` Patrick Steinhardt
0 siblings, 0 replies; 5+ messages in thread
From: Patrick Steinhardt @ 2026-12-01 13:31 UTC (permalink / raw)
To: Eric Sunshine
Cc: D. Ben Knoble, Bhushan Gaikwad via GitGitGadget, git,
Bhushan Gaikwad
On Tue, Dec 01, 2026 at 12:31:20AM -0500, Eric Sunshine wrote:
> On Mon, Nov 30, 2026 at 11:08 AM D. Ben Knoble <ben.knoble@gmail.com> wrote:
> > On Thu, Nov 26, 2026 at 1:25 PM Bhushan Gaikwad via GitGitGadget
> > <gitgitgadget@gmail.com> wrote:
> > > Ignored configuration files such as .env often need to remain identical
> > > across linked worktrees. Today users must copy them manually or install a
> > > post-checkout hook, which is easy to forget and can create divergent local
> > > configuration.
> > >
> > > Add a multi-valued worktree.sharedFile setting. For each configured
> > > root-level regular file, worktree add creates an absolute symbolic link
> > > from the new worktree to the primary worktree before running the
> > > post-checkout hook.
> > >
> > > Reject nested paths and source symlinks, avoid replacing checkout files,
> > > and fail cleanly when symbolic links are unavailable.
> >
> > The limitations and design choices seem odd to me:
> >
> > • On what platforms do we not support symlinks? Wikipedia suggests
> > Windows has equivalents, but our mingw compatibility layer says they
> > are disabled unless MSYS contains winsymlinks:nativestrict, and of
> > course I don't know what other platforms do to customize the build.
> > • Why reject nested paths? A project at work has api/.env{,.example}
> > which might benefit.
> > • Why use absolute symlinks? I suppose that might be annoying when a
> > directory might have a different absolute path on different machines
> > (due to NFS or something)? Is it a security thing, something about
> > relative symlinks becoming a nice target for some kind of exploit?
>
> For what it's worth, these are the exact same questions/oddities which
> came to my mind upon reading the commit message.
>
> > I am somewhat negative on making Git do this automatically,
> > personally, especially since the example ".env" is typically ignored
> > (i.e., not managed by Git at all). But that's just my opinion, so if
> > the design is fleshed out and others are positive, ok.
>
> I am similarly negative on this proposal. While I understand that it
> addresses a real need, I can't help but feel that it is too
> specialized and narrowly focused. It feels quite out of place for the
> otherwise generic worktree mechanism. I could imagine having a more
> favorable reaction to a proposal for a more generally applicable
> solution, however, a very general mechanism already exists in the form
> of hooks. (And, yes, I am aware that the commit message mentions the
> potential downside of employing a hook for this case.)
>
> Bhushan's alternative proposal[*] of instead documenting a hook recipe
> in git-worktree(1), on the other hand, seems reasonable. Adding a
> sample hook to .git/hooks/ might be even better.
>
> [*]: https://lore.kernel.org/git/010601a22ff63cd4-ad6646b6-c34d-414e-ad82-1338e97b7d96-000000@ap-northeast-1.amazonses.com/
Sorry for repeating what you're saying, but my immediate reaction was
also that this is way too niche of a use case, and that adding a hook
would be the better solution to this.
But the other question is whether you really need a hook in order to
achieve this. In other words, isn't it sufficient to have a small script
that wraps git-worktree(1) to create the worktree and copy over the
files? We typically only add hooks when there really is no other way to
achieve something, e.g. when it happens in the middle of an operation
performed by Git. But that's not really the case here, as it can be
trivially achieved by such a small wrapper script.
Thanks!
Patrick
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-01 13:31 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 18:25 [PATCH] worktree: allow sharing root files with linked worktrees Bhushan Gaikwad via GitGitGadget
2026-11-30 16:08 ` D. Ben Knoble
2026-12-01 4:56 ` Bhushan Gaikwad
2026-12-01 5:31 ` Eric Sunshine
2026-12-01 13:31 ` Patrick Steinhardt
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.