6

I need to add some JS library to my project and I also want to have ability to update that library from it's repo (that's why I'm adding it as submodule). But I need only on 1 file from that repo to be placed withing my JS directory, whereas library repository contain tests, coffeescript sources and so on, which I do not need in my repository. So the question is - how to add not whole repo as submodule, but single file/directory from it?

I suppose this is somehow possible with help of Git Sparse checkouts but not sure if I'm on the right way with this...

SET001
  • 11,480
  • 6
  • 52
  • 76

2 Answers2

8

It is possible and I've done it many times.

All you need to do is to create a branch of the repository that you are adding as a submodule and strip out all the code that you don't want and add the submodule with that branch.

As an example, look at this repository

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • Thank you, Abizem, this looks like a good solution, But since I'm very weak in git I can't image how I can set up auto update from dev to master branch. Without auto update, if I have to do it manually it would be almost the same as if I would replace that file without even using submodules. – SET001 Jan 08 '14 at 03:58
  • I don't understand what you mean by "from dev to master" and "autoupdate". – Abizern Jan 08 '14 at 04:00
  • `master` branch now contain only one final compiled file in the root of repo, `development` contain everything needed for development and this same file somewhere in subdirectory. Now if something would be pushed in `development` branch - file in `master` also should be updated - this is what I mean autoupdate. Are there any way to do this? – SET001 Jan 08 '14 at 04:13
  • Just do a merge. If you removed the files correctly from master, then you'll hardly have to clean anything up. – Abizern Jan 08 '14 at 04:16
  • it was not obvious for that file in /foo/bar/blah.js from one branch will be merged with blah.js of another branch. Still can't explain how but this work ) – SET001 Jan 08 '14 at 09:08
  • This isn't a good answer. You'll still need to manually update the _submodule branch_ with `master` . Defeats the purpose of submodule then – Pants Aug 16 '22 at 18:25
6

To add to Abizern's answer (upvoted), you can declare your submodule (set to the branch which contains the files you want) to follow that branch (git submodule man page):

git submodule add -b yourSubmoduleBranch /url/to/your/submodule/repo

If you have already added your submodule, without the -b, you can add that option easily enough: see "Git submodules: Specify a branch/tag".

# Make sure your submodule is actually at the latest of that branch:
cd path/to/your/submodule
git checkout -b branch --track origin/branch
# record that on the parent repo
cd /path/to/your/parent/repo
git config -f .gitmodules submodule.<path>.branch <branch>
git add yourSubmodule
git commit -m "Submodule now follows branch"

All you need to do for your submodule to reflect your latest modifications is now, on the parent repo level:

git submodule update --init --remote --recursive

As noted by frmbelz in the comments, my 2018 answer using info/sparse-checkout or git clone --filter does allow to pull just a couple of files into submodule.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • VonC, looks your later [answer](https://stackoverflow.com/a/53233492/1496974) does this. Using that answer I could pull just a couple of files into submodule. – Vladislav Povorozniuc Feb 16 '23 at 16:01
  • @frmbelz Thank you for your feedback, good point. I have included your comment in the answer for more visibility. – VonC Feb 16 '23 at 17:27