Search code examples
git-submodules

git submodule contents are not cloned along with the base repo


I created a repo:

git init --bare myrepo.git

Then on the same server, created the repo for production

cd /home/myuser/public_html
git init
git remote add origin /usr/local/gitroot/myrepo.git
git commit --allow-empty -m "Initial commit"

Then created the submodule

cd subdirectory
git init
git add .
git commit -m "Initial submodule commit"
cd ..
git submodule add /home/myuser/public_html/subdirectory subdirectory
git add .
git commit -m "Initial commit of base files and submodule"
git push origin master

Then on my local machine

git clone --recursive ssh://user@mydomain/path/to/git mygitdirectory

And at the tail end of what was looking like a clean clone, I get

fatal: repository '/home/myuser/public_html/subdirectory' does not exist
Clone of '/home/myuser/public_html/subdirectory' into submodule path 'subdirectory' failed

Trying git submodule add and git submodule update in subdirectory yields the same result. I ended up with all the base files, but the subdirectory being empty. On the server, git log shows the commit of the base files + submodule, and git status in both the base and the submodule shows clean.

Postscript

I blew away everything and tried again changing the submodule creation to:

git submodule add ./subdirectory ./subdirectory

which yielded a .gitmodules on the server of

[submodule "subdirectory"]
path = subdirectory
url = ./subdirectory

When doing the clone on the local machine, it resulted in the same error when it got to the submodule part. So, I changed the .gitmodules (and then did a git submodule sync) to:

[submodule "subdirectory"]
path = subdirectory
url = ssh://user@mydomain/path/to/git

Reading (many) different workflow suggestions, I find myself unsure (a) what the contents of each .gitmodules should be (I assume the server one is correct) (b) whether the remote bare repo to which the super repo on the server is pushed should be the only bare repo or whether the submodule needs one as well (I assume that once the submodule is committed in the super repo that pushing the super repo to the bare repo (origin) takes the submodule history as well), and (c) whether the local submodule needs to have a remote defined (it didn't appear to on the server, so I didn't so so locally)


Solution

  • Your local git is trying to clone the submodule using the remote URL that you originally cloned it from - which is a local path on the server and won't exist on your local machine.

    Try cloning the submodule via SSH instead, so its remote URL will work on both the server and your local machine.