Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude submodule of a submodule

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?

like image 681
skrat Avatar asked Jun 24 '19 23:06

skrat


People also ask

How do I add a submodule to a superproject?

You must run two commands: git submodule init to initialize your local configuration file, and git submodule update to fetch all the data from that project and check out the appropriate commit listed in your superproject:

What are submodules in Git?

A repository that was cloned independently and later added as a submodule or old setups have the submodules git directory inside the submodule instead of embedded into the superprojects git directory. This command is recursive by default. Only print error messages. This option is only valid for add and update commands.

How to update a submodule when no option given?

When no option is given and submodule.<name>.update is set to none, the submodule is not updated. So set update = none in the configuration file. You can also explicitly give paths after -- to only update specific submodules. To do this on the fly and not change your configuration file, @PrashantShubham notes you can:

What happens to the submodule folder if I do not init?

If you do not init, the submodule folder will stay empty. But you can use other methods regarding the versions of Git. Using Git 1.6.5 version or higher, you can execute a single line: With version 2.13 and later, you can use --recurse-submodules instead of --recursive -j8 is an optional performance optimization available in version 2.8.


2 Answers

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.

like image 114
Saurabh P Bhandari Avatar answered Sep 25 '22 23:09

Saurabh P Bhandari


Here is another syntax

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

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

like image 45
Victor Sergienko Avatar answered Sep 23 '22 23:09

Victor Sergienko