64

I am trying to find out if it is possible to use someone else's repository (or branch of a repository) as a Submodule in your own Git repository. Documentation on github itself is either missing, or I'm not using the right terminology to look for it. If this isn't the preferred way to go about including a public repository as a shared library within ones git project, suggestions as an alternative best practice would be appreciated.

Graham Conzett
  • 8,344
  • 11
  • 55
  • 94

1 Answers1

89

Yes, you can add any repository as a submodule in your project. Just do:

git submodule add git://github.com/whomsoever/whatever.git

... in the top level of your repository. This is indeed the easiest way with git to use some existing useful repository within your own. For more information on submodules, you could look at:

Update: as jfountain points out below, if you want to add the submodule at a subdirectory path (or with a name different from the default) you can supply that as an additional parameter to that command, e.g.:

git submodule add git://github.com/whomsoever/whatever.git foo/bar
Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 1
    Does this still work if the submodule is not hosted on GitHub, but your repo is? – ch3rryc0ke Aug 01 '12 at 18:57
  • 3
    @ch3rryc0ke: yes - if you can clone the URL for a repository, you can add it as a submodule. – Mark Longair Aug 02 '12 at 08:03
  • 1
    To clone into a subdirectory use git submodule add git://github.com/whomsoever/whatever.git subdirectory/whatever – jfountain Jan 06 '14 at 21:58
  • If I just want to include one specific file from someone's repo is that even possible? I don't want to include their license, test files, readme, etc. Am I looking in the wrong place with submodule, or is this not even possible? – GreatBlakes Oct 08 '14 at 00:43
  • 1
    @GreatBlakes you will need to do a sparse-checkout – server_kitten Jan 16 '15 at 13:30
  • If someone downloads the .zip archive of the repro on github does this also contain the submodules? – David Douglas Apr 07 '16 at 09:14
  • 2
    @DavidDouglas It seems that the zip file you get from "Download ZIP" doesn't contain the contents of submodules, I'm afraid. – Mark Longair Apr 07 '16 at 13:47
  • @mark-longair It looks like your links to `Pro Git's section on submodules` and the `git community book` (1st & 3rd bullets in your `more info` section) at http://progit.org/book/ch6-6.html and https://book.git-scm.com/5_submodules.html respectively, are no longer working. – Scott Veirs Mar 21 '23 at 16:47