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

[PATCH] worktree: allow sharing root files with linked worktrees

From
BGBhushan Gaikwad via GitGitGadget <gitgitgadget@gmail.com>
Date
Nov 26, 2026, 18:25 UTC
Message-ID
<pull.2245.git.1795717517830.gitgitgadget@gmail.com>
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
Next: D. Ben Knoble
Message 1 of 5 in “worktree: allow sharing root files with linked worktrees”
  1. worktree: allow sharing root files with linked worktreesBhushan Gaikwad via GitGitGadget, Nov 26, 2026
  2. D. Ben KnobleNov 30, 2026
  3. Bhushan GaikwadDec 1, 2026
  4. Eric SunshineDec 1, 2026
  5. Patrick SteinhardtDec 1, 2026

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.