Duplicate submodules with Git

This isn’t built into git, but you can definitely do it with symlinks like you say. You might want to have a look at git new-workdir (from git’s contrib directory), which does essentially this. It’s not aware of anything to do with submodules, but a submodule doesn’t know it’s a submodule – it’s the parent repo that knows about that stuff. I haven’t tried this, but I’m fairly certain you could use it something like this:

# remove the target first (new-workdir will refuse to overwrite)
rm -rf submodule_b/submodule_m

#               (original repo)         (symlinked repo)
git new-workdir submodule_a/submodule_m submodule_b/submodule_m

It works by symlinking essentially all of the .git directory; the notable thing that isn’t symlinked is HEAD; the two directories can have different things checked out, but share the same refs and objects.

From here you should be good. When you run a git submodule command in the supermodule, it just goes into the submodules and runs appropriate commands there, which will all work as expected.

The one thing you usually need to be aware of with symlinked repos like this is that they share the same set of branches, so if they both have the same branch checked out, and you commit to it in one, the other will become out of sync. With submodules this generally won’t be a problem, though, since they’re essentially always in detached HEAD state unless you intervene.

Leave a Comment