141

Skimming through the SubModule tutorial, I created a submodule out of the boto project. Then, I discovered that I actually need only a subset of this project - specifically, the boto folder.

I would like to change my submodule to point to this folder. When I look in .gitmodules, I see

[submodule "backup/src/boto"]
    path = backup/src/boto
    url = https://github.com/boto/boto.git

What URL should I use instead of https://github.com/boto/boto.git? After I change the URL, should I delete the boto folder locally and re-pull?

ripper234
  • 222,824
  • 274
  • 634
  • 905
  • 5
    It's not exactly what you want - not a submodule - but you might have a look at [`git subtree`](https://github.com/apenwarr/git-subtree) – Cascabel Mar 14 '11 at 23:18
  • 2
    What I ended up doing is having the entire submodule, and telling IntelliJ that the boto folder is a 'source folder', so it can find packages in it. – ripper234 Mar 15 '11 at 07:12
  • 29
    I can't believe git doesn't do this natively...yikes. – rogerdpack Dec 06 '12 at 19:20
  • 4
    Found a similar question - https://stackoverflow.com/questions/1121227/git-submodule-to-a-subfolder – sashoalm Jan 13 '15 at 10:15

5 Answers5

85

I'm afraid the URL for submodules always just points to the repository - you can't specify that you only want a subfolder of a repository, in the same way that git doesn't support "narrow clones" in general.

If you can't live with having the whole repository as a submodule, you could always create a new repository that's cloned from boto and then set up a cron job to:

  1. git fetch that repository into a directory
  2. Use git filter-branch to update a branch where the subdirectory is at the top level.
  3. Add that branch of the repository as the submodule. However, that's all a bit fiddly and my preference would just be to live with having the whole repository as a submodule.
Ballsacian1
  • 17,214
  • 2
  • 26
  • 25
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
33

You cannot clone only a part of a repository. This is because git treats the repository as a whole object : when you get it, you get it all.

So, the solution here would be to fetch the submodule in another directory, then use a symlink to achieve your goal.

Artefact2
  • 7,516
  • 3
  • 30
  • 38
22

All the answers here are pretty dated. You could use the newer git sparse-checkout command docs here, further examples in this article to grab pieces of a repo. This is effective if you only want a directory or two from a larger git project.

TLDR:

git sparse-checkout init --cone
git sparse-checkout set <dir1> <dir2> ...
git checkout main
Justin Hammond
  • 595
  • 8
  • 20
  • 4
    That seems fine if the files are you in YOUR (mono) repo, but what about when the files you want are a subset of a different mono repo - aka a subfolder of a submodule? – FuriousGeorge Jan 05 '23 at 20:16
  • The article says, `if you ran git sparse-checkout set A/B, then Git would include files with names A/B/C.txt (immediate child of A/B) and A/D.txt (immediate sibling of A/B) as well as E.txt (immediate sibling of A).` Personally, I consider this limiting: I only want `A/B/C.txt`. In the context of the question, here's my take: "`sparse-checkout` lets you exclude *root subdirs* from *your repo*. `submodule` lets you include the root of *another repo* anywhere in your repo." – Daniel Kaplan Mar 11 '23 at 04:01
18

What you want to do is create a branch in the submodule and move the folder up and delete what you don't need. You can then manage that branch instead. If you want to push up your changes, you should be able to back merge first. Git will know that you moved the files and do the merge successfully.

Hope this helps.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • 2
    What is a "back merge" ? Can you offer an example? – Sukima Jul 08 '13 at 21:32
  • a merge that goes "upstream" - from a higher order branch to a lower order one. Linus hates those. They tie in history from other branches that may have been merged already. – Adam Dymitruk Jul 08 '13 at 21:36
  • Could you please add some example about "back merge" in this situation? – Xiao Aug 06 '13 at 07:09
  • Typically it's when you try and keep your feature branch current by merging in things from a mainline branch where everyone is merging their latest changes. This is bad because it ties your branch to others' work. If the product requires your branch but not one of the other ones already in the integration branch, it's not possible. – Adam Dymitruk Aug 09 '13 at 03:05
  • 1
    interesting idea, have anyone tried this? I am going to try soon, but still not sure that "back merge" can be clear and automated... – moudrick Aug 17 '17 at 17:49
1

Save the submodule to the submodules/ directory, and then create a symlink:

git submodule add https://github.com/user/repo submodules/repo
ln -s submodules/repo/subdir .
git add subdir

Result:

./
  submodules/
    repo/
      subdir/

  subdir  -->  ./submodules/repo/subdir

One benefit to this approach is that it can be reused with multiple subdirectories.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135