Git 存储库中的 Git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4500305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:40:45  来源:igfitidea点击:

Git repository within Git repository

gitdirectoryrepositoryclonesubdirectory

提问by Buuuh

I have a main git repository A and we are using sources out of another git repository B in a subdirectory of our main project. Now it would be good to have the B repository checked out within the A repository in this used subdirectory. If someone else then clones the repository of course he should get our main repository A and within that automatically the B repository.

我有一个主 git 存储库 A,我们正在使用主项目子目录中另一个 git 存储库 B 的源代码。现在最好在这个使用过的子目录中的 A 存储库中检出 B 存储库。如果其他人随后克隆了存储库,他当然应该获得我们的主存储库 A,并在其中自动获得 B 存储库。

Let me visualize the directory structure:

让我可视化目录结构:

+ main_repository       - the root directory of the main Repository
  + src                 - directory containing the source 
    + foreignRepo       - this should be the root directory of another git repo
  + binaries
  + other

This must be also known in the remote repository, just a local copy doesn't help me, because other people check this out and must be able to compile all the stuff.

这在远程存储库中也必须是已知的,只是本地副本对我没有帮助,因为其他人检查了这一点并且必须能够编译所有内容。

回答by Adrian Petrescu

You'll want to read about Git submodules.

您将需要阅读有关Git 子模块的信息

回答by John McGehee

Use git subtreein git version 1.7.11 and above. git subtreeis superior to git submodulebecause:

git subtree在 git 1.7.11 及以上版本中使用。 git subtree优于 git submodule因为:

  • Management of a simple workflow is easy. Older versions of git are supported (even before v1.5.2)
  • The sub-project's code is available right after the clone of the super project is done
  • git subtreedoes not require users of your repository to learn anything new, they can ignore the fact that you are using subtree to manage dependencies
  • git subtreedoes not add new metadata files like git submoduledoes (such as .gitmodule)
  • Contents of the subtree can be modified without having a separate repository copy of the dependency somewhere else
  • 管理简单的工作流程很容易。支持旧版本的 git(甚至在 v1.5.2 之前)
  • 子项目的代码在完成超级项目的克隆后立即可用
  • git subtree不需要存储库的用户学习任何新东西,他们可以忽略您使用子树来管理依赖项的事实
  • git subtree不像git submodule(例如 .gitmodule)那样添加新的元数据文件
  • 可以修改子树的内容,而无需在其他地方拥有依赖项的单独存储库副本

Additionally, if you need to detach a subtree of your existing repository into a new repository, git subtree splitcan help you with that.

此外,如果您需要将现有存储库的子树分离到新存储库中git subtree split可以帮助您。

UPDATE February 2020:Nowadays I like subrepoeven more.

2020 年 2 月更新:现在我更喜欢subrepo

回答by Sjoerd

You can nest two git repo's without using submodules. Suppose ChildRepo is a subdirectory of ParentRepo, and both are git repositories.

您可以在不使用子模块的情况下嵌套两个 git repo。假设 ChildRepo 是 ParentRepo 的子目录,两者都是 git 仓库。

+ ParentRepo
  - ChildRepo

If you add a file to ChildRepo, ParentRepo will ignore it. When you commit it it will be added to ChildRepo. It is not possible to add files within ChildRepo to ParentRepo.

如果您将文件添加到 ChildRepo,ParentRepo 将忽略它。当您提交它时,它将被添加到 ChildRepo。无法将 ChildRepo 中的文件添加到 ParentRepo。

More info: Nested GIT repo gotchas!

更多信息:嵌套的 GIT 存储库问题!