Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse a Part of a git Repository

I have the following project setup:

  • Solution A
    • Project 1 (a lightweight component)
    • Project 2 (contains a lot of files and depends on Project 1)

Solution A is a single git repository. Then I created another solution and found that I could reuse and even update the functionality of Project 1. So my second solution would probably look like this:

  • Solution B
    • Project 1 (must be shared!)
    • Project 3 (depends on Project 1).

Now I want Project 1 to become a shared component. That is, every time I change the source code of Project 1 from either solution (A or B), I need the other one to update accordingly.

Maybe this has something to do the the submodule feature of git. However, the only way I was able to use it is to specify the whole Solution A as a submodule for Solution B. This is not really what I want ideally due to enormous size of Solution A. I only need a tiny part of it to be a submodule.

I know that it's possible in svn and works exactly as I've described: you specify a directory within an external repository in the svn:externals property.

Any tips on that? Or maybe, I'm missing something?

like image 760
Kerido Avatar asked Feb 24 '10 13:02

Kerido


1 Answers

This is definitely related to submodules (see the nature of submodules)

In your case, the ideal solution would be to extract Project1 from SolutionA Git repo:
See How to extract a git subdirectory and make a submodule out of it?.

But that involves rewriting SolutionA history, which is a problem if you have already published it and if some people are pulling from it.

Using filter-branch for the extraction process.

To rewrite the repository to look as if Project1/ had been its project root, and discard all other history:

git filter-branch --subdirectory-filter Project1 -- --all

Thus you can, e.g., turn a library subdirectory into a repository of its own. Note the -- that separates filter-branch options from revision options, and the --all to rewrite all branches and tags.

Then declare Project1 as a submodule in SolutionB:

cd SolutionB
git submodule add /path/to/Project1 Project1

NOTE: Do not use local URLs here if you plan to publish your SolutionB!

git commit -m "Add submodules Project1"
like image 194
VonC Avatar answered Sep 24 '22 02:09

VonC