gitgit-submodules

Adding local repo as submodule always resolves to Github


This is such a bizarre thing. I'm trying to add local repo ProjectB as a submodule of ProjectA. I'm very familiar with the process and commands to work with submodules, but trying to run git submodule add ../ProjectB ProjectB in the root of ProjectA causes git to try to clone from Github, not the local repo (I've omitted from this the other submodule commands, but they were run). I've tried multiple times, resetting everything each time (.gitmodules, .git/config, .git/modules/ProjectB)

I'm not sure if that's a bug or a questionable design choice, but digging in I ran GIT_TRACE=1 GIT_CURL_VERBOSE=1 git submodule add --force ../ProjectB ProjectB and indeed it is trying to clone from Github.

So--I remove all references to Github from ProjectB: package.json, git remote, everything I could find. I'm pretty sure I got them all, but when I try the above again, it still resolves to Github instead of the local repo.

I am looking for an explanation of this behavior, not the unfortunately common "Why are you using submodules?" style of non-answer.

Does someone have an explanation for this behavior? It doesn't make sense how git even KNOWS where that repo is on Github since I've removed all references to Github from ProjectB, much less why and how it's choosing to clone from Github instead of where I told it.


Solution

  • Explanation

    The problem is that ../ProjectB is interpreted by Git as a relative URL with respect to the main sandbox's remote, i.e., wrt ProjectA's remote in this case.

    The is useful when you store your project and its submodules on the same server. If you fork everything, move to a different server, or move everything to local storage, the same submodule definitions will remain valid in all cases.

    Solution

    If you want the file protocol when the main sandbox uses a remote one, you'll have to be explicit about it:

    git submodule add --force file:../ProjectB