Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git subtree: Use only subrepository instead of whole repository

I have a project that uses some 3rd Party libraries. So the directory structure is something like:

MY_COOL_PROJECT
   3rdParty
      LIB_1
      LIB_2
   Source
      MY_PROJECT   

The libraries are located in separate repositories. So, if I want to use a git repository for the 3rd Party libraries I can do:

git subtree add --prefix 3rdParty/LIB_1 --squash http://My3rdPartyLibs.com/lib1.git master

However, inside lib1.git repository there is only one bin folder I need. It contains also folders such as documentation, examples, etc. How can I only "connect" my repository with lib1/bin folder instead of the whole repository? Is that even possible?

like image 956
user2070238 Avatar asked Feb 13 '13 22:02

user2070238


People also ask

How does git subtree work?

git subtree allows you to nest a repository as a subdirectory inside another. It's one of the various options for managing project dependencies in Git projects. You add a subtree to an existing repository where the subtree is a reference to another repository URL and branch/tag when you wish to utilize it.

What is git subtree split?

Splitting the Original Repository The subtree commands effectively take a folder and split to another repository. Everything you want in the subtree repo will need to be in the same folder.

What is prefix in git subtree?

The –prefix parameter defines the root directory for the cloned repository, then add the remote url, the branch and let it squash the entire commit history (–squash). Git doesn't like uncommitted changes so make sure to stash/commit any existing changes before adding a new subtree.

Are git submodules a good idea?

Git submodules may look powerful or cool upfront, but for all the reasons above it is a bad idea to share code using submodules, especially when the code changes frequently. It will be much worse when you have more and more developers working on the same repos.


1 Answers

Normally, a git repo is done to be fully cloned/loaded.

You could go for a sparse checkout (Git1.7+), but only if you don't intent to do any modification and push those back. See this example:

$ git init
Initialized empty Git repository in /tmp/si-sandbox/.git/
(master) $ git config core.sparsecheckout true
(master) $ echo message-store/ >> .git/info/sparse-checkout
(master) $ git remote add origin git://github.com/iwein/Spring-Integration-Sandbox.git
(master) $ git pull origin master

The OP user2070238 reports:

This worked with a few changes.
Because, I use submodule I had to use

 echo MY_FOLDER/* >> .git/info/modules/MY_MODULE/sparse-checkout 

And for some reason the MY_FOLDER/ part was not working without *

like image 51
VonC Avatar answered Oct 16 '22 15:10

VonC