重用 git 存储库的一部分 [英] Reuse a Part of a git Repository
问题描述
我有以下项目设置:
方案A
Project 1
(轻量级组件)Project 2
(包含大量文件,依赖于Project 1
)
解决方案 A
是单个 git 存储库.然后我创建了另一个解决方案,发现我可以重用甚至更新 Project 1
的功能.所以我的第二个解决方案可能看起来像这样:
方案B
Project 1
(必须分享!)Project 3
(依赖于Project 1
).
现在我想让 Project 1
成为一个共享组件.也就是说,每次我从任一解决方案(A
或 B
)更改 Project 1
的源代码时,我都需要另一个更新相应地.
也许这与 git 的 submodule 功能有关.但是,我能够使用它的唯一方法是将整个 Solution A
指定为 Solution B
的子模块.由于解决方案A
的巨大规模,这并不是我理想中想要的.我只需要它的一小部分就可以成为子模块.
我知道在 svn 中是可能的,并且完全按照我的描述工作:您在 svn:externals
属性中指定外部存储库中的目录.>
有什么提示吗?或者,我遗漏了什么?
这肯定与子模块有关(参见 子模块的性质)
在您的情况下,理想的解决方案是从 SolutionA
Git 存储库中提取 Project1
:
请参阅 如何提取 git 子目录和用它制作一个子模块?.
但这涉及重写 SolutionA 历史记录,如果您已经发布了它并且如果有人从中提取,这是一个问题.
使用 filter-branch 进行提取过程.
<块引用>重写存储库以使其看起来好像 Project1/
是其项目根目录,并丢弃所有其他历史记录:
git filter-branch --subdirectory-filter Project1 -- --all
<块引用>
因此,例如,您可以将库子目录转换为自己的存储库.请注意将 filter-branch
选项与修订选项分开的 --
,以及用于重写所有分支和标签的 --all
.
然后在SolutionB
中声明Project1
为子模块:
cd SolutionBgit submodule add/path/to/Project1 Project1
<块引用>
注意:如果您计划发布您的 SolutionB
,请勿在此处使用本地 URL!
git commit -m "添加子模块 Project1"
I have the following project setup:
Solution A
Project 1
(a lightweight component)Project 2
(contains a lot of files and depends onProject 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 onProject 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?
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 separatesfilter-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"
这篇关于重用 git 存储库的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!