Search code examples
gitgit-submodules

Git error while cloning a repository with submodule


I just started learning about git submodules and while I was experimenting with it I ran into an error I can't solve. The details are the following:

In a directory on my machine called Sandbox I've created the following folders: local-repo1, local-repo2, remote-repo. My goal was to create a remote repository based on local-repo1insideremote-repoand then pull from that remote tolocal-repo2, practically to make a working copy of local-repo1`.

Here is the directory structure of local-repo1:

local-repo1
├── .git
├── .gitmodules
├── project1
│   └── data
└── project2
    ├── .git
    └── data

The content of .gitmodules:

[submodule "project2"]
    path = project2
    url = ./project2
    branch = master

I made one commit on project2 then I added it to the parent repository with the git submodule add command, later made a commit on that repository too. The next step was pushing to remote-repo, where I already created a bare repository. Everything went well.

Finally I initalized a repository in local-repo2 and pulled the content from remote-repo. Things went well, but when I looked inside project2, it was empty and when I attempted to resolve it with git submodule update --init I got the following error:

Submodule 'project2' (../remote-repo/project2) registered for path 'project2'
fatal: repository '../remote-repo/project2' does not exist
fatal: clone of '../remote-repo/project2' into submodule path '/home/user/Sandbox/local-repo2/project2' failed
Failed to clone 'project2'.

Assume that data inside project2 is crucial to local-repo2 to compile and work. How can I resolve it?


Solution

  • If I understand correctly what you are trying to do, the problem is that you cannot create a new project in this way. git submodule add wants as its argument the URL of an existing Git repository whose project you would like to add as a submodule here. The URL needs to resolve in the same way as git clone requires as its argument a remote repository which it can connect to and fetch.