Skip to content

worktree: add --recurse-submodules support to git worktree add#2112

Open
Ergus wants to merge 4 commits into
gitgitgadget:masterfrom
Ergus:worktree-recurse-submodules
Open

worktree: add --recurse-submodules support to git worktree add#2112
Ergus wants to merge 4 commits into
gitgitgadget:masterfrom
Ergus:worktree-recurse-submodules

Conversation

@Ergus
Copy link
Copy Markdown

@Ergus Ergus commented May 12, 2026

"git worktree add" leaves submodules uninitialised in the new working
tree. This series adds --recurse-submodules to initialise them
automatically, using the same per-worktree-gitdir model that linked
worktrees already use for the superproject.

The key design choice is where the per-worktree submodule gitdir lives.
An earlier iteration (v1) nested it under the superproject's own
worktrees/ tree:

$GIT_COMMON_DIR/worktrees/<wt-id>/modules/<name>/

Junio pointed out that this placement is invisible to the shared
submodule repository, so "git gc" on the shared repo can prune commits
reachable only from a per-worktree HEAD. This series follows the
approach he suggested instead: treat the absorbed submodule repo as a
bare-like repo and create proper linked worktrees off it:

$GIT_COMMON_DIR/modules/<name>/worktrees/<wt-id>/

Because this path lives inside the shared repo's own worktrees/ tree,
"git gc" finds it via get_worktrees() and protects per-worktree objects.

The series is structured as follows:

1/4 submodule: place per-worktree gitdir under shared repo
      Teaches submodule_name_to_gitdir() to return the new path in
      linked worktrees and fixes validate_submodule_legacy_git_dir()
      to handle the new path shape.

2/4 submodule--helper: init per-worktree submodule gitdir
      Wires up clone_submodule() to create the per-worktree gitdir at
      the new location, with auto-absorb of legacy in-tree gitdirs and
      a guard in ensure_core_worktree() to prevent overwriting the
      shared repo's core.worktree.

3/4 worktree: add --recurse-submodules to worktree add
      Adds the flag to "worktree add" and teaches "worktree remove" to
      clean up per-worktree submodule gitdirs explicitly (since they
      no longer nest under the worktree's own gitdir).  Includes
      documentation.

4/4 t2403, t2405: test per-worktree submodule gitdir model
      Tests covering initialisation, removal, mixed/legacy layouts,
      nested submodule paths, and the auto-absorb code path.

@gitgitgadget
Copy link
Copy Markdown

gitgitgadget Bot commented May 12, 2026

Welcome to GitGitGadget

Hi @Ergus, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that either:

  • Your Pull Request has a good description, if it consists of multiple commits, as it will be used as cover letter.
  • Your Pull Request description is empty, if it consists of a single commit, as the commit message should be descriptive enough by itself.

You can CC potential reviewers by adding a footer to the PR description with the following syntax:

CC: Revi Ewer <revi.ewer@example.com>, Ill Takalook <ill.takalook@example.net>

NOTE: DO NOT copy/paste your CC list from a previous GGG PR's description,
because it will result in a malformed CC list on the mailing list. See
example.

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "revisions:" to state which subsystem the change is about, and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the Libera Chat IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this. Note that any reviewers CC'd via the list in the PR description will not actually be sent emails.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join git-mentoring@googlegroups.com, where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Libera Chat. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

Ergus added 4 commits May 12, 2026 12:35
submodule_name_to_gitdir() currently returns a path under the
superproject's worktrees/ tree in a linked worktree:

  $GIT_COMMON_DIR/worktrees/<wt-id>/modules/<name>/

That path is invisible to the shared submodule repository at
$GIT_COMMON_DIR/modules/<name>/, so "git gc" on the shared repo
cannot find it via get_worktrees() and may prune commits that are
only reachable from a per-worktree HEAD.

Return the per-worktree gitdir as a genuine linked worktree of the
shared submodule repository instead:

  $GIT_COMMON_DIR/modules/<name>/worktrees/<wt-id>/

Because it lives inside the shared repo's worktrees/ tree, "git gc"
sees it via get_worktrees() and protects per-worktree objects.

Update validate_submodule_legacy_git_dir() to strip the trailing
/worktrees/<wt-id> before the sibling-clash name check, since the
new path no longer ends with the submodule name.

Signed-off-by: Jimmy Aguilar Mena <kratsbinovish@gmail.com>
When "git submodule update --init" runs in a linked worktree, set up
the submodule's gitdir following the layout from the previous commit:

  $GIT_COMMON_DIR/modules/<name>/worktrees/<wt-id>/

Any cloning targets the shared submodule repo at
$GIT_COMMON_DIR/modules/<name>/.  Once that exists, a thin
per-worktree gitdir is created under the shared repo's own worktrees/
tree with a "commondir" file pointing back at the shared repo.  The
per-worktree gitdir holds only HEAD and (after first use) the index;
all objects, refs and config come from the shared repo.

If the shared repo is missing and the main worktree has an in-tree
.git/ directory for the submodule, run absorbgitdirs first to
relocate it; if that yields nothing, clone from the upstream URL.

Skip overwriting core.worktree in the shared repo's config in
ensure_core_worktree() when the submodule gitdir has
different_commondir set; writing a per-worktree relative path into
the shared config would corrupt the main worktree's view.

Signed-off-by: Jimmy Aguilar Mena <kratsbinovish@gmail.com>
"git worktree add" leaves submodules uninitialised in the new working
tree; the user must run "git submodule update --init" afterwards.

Add --recurse-submodules to do this automatically.  After the
checkout succeeds, a child "git submodule update --init --recursive"
is run with its working directory set to the new worktree path.
Each submodule's gitdir is set up as a linked worktree of the shared
submodule repository, so objects and refs are shared without
requiring hardlinks.

The flag is incompatible with --no-checkout (nothing checked out)
and --orphan (no commits to carry submodule config).

Teach "worktree remove" to delete per-worktree submodule gitdirs
from $GIT_COMMON_DIR/modules/<name>/worktrees/<wt-id>/ when the
worktree is removed; they are no longer nested inside the worktree's
own gitdir and must be cleaned up explicitly.  Worktrees where all
submodules use this model are allowed past the submodule guard in
check_clean_worktree(); the existing "git status" check still blocks
removal of trees with uncommitted submodule changes.

Signed-off-by: Jimmy Aguilar Mena <kratsbinovish@gmail.com>
Add tests verifying:

 * "worktree add --recurse-submodules" initialises submodules and
   places their per-worktree gitdir at
   $GIT_COMMON_DIR/modules/<name>/worktrees/<wt-id>/ with a
   commondir file pointing at the shared repo and no objects/ of
   its own.

 * "worktree remove" deletes the per-worktree submodule gitdir and
   leaves the shared submodule repo intact.

 * Removal is blocked when any submodule uses the legacy shared
   gitdir layout or when submodule changes are uncommitted.

 * scan_modules_for_wt_id() handles nested submodule repos and
   slash-named submodule paths correctly.

 * The auto-absorb path works when the main worktree has an in-tree
   submodule .git/ directory.

Signed-off-by: Jimmy Aguilar Mena <kratsbinovish@gmail.com>
@Ergus Ergus force-pushed the worktree-recurse-submodules branch from 233efe0 to cea155c Compare May 12, 2026 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant