3

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)

JAyenGreen
  • 1,385
  • 2
  • 12
  • 23

1 Answers1

3

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.

n.st
  • 946
  • 12
  • 27
  • How would I do that? I tried changing the url in the .gitmodules file to the repo url + the relative path to the submodule, and then tried it with just the repo url. both failed. If need to get the git submodule update working not only to get the files initially but to be able to stay in sync moving forward, right? – JAyenGreen Nov 28 '13 at 18:51
  • I changed the url for the submodule in .gitmodules to the ssh url, and did a git submodule sync. Now when I do a gitmodule update, instead of an error saying there's no repo, I get an error saying fatal: reference is not a tree – JAyenGreen Nov 28 '13 at 20:00
  • I haven't encountered this particular error yet, I'll try to replicate it when I get back home in a few hours. – n.st Nov 28 '13 at 20:15
  • Added a postscript above to detail the .gitmodules contents and what else I've tried – JAyenGreen Nov 28 '13 at 22:14
  • @Ayen Did get it to work, but it's somewhat messy at the moment. Git doesn't seem to like cloning an empty repository as a submodule. I'll try to elaborate the steps I've taken later. – n.st Nov 29 '13 at 01:27
  • Excellent! I appreciate it. Glad to know it wasn't just me. – JAyenGreen Nov 29 '13 at 01:48
  • First off, I'll refer you to [this tutorial on the basic use of submodules](https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial) -- it's way better at explaining this than me. ;) (It doesn't cover SSH access, though.) Your first set of commands seems to be almost right, however doing `git add .` after adding the submodule is futile (all changed associated with adding the submodule are staged automatically) and you will probably need to run `git submodule update --init` at that point. [to be continued below due to character limit] – n.st Nov 29 '13 at 16:54
  • With later cloning in mind, [you'll have to clone the submodule from a URL accessible on all systems on which you will want to clone the main repo](http://stackoverflow.com/a/6031607/1114687), so you will probably want to use the SSH url to the submodule right from the beginning. You could also clone the main repo without using `--recursive`, edit `.gitmodules` and run `git submodule update`. – n.st Nov 29 '13 at 16:58
  • Some final points: `git submodule add ./subdirectory ./subdirectory` looks like a very bad idea -- I'm surprised that even works. You *probably* shouldn't clone a directory as itself and you *definitely* should use absolute paths when cloning submodules locally. And while I suspected an empty repository as the reason for your problems, you've eliminated that problem by committing to your later-to-be submodule before cloning it, so it really comes down to specifying the correct URL for the submodule. – n.st Nov 29 '13 at 17:07
  • Ok, then one final question, for clarification. Again, I have local repo A which pushes to/pulls from remote bare repo B. And remote production repo C which also pushes/pulls B. When I clone the super repo locally, my clone command points to B. Are you saying that my submodule clone should point to C (that being the only submodule url I have)? – JAyenGreen Nov 30 '13 at 01:17
  • I've used Stack Overflow's feature to copy these comments to a public chat, since "extended discussions in comments" seem to be discouraged. Please [continue reading here](http://chat.stackoverflow.com/rooms/42230/discussion-between-n-st-and-ayen). – n.st Nov 30 '13 at 01:35