33

In svn you can link a repository to any folder in another svn repository. I'm wondering if there is a similar feature for git? Basically I want a git submodule inside my repository, but I want the submodule to be a pointer to a subfolder of another git repository, not the whole repository. Is this possible?

Cory
  • 22,772
  • 19
  • 94
  • 91

3 Answers3

27

Git does not support partial checkouts, so the submodule must point to a complete repository.

This thread on the Git mail list provides some background information.

This article from Panther Software offers some insight from someone else trying to accomplish a similar goal (i.e. replicate svn:externals using Git).

If both projects are under your control, then I suggest you isolate the "subfolder" that you interested in to its own standalone repo. Then both projects can create submodules that link to it.

crenshaw-dev
  • 7,504
  • 3
  • 45
  • 81
Tim Henigan
  • 60,452
  • 11
  • 85
  • 78
  • 1
    Yes, it does sound like the subfolder should be it's own module if it is to be shared between two projects. – Dana the Sane Jul 13 '09 at 19:17
  • 1
    Link died at Panther Software http://panthersoftware.com/articles/view/3/svn-s-svn-externals-to-git-s-submodule-for-rails-plugins – Nam G VU Aug 22 '16 at 11:07
24

I'm running into the same issue. It doesn't look solvable from a git level, at least not in a way that lets you easily pull or push to the parent repo.

However, you can work around this limitation by using a simple symlink:

  1. Set up your submodule in a separate directory.
    • git submodule add http://example.com/repo.git ./submodules/repo
  2. Create a symlink to the subfolder in the place you want:
    • ln -s ./submodules/repo/subdirectory ./wherever/symlinked_directory

References:

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Anthony Michael Cook
  • 1,082
  • 10
  • 16
3

Usually when you're trying to extract a subfolder of some other project, that subfolder should be a separate project in the first place, and both parent projects should be referring to it.

You can extract such a subproject's history using a tool like git subtree. Then you can link the subtree back into your project using either git submodule or git subtree, whichever you prefer.

apenwarr
  • 10,838
  • 6
  • 47
  • 58
  • 12
    Not always. For example I want to pull Twitter Bootstrap into my project to use the .less files. I don't need all the examples and parser, etc. just the .less files from the /lib/ folder. Currently I cannot get that on it's own using a submodule – Hades Jan 08 '12 at 08:56
  • You can map just a specific subdirectory to a directory in your repo using `git subtree`. – technomage Apr 24 '17 at 16:37