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.

asked Mar 9, 2011 at 21:34

1 Answer 1

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
answered Mar 9, 2011 at 21:37
8
  • 1
    Does this still work if the submodule is not hosted on GitHub, but your repo is? Commented Aug 1, 2012 at 18:57
  • 3
    @ch3rryc0ke: yes - if you can clone the URL for a repository, you can add it as a submodule. Commented Aug 2, 2012 at 8:03
  • 1
    To clone into a subdirectory use git submodule add git://github.com/whomsoever/whatever.git subdirectory/whatever Commented Jan 6, 2014 at 21:58
  • 1
    @GreatBlakes you will need to do a sparse-checkout Commented Jan 16, 2015 at 13:30
  • 2
    @DavidDouglas It seems that the zip file you get from "Download ZIP" doesn't contain the contents of submodules, I'm afraid. Commented Apr 7, 2016 at 13:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.