Create a worktree for a project using submodules

Viewed 177

I have a top project, with about 10 submodules, and some nested submodules.

./top-project/
./top-project/module-a
./top-project/module-b
...

Some submodules have a classic url, like url = git:software/module-a, other have an url relative remote top-project like url = ../module-b. top-project url is url = git:software/top-project.

I want to create a worktree copy of top-project, AND all its submodules.

git worktree add -f ../top-project-v2

But I can't find a way making the submodules pointing to their original path. I would like that top-project-v2/module-a git pointing to top-project/module-a, and not cloning a brand new module-a.

If I do cd top-project-v2; git submodule upate --init --recursive, the command succeeds but submodules are not linked to top-project submodules.

If I do git checkout --recurse-submodules origin/v2, I get an error:

git checkout --recurse-submodules origin/v2
fatal: not a git repository: ../../top-project/.git/worktrees/top-project-v2/modules/module-a
fatal: could not reset submodule index

Is there any way to do so?

1 Answers

If the submodules are not optional or cheap enough to fetch anyway, carry the submodule histories in the main repo as branches and git submodule init; git worktree add for them.

Related