Clone submodule into directory
Asked Answered
K

5

5

I'm having an issue with creating a submodule in my project. If I create the submodule directly in the repository root, everything works fine. If the submodule is any deeper, the repository does not get cloned.

For example, this works as expected:

git submodule add git://someproject.com/.git someproject

However, when I run the following command, the project is added to .gitmodules and an empty repository is created, but no code is pulled down (even after a git submodule update --init). The command does not produce any output.

git submodule add git://someproject.com/.git lib/someproject
Ky answered 19/12, 2012 at 1:15 Comment(0)
K
3

It appears that a previous attempt to add a submodule had left my repository in a bad state. I was able to clone the submodule correctly after performing these steps:

  1. Remove the submodule: rm -rf lib/someproject
  2. Remove the submodule from the .gitmodules file
  3. Remove the submodule from the .git/config file
  4. Remove .git/modules/someproject

Then running the git add submodule command again worked. Many thanks to all the answerers who pushed me in the right direction!

Ky answered 20/12, 2012 at 1:35 Comment(1)
This is the right answer. The issue was because repository I tried to make as submodule of core project was empty and command "git submodule add" did bad things.Alkalimeter
B
3

Try running

git submodule init
git submodule update

after you added the submodule.

Update 1

Try this:

cd lib/someproject
git status

You should see something like # Not currently on any branch.

If not, there is no git repo present, and you might have stumbled upon a git bug, if you see the above message do the following:

git checkout master
git pull
Binghi answered 19/12, 2012 at 1:26 Comment(2)
That would seem logical, and I did try that. Unfortunately nothing at all happened after running both of those commands. (No output, no new files, nothing).Ky
Stranger and stranger. git status outputs On branch (null). Initial commit. git checkout master outputs error: pathspec 'master' did not match any file(s) known to git There is only one file in the directory, a .git file. The entire contents are gitdir: ../../.git/modules/lib/someproject. (Thanks for the continued help, by the way)Ky
K
3

It appears that a previous attempt to add a submodule had left my repository in a bad state. I was able to clone the submodule correctly after performing these steps:

  1. Remove the submodule: rm -rf lib/someproject
  2. Remove the submodule from the .gitmodules file
  3. Remove the submodule from the .git/config file
  4. Remove .git/modules/someproject

Then running the git add submodule command again worked. Many thanks to all the answerers who pushed me in the right direction!

Ky answered 20/12, 2012 at 1:35 Comment(1)
This is the right answer. The issue was because repository I tried to make as submodule of core project was empty and command "git submodule add" did bad things.Alkalimeter
C
2

You need to run the following command

git submodule update --init lib/someproject

For some reason git only looks in the root directory when running and update on submodules, instead of through the whole working copy.

Also make sure your .gitmodules file contains an entry like this:

[submodule "someproject"]
    path = lib/someproject
    url = git://someproject.com/.git

And your .git/config file contains:

[submodule "someproject"]
    url = git://someproject.com/.git

Docs:

http://git-scm.com/book/en/Git-Tools-Submodules

Cerda answered 19/12, 2012 at 5:1 Comment(3)
I thought it would be something like that. Unfortunately git reports error: pathspec 'lib/someproject' did not match any file(s) known to gitKy
Added what the .gitmodules file should look at as well as the .git/config fileCerda
Okay... got the problem solved now. I'll upvote your answer as it set me looking in the right place. There were problems not only in the .git/config file but also in the .git/modules folder as well!Ky
F
0

I just got through reading the submodule section of the O'Reilly book and the author mentions when adding a submodule (manually with git add to a cloned repo at the root of the project in his example), that including the trailing slash causes it to add the folder to the index vs. creating a gitlink. It sounds silly, but maybe try changing directories to lib before your add to ensure this isn't happening to you due to the slash in your path to the submodule.

This might also be a peculiarity of 'submodule add' only recieving the path and remote url and inferring the submodule name from the path when writing to the configs. Perhaps manually edit the gitmodules and config entries to ensure the submodule is named without 'lib/', but the path contains it.

Fulkerson answered 19/12, 2012 at 6:53 Comment(0)
N
0

I had to delete the content of .gitmodules and add submodule again with --force option e.g. git submodule --force add git://someproject.com/.git. Also since I'm using a Dockerfile to install a specific submodule of .gitmodules, I forced the update there too RUN git submodule update --init -f someproject. However, I think the forcing adding again solved the issue.

Nonjuror answered 22/5, 2020 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.