Apparently, adding a subtree of a repository that has submodules will break git submodule init
. Here is a script which reproduces the problem:
#!/bin/sh
set -ex
mkdir submod
cd submod
git init
touch foo
git add foo
git commit -asm "This is a submodule"
cd ..
mkdir subtree
cd subtree
git init
git submodule add `realpath ../submod` submod
git commit -asm "This has reference to submodule"
cd ..
mkdir top
cd top
git init
touch bar
git add bar
git commit -asm "Dummy commit so HEAD resolves correctly"
git subtree add --prefix=subtree `realpath ../subtree` master
# This fails!
git submodule init
What this script is doing is:
- Create a repo submod
- Create a repo subtree that has a submodule reference to submod
- Create a repo top that has a subtree reference to subtree
Upon further consideration, it is clear what the problem is: the subtree mechanism has added subtree's submodule reference to submod to the tree, but the .gitmodules
metadata remains in subtree/.gitmodules
, not the top-level .gitmodules
, which means that git submodule init
fails. If we copy the contents of subtree/.gitmodules
to .gitmodules
, adjusting all the paths accordingly, that solves the problem...
[submodule "submod"]
path = subtree/submod
url = /Users/ezyang/Dev/labs/git-subtree-submod/submod
...but it is a bit of pain if the subtree has a lot of submodules. Is there a better way to do this?