15

I added a submodule:

git submodule add git://github.com/chneukirchen/rack.git rack

A file .gitmodules was created like:

[submodule "rack"]
path = rack
url = git://github.com/chneukirchen/rack.git

And of course Git knows about it:

git submodule status
30fb044db6ba5ea874ebc44a43bbd80a42676405 rack (1.3.0-64-g30fb044)

I added a submodule by hand, for example, adding to that file:

[submodule "redcloth"]
path = plugins/redcloth
url = git://github.com/jgarber/redcloth.git

And I repeated the previous command:

git submodule init
Submodule 'rack' () registered for path 'rack'

git submodule update
(no output)

git submodule status
30fb044db6ba5ea874ebc44a43bbd80a42676405 rack (1.3.0-64-g30fb044)

So, as far I can see, what I added by hand is ignored. Is there some way to make Git aware of the lines added by hand in the .gitmodules file?

Note: I've also tried to add the lines by hand to the .git/config file and that didn't work either.

CJ Dennis
  • 3,676
  • 2
  • 32
  • 53

2 Answers2

18

Ok, so thanks to Adam I found the answer, was kind of obvious but nevertheless, here it is:

If you check what git submodule add does, you'd notice that it does three things:

  1. Adds the lines to the .gitmodules file,
  2. Clones the repo in the 'path' you determined in the command, and
  3. Adds the module to the .git/config file.

So, basically the only difference between a repo with a submodule added by hand and the one added via the git submodule command is the contents of the repo itself.

Answering with the same example, you should:

$ git clone git://github.com/jgarber/redcloth.git plugins/redcloth

Add the following to the .git/config file*:

[submodule "redcloth"]
url = git://github.com/jgarber/redcloth.git

Make sure at least you add them to the git repo:

$ git add plugins/redcloth

And then check if git actually is "aware":

$ git submodule status
0766810ab46f1ed12817c48746e867775609bde8 plugins/redcloth (v4.2.8)
30fb044db6ba5ea874ebc44a43bbd80a42676405 rack (1.3.0-64-g30fb044)

*note that the "path" variable you use in the .gitmodules file isn't needed in that file

  • This does not work correctly. What it does, it adds repository not as submodule but as part of main repository code. Doing `git clone`, clones it normally even if you clone in external repository. And after adding it shows code added normally instead as submodule. Or maybe I missed something here? – Andrius Jul 25 '18 at 06:17
  • 1
    @Andrius Yes, I think actually Pablo missed something. He's not showing the necessary edit to the `.gitmodules` file. The submodule needs to be specified in both the `.git/config` and `.gitmodules` files (as shown in the second code block of the original question). – Auspex Aug 21 '18 at 09:05
3

You need to run

git submodule update --init --recursive 

UPDATE:

the submodule add command actually clones the entire repo and adds the sha1 to the index.

This may be new behaviour as compared to previous versions of git, the clone was not done immediately.

If you don't have an entry in the index pointing the module to a particular commit, git submodule update refuses to do anything. This seems to be new behaviour.

Hope this helps.

Adam Dymitruk
  • 109,813
  • 21
  • 138
  • 137
  • 3
    That doesn't work... it still doesn't detect the hand-made submodule. – Pablo Olmos de Aguilera C. Oct 03 '11 at 03:34
  • did you add a blank directory called rack in the repo? – Adam Dymitruk Oct 03 '11 at 20:24
  • No... it's not needed. They're created automatically. The issue here, is that git submodule is detecting only the module that has been added through git submodule add and not the ones added directly into the file. Also, looks like that --recursive option has something to do with something really different. Maybe I haven't explained well enough? – Pablo Olmos de Aguilera C. Oct 04 '11 at 22:30
  • git doesn't store anything outside of the repo folder. Clone the repo and then do a directory difference. You will probably see that the empty dir is made and you haven't. Please post the difference output here so people can help you. – Adam Dymitruk Oct 04 '11 at 23:25
  • Sorry, but I still understand how that is related to my original question (Did I explain clearly?). The problem is that I added submodules to the `.gitmodules` file by hand instead using the `git submodule` command, I expected when I run `git submodule update --init`, the submodules would have been updated too, but everything I added by hand are being ignored. – Pablo Olmos de Aguilera C. Oct 05 '11 at 00:27
  • 1
    All I'm saying is see for yourself what the difference is by making a copy of the repo, in one do it by hand, in the other do it by means of commands. Now run something like windiff to see the difference between the 2 in structure and any file content. – Adam Dymitruk Oct 05 '11 at 00:47
  • Oh ok, I understand :$. I don't know if you _knew_ the answer and you just wanted me to search for it or it was a supposition. In any of both ways (specially if it was first though) thank you, the problem is solved : ) – Pablo Olmos de Aguilera C. Oct 05 '11 at 01:28