11

I have git repo where I link some of my dependencies using git submodules inside extern/ dir. Say I have a dependency A as a submodule, located at extern/A. Now A itself has some dependencies as git submodules. In A's repo, that is, eg. A/test/data/X.

Now when I clone my repo, and run git submodule update --init --recursive, in order to build it on a CI server, or w'ever, I'd like the above command to ignore exter/A/test/data/X, because it's like 1G of data that I don't want.

My other dependencies however, have some useful submodules, so I can't just skip --recursive. Is there a way to do this?

Braiam
  • 1
  • 11
  • 47
  • 78
skrat
  • 5,518
  • 3
  • 32
  • 48
  • 3
    Possible duplicate of [How to exclude a specific git submodule from update?](https://stackoverflow.com/questions/52179463/how-to-exclude-a-specific-git-submodule-from-update) – Saurabh P Bhandari Jun 25 '19 at 00:42
  • @SaurabhPBhandari it is for sure not a duplicate for that one. It is a valid question – Zatarra Nov 19 '19 at 13:27
  • @Zatarra, Updated the answer – Saurabh P Bhandari Nov 20 '19 at 17:33
  • @SaurabhPBhandari your update it is still not valid for a submodule of a submodule (nested) – Zatarra Nov 20 '19 at 22:21
  • @Zatarra, I have tested it on nested submodules, it works for me – Saurabh P Bhandari Nov 21 '19 at 01:55
  • 1
    @zatarra why is it "not valid" ? – Jonas Wilms Nov 21 '19 at 06:44
  • Just try to apply that on https://github.com/MISP/MISP.git by ignoring tests/viper-test-files submodule of PyMISP submodule . For me it didn't work – Zatarra Nov 21 '19 at 12:14
  • 1
    @Zatarra, since the submodule is located in a directory called tests, the submodule name in this case would be "tests/viper-test-files", then the command will be ```git -c submodule."tests/viper-test-files".update=none submodule update --init --recursive``` – Saurabh P Bhandari Nov 21 '19 at 13:49
  • @SaurabhPBhandari I know, but have you tried it? I'll make it as short as possible for you in the next comment and you will still see the files are there, when they shouldn't – Zatarra Nov 21 '19 at 14:18
  • god@supermachine:~/git-test$ git clone https://github.com/MISP/MISP.git > /dev/null 2>&1 god@supermachine:~/git-test$ ls MISP god@supermachine:~/git-test$ cd MISP/ god@supermachine:~/git-test/MISP$ git -c submodule."tests/viper-test-files".update=none submodule update --init --recursive > /dev/null 2>&1 god@supermachine:~/git-test/MISP$ ls PyMISP/tests/viper-test-files/ README.md test_files god@supermachine:~/git-test/MISP$ – Zatarra Nov 21 '19 at 14:25
  • There was a version problem which got fixed in later updates (it seems that I was using a very old one) – Zatarra Nov 21 '19 at 15:19

2 Answers2

8

You can use submodule.<name>.update config variable to set which submodule should be updated as mentioned here How to exclude a specific git submodule from update?.

If you want to exclude the submodule X in A, then you can use the following command:

git -c submodule."X".update=none submodule update --init --recursive

Note: This assumes that the submodule X in repo A (which is a submodule of extern) is unique across the whole repo extern (including all its submodules, and their submodules and so on...) If X is not unique, then all submodules named X will be skipped in repo extern.


You can skip the submodules during the cloning process, using the following command:

$ git -c submodule."X".update=none clone --recurse-submodules <repository>

If the submodule is located in a directory under the repository, then the relative path with respect to its repository should be included in the submodule name. To avoid any confusion, you can get the submodule name from the .gitmodules file in the repository.

Consider the following example .gitmodules file,

[submodule "path/to/submodule_X"]
    path = path/to/submodule_X
    url = https://github.com/<user>/<repo>
[submodule "Y"]
    path = Y
    url = https://github.com/<user>/<repo>

If you want to skip submodule X, then replace <name> in submodule.<name>.update with this "path/to/submodule_X". The same applies to nested submodules.

Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
  • 2
    I don't know in which version they did the patch, but it was not working on 2.7.4, but it worked on 2.17.2. Thanks a lot for your time on this issue – Zatarra Nov 21 '19 at 15:18
1

Here is another syntax

git clone --recurse-submodules=':(exclude)**/unwanted_submodule_pathspec:

using the optional pathspec argument of --recurse-submodules[=<pathspec>].

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91