2

I want to include a large git repository as a submodule in my project. To reduce download times for developers in the project, I'd like to add the submodule so that when it is cloned, only one version is retrieved. Also, the version I need is an older version which is marked with a tag.

How do I achieve this when adding the module or when setting up a new clone?

Rafael Vega
  • 4,575
  • 4
  • 32
  • 50

2 Answers2

1

As stated in this answer you can use the --depth parameter:

git submodule add --depth 1 -- repository path
git submodule update --depth -- [<path>...]
Community
  • 1
  • 1
Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • That answer explains how to do a shallow submodule (nice!), but how do you set it up so that it checks out a particular tag and not master HEAD? – Rafael Vega Jan 16 '16 at 17:44
  • @RafaelVega If you could set a branch where the tag is, you could use the `-b` parameter to specify the branch to clone. – Klas Mellbourn Jan 16 '16 at 17:47
  • The repo I want to include is not mine so I'd have to fork it just to create a branch for this purpose (not too practical but doable). Is it possible to do the same thing with a tag name instead of a branch name? – Rafael Vega Jan 16 '16 at 17:54
  • @RafaelVega Not possible as far as I understand the documentation – Klas Mellbourn Jan 16 '16 at 17:59
1

The tag is actually a human-readable version of a commit and when you add a submodule to a project, the project that you've added it to will store the state of the submodule at that commit hash. It doesn't point at HEAD but rather the exact point in time commit when the submodule was added.

For example, game-music-emu @ 21a064e is at the exact commit of 21a064e, this is how submodules work by default.

If the commit exists in the default (e.g. master) branch, the --depth 1 should get you just the commit history from the tag you are looking for. On the other hand, the default behavior is to bring with it all of the commit history from the default (e.g. master) branch, this is why the shallow, or --depth 1 is needed as part of the submodule command. This is just how git repositories are cloned by default.

You can save the commit your submodule is currently on by using git add path/to/submodule. Note, the submodule and the parent folder in which it lives are two separate repos!

Note... if the commit exists on another branch, git submodule add also supports the --branch option and the documentation states the .gitmodules file will contain an entry for branch = somebranchname, however in testing, git 1.9.1 does not seem to honor this and the default (e.g. master) branch is used regardless.

So to answer the question, to make this faster for your developers, write a helper script into your build system to pull in your submodules at a shallow depth automatically using the git submodule combined with the --depth parameter, or alternately update your documentation to specify --depth 1 so speed up clone times.

Lastly, git submodule also supports foreach [--recursive] <command> option which will allow custom commands on each submodule.

Here's an example I'm working on in cmake to lazy fetch at a shallow depth for an entire project. https://gist.github.com/tresf/263ea897036fa762674d489c8d457d68

tresf
  • 7,103
  • 6
  • 40
  • 101