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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@JanWrobel a submodule is a git repo in itself, and sparse checkout (stackoverflow.com/questions/2336580/…) isn't supported for submodules. The best practice remains for a git repo to represent one "unit of code", not a collection.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.