How to include subfolder from one git repo to another?
Say I have Project1 in a git repo. Within Project1 there is a subfolder that I want to include in Project1+ (Project2, Project3, etc.) By “include” I don’t mean copy-and-paste. I want a reference to Project1 so that if I make changes to Project1 I can automatically integrate those changes in Projects1+. Further, I want to place it in more than one location within Project1+.
Project1 |-Subfolder1 |-Stuff I want... |-README.md Project2 |-Subfolder1 <- from Project1 |-SomeOtherFolder |-Subfolder1 <- from Project1 |-README.md
Git submodules won’t work because:
- The submoldule is the entire repo, not the subfolder. I don’t want the entire repo, just the one folder.
The subtree merge strategy won’t work because:
- Only one subtree is supported; when you do the pull for the subtree (git pull -s subtree…) it only updates the one at the highest level (no matter the order in which they were created).
Now bear in mind I am new to git so it’s possible the above conclusions are wrong. But this is what I have discovered through both research and experimentation.
The possible solution I have come up with is:
- Create a branch for Project1.
- Delete everything I don’t want from this branch.
- Move the files I DO want to the root of this branch.
- Delete Subfolder.
- Add this branch as a submodule to Project1+.
This sorta-kinda worked. I modified the Project1 master branch and was able to merge the changes into the new branch, and therefore update the submodule, but this seems unnecessarily complex (given how easy it is in SVN). Furthermore when I merged the changes in Project1 it seemed to consider the deleted files as conflicted; i.e. it was just a huge PITA to update Project1. Maybe I need a different merge strategy?
But mainly I’m asking is this even the right path at all?
For the record, this is trivially easy with SVN Externals. So the use-case is there. And I realize it’s entirely possible no one is doing this with git. The explanations I can find online after a couple days of research all boil down to “restructure your projects so each repo has what you want to share”; i.e. “Subfolder” from Project1 becomes its own repo. I’m hoping that’s not really the final answer.
One Solution collect form web for “How to include subfolder from one git repo to another?”
Anyway, for all possible workflows you have to use 3 repositories: Subfolder1 becomes separate repo, which you’ll use in “superrepos”
- I think, you can use even submodules (with the note above)
- IMHO (very humble), git-subtree is better iteration and will be complete replacement of uly submodules in nearest future. Sharing code between projects with git subtree demonstrate nice use-case for situation almost identical to your with detailed step-by-step illustrated with commands guide of converting monolitic repo into linked by subtree 2 repos, there separated repo is ready to re-usage