【问题标题】:How to nest git repositories; fetch and merge如何嵌套 git 存储库;获取并合并
【发布时间】:2012-05-06 17:21:36
【问题描述】:

所以我有两个 git repos。第一个是我们的框架(想想 db 抽象、函数),然后是我们新项目的另一个 git repo。

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

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

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

【问题讨论】:

标签: git version-control git-merge git-fetch


【解决方案1】:

您可能想了解 Git 的 submodule 支持。子模块允许您将一个 git 存储库嵌入到另一个 git 存储库中。有alternative solutions这种东西,但我自己没用过。

一个示例可能如下所示:

$ 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 它们。然后,您必须在父模块中提交新修订。

【讨论】:

  • 完美,所以当对框架进行更改时,既然它是一个模块,我如何将它们拉入项目中?另外,如果我对框架而不是项目进行更改,我如何将它们推回框架 git 存储库?
  • 一张小纸条。子模块 HEAD 已分离,因此您需要小心在子模块中进行更改。创建一个分支(或切换到一个分支),进行更改,提交然后推送。
猜你喜欢
  • 1970-01-01
  • 2018-04-25
  • 2017-02-03
  • 2010-11-28
相关资源
最近更新 更多