gitclonegit-submodulessparse-checkoutshallow-clone

Set Git submodule to shallow clone & sparse checkout?


Many vendor Objective-C libraries (e.g., facebook-ios-sdk) instruct you to copy a certain subset of its repo's files/dirs into your Xcode project. One problem with this is then you do not know what revision of the vendor code you have. Another is that if you make changes to the vendor code, it's not easy to contribute your changes via Git.

As a solution, I want to add each vendor library as a Git submodule of my project's repo with some extra settings (say, in the .gitmodules file). This way, if another person clones my project and does git submodule update --init, their repo & submodules will have the same state as mine because they'll be using the same default settings I set:

  1. Sparse checkout: Only check out certain files of the submodule.
  2. Shallow clone: Only clone a certain SHA1 of the submodule.

How do I set the above settings for a Git submodule?


Solution

  • With git1.8.4 (July 2013), in addition git shallow update for submodule (git submodule update --depth 1), you now can have a custom update:

    In addition to the choice from "rebase, merge, or checkout-detach", "submodule update" can allow a custom command to be used in to update the working tree of submodules via the "submodule.*.update" configuration variable.

    See commit 6cb5728c43f34a7348e128b44b80d00b9417cb19:

    Users can set submodule.$name.update to '!command' which will cause 'command' to be run instead of checkout/merge/rebase.
    This allows the user finer-grained control over how the update is done.

    Signed-off-by: Chris Packham <judge.packham@gmail.com>

    That means you can version a 'command' that you can then use for any submodule update (through the submodule.$name.update setting).
    That script can do a sparse checkout if you want.


    Update August 2016 (3 years later)

    With Git 2.10 (Q3 2016), you will be able to do

     git config -f .gitmodules submodule.<name>.shallow bool
    

    See "Git submodule without extra weight" for more.