111

I'm trying to get just a folder from an external github repo to use in my project.

I want my project setup to be like this:

-my_project
    -submodule
        -code.py
    -MY_README
    -.git

And I have the remote repo named some-submodule with following structure:

-submodule
    -code.py
-README
-.gitignore

So I just want the submodule folder added to my project.

But I end up with this after

git submodule add http://github.com/user/submodule.git submodule
-my_project
    -submodule
        -submodule
            -code.py
        -README
        -.gitignore
    -MY_README
    -.gitignore

I am new to git so I really don't know if it possible using just git. If it is of some help I'm using msysgit on windows.

So, is there anyway that I can get a clean submodule folder in my project from a public repo?

If you're curious of exactly what I'm trying to do I'm trying to take directly from their repos these django plugins 1 2 to add them to my project.

igauravsehrawat
  • 3,696
  • 3
  • 33
  • 46
demula
  • 1,214
  • 2
  • 9
  • 12
  • 5
    Exact duplicate of [How can I add a specific folder from a git repo as a git submodule?](http://stackoverflow.com/questions/3158597/how-can-i-add-a-specific-folder-from-a-git-repo-as-a-git-submodule) – Dan Dascalescu Mar 25 '14 at 08:57
  • 3
    Possible duplicate of [How to change a git submodule to point to a subfolder?](https://stackoverflow.com/questions/5303496/how-to-change-a-git-submodule-to-point-to-a-subfolder) – Captain Man Feb 22 '19 at 15:58

3 Answers3

61

What you want to do is not feasible because you cannot clone a part of a repository.

See details in duplicate How to change a git submodule to point to a subfolder?

Community
  • 1
  • 1
dregad
  • 1,150
  • 8
  • 21
  • 15
    While, strictly speaking, _cloning_ only parts of a repository is not possible there is **sparse checkout** since Git 1.7 to solve what the OP probably wants. See https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository – Peterino Oct 31 '17 at 17:05
9

If you:

git submodule add http://github.com/user/submodule.git

directly under my_project, you should end up with the desired organization of directories.

From git submodule add man page:

The optional argument <path> is the relative location for the cloned submodule to exist in the superproject.
If <path> is not given, the "humanish" part of the source repository is used ("repo" for "/path/to/repo.git" and "foo" for "host.xz:foo/.git").

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Awesome! I was following tutorials all the way and I didn't read the manual... I thought that I would end up with README file of the submodule in my own project... Thanks a lot! – demula Oct 05 '10 at 19:47
  • 1
    There's a problem though... it only works if the repo of the submodule has the same name as the folder I want to use but if not ie: repo->django-submodule folder name->submodule it doesn't work anymore. Let's see if I find some workaround – demula Oct 05 '10 at 21:34
  • I'm sorry man, I tried your solution with one of the repos that was well structured and I thought the problem was already solved... my bad. – demula Oct 05 '10 at 21:58
-3

just

git submodule update --init --recursive

in the root-directory of your project and it should do what you want

Immanuel
  • 161
  • 2
  • 2