andypaxo
andypaxo

Reputation: 6641

Clone submodule into directory

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

Upvotes: 5

Views: 2039

Answers (5)

Roxana Tapia
Roxana Tapia

Reputation: 115

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.

Upvotes: 0

andypaxo
andypaxo

Reputation: 6641

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!

Upvotes: 3

daegren
daegren

Reputation: 142

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

Upvotes: 2

Kyle
Kyle

Reputation: 81

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.

Upvotes: 0

Peter van der Does
Peter van der Does

Reputation: 14508

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

Upvotes: 3

Related Questions