33

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?

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • I don't think you can do that, but you could commit a script like this one: http://stackoverflow.com/questions/2144406/git-shallow-submodules – Mauricio Scheffer Jun 04 '11 at 18:49
  • Note that `git submodule` now can perform a **custom update**: see [my answer below](http://stackoverflow.com/a/17693008/6309) – VonC Jul 17 '13 at 06:50

3 Answers3

22

You can do sparse checkouts of submodules the same way as normal sparse checkout. Just remember the sparse-checkout file for each module goes in .git/modules/<mymodule>/info/. But, as discussed in git 1.7 sparse checkout feature, sparse checkouts are exactly that: checkouts. You can't move files or share the settings.

Community
  • 1
  • 1
gwohpq9
  • 2,143
  • 2
  • 18
  • 18
  • 1
    +1 for a good solution (thanks @takoi). Create a submodule, then filter the files/dirs that appear in the working tree using the sparse checkout feature. There is a simple how to on Sparse checkout at http://blog.quilitz.de/2010/03/checkout-sub-directories-in-git-sparse-checkouts – johnhunter Aug 12 '12 at 17:51
  • 3
    @johnhunter Link is dead – void.pointer Jan 15 '15 at 01:19
  • there is a version of the blog on the wayback machine: https://web.archive.org/web/20150819045120/http://blog.quilitz.de/2010/03/checkout-sub-directories-in-git-sparse-checkouts – SAK Mar 26 '20 at 18:36
22

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    VonC, but exactly what command do you then suggest to do the sparse checkout? I have no idea what it is. – Thomas Vander Stichele Oct 30 '13 at 01:29
  • @ThomasVanderStichele a script that would perform a sparse checkout, similar to this one: http://stackoverflow.com/a/2340860/6309 – VonC Oct 30 '13 at 07:13
  • `!command` does not work anymore: “Note that the !command form is intentionally ignored here for security reasons.” – saji Apr 11 '17 at 10:47
  • @saji in which context/OS/policy do you see ! (shell execute) ignored for security reason? – VonC Apr 11 '17 at 10:49
  • @VonC My bad. It seems that `!command` form in `submodule..update` is not allowed in [`gitmodules`](https://git-scm.com/docs/gitmodules), but [is fine](https://github.com/git/git/commit/6cb5728c43f34a7348e128b44b80d00b9417cb19#diff-08d0168d2216294c4f9de46cbb36bfcb) in “regular” config files.Which makes sense as `.gitmodules` is commited and `.git/config` is not. – saji Apr 13 '17 at 09:27
-1

Submodules can't do part of a repo. You should check out subtree merge instead.

Tekkub
  • 30,739
  • 2
  • 30
  • 20