如何嵌套 git 存储库;获取和合并



所以我有两个git存储库。第一个是我们的框架(想想数据库抽象,函数),然后是我们新项目的另一个git存储库。

我想将框架 git 存储库包含在项目 git 中,根据 GitHub 帮助,这应该可以工作:

 cd /project
 git remote add framework git://github.com/username/Framework.git
 git fetch framework
 git merge framework/master

问题是,当我进行合并时,它会从框架中获取所有文件,并简单地将它们转储到项目的根目录中。相反,我们如何将框架文件合并到像 /project/framework/ 这样的子目录中?

你可能想看看 Git 的子模块支持。 子模块允许您将一个 git 存储库嵌入到另一个 git 存储库中。 这种事情有替代解决方案,但我自己没有使用过它们。

示例可能如下所示:

$ git clone git://github.com/username/project.git
$ cd project
$ git submodule add git://github.com/username/framework.git framework
$ git commit -m "added framework submodule"

如果要克隆包含子模块的存储库,则需要使用 --recursive 选项:

$ git clone --recursive git://<repository-with-submodules>.git

或者,您可以定期克隆,然后运行:

$ git submodule init
$ git submodule update

阅读链接的文档(和git submodule --help)以获取更多信息。

如果对子模块进行了更改,则可以像这样引入它们:

# first update the submodule just like any other git repository
$ cd project/framework
$ git pull
# now you have to record the new commit in the parent repository
$ cd ..
$ git commit -m "updated framework submodule"

最后一步是必要的,因为 git 会记录与给定子模块关联的特定提交(这样当任何人克隆父模块时,他们将获得该版本的子模块,而不是其最新的修订版,后者可能经历了重大更改,这会阻止它与父存储库按预期工作)。 因此,如果更新子模块,则需要在父模块中记录新提交。

如果在framework子模块中进行更改,您将再次像使用任何其他存储库一样git push它们。 然后,您必须在父模块中提交新版本。

最新更新