4

I have a git repository (A) that includes a library code that I need to reuse in the second repository (B).

My understanding is that git submodule does not allow to import only specific path within a repository, the whole repository needs to be imported. Is this correct?

Because of this, I see two solutions to my problem:

  1. Have A and B as separate repositories and add a symbolic link from B to a library directory in A.
  2. Add a new git repository C with the library and import it as a submodule in A and B.

What are the advantages of the second approach? It seems to me that adding a separate repository for a small library is an overkill that can add unnecessary burden to the project maintenance. Is there any better way to solve my problem?

Jan Wrobel
  • 6,969
  • 3
  • 37
  • 53

1 Answers1

6

The main idea is build reproducibility: the ability to recover the exact configuration (ie the exact list of labels or SHA1) part of what you used at a specific time to build.

In that context (ability to reproduce the build), submodule (and the solution 2) is preferable to any symlink (which is a shortcut to a directory content, without referring to a specific version).

The other alternative is to externalize the lib, outside of a source control system, into an artifact repository like Nexus, and leave in your project(s) only a reference to the right version of that library to fetch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Site question: Do you know if there is any fundamental reason that makes it impossible for a submodule to import only a select directory from a repository? Or is this just a limitation of the current implementation? – Jan Wrobel Oct 02 '12 at 11:54
  • @JanWrobel a submodule is a git repo in itself, and sparse checkout (http://stackoverflow.com/questions/2336580/sparse-checkout-in-git-1-7-0) isn't supported for submodules. The best practice remains for a git repo to represent one "unit of code", not a collection. – VonC Oct 02 '12 at 13:15