git submodule 和 subtree 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31769820/
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-09 03:25:20  来源:igfitidea点击:

Differences between git submodule and subtree

gitgit-submodulesgit-subtree

提问by Nathan H

What are the conceptual differences between using git submodule and subtree?

使用 git submodule 和 subtree 之间的概念区别是什么?

What are the typical scenarios for each?

每个场景的典型场景是什么?

采纳答案by VonC

What if I want the links to always point to the HEAD of the external repo?

如果我希望链接始终指向外部存储库的 HEAD,该怎么办?

You can make a submodule to follow the HEAD of a branch of a submodule remote repo, with:

您可以创建一个子模块来跟随子模块远程仓库分支的 HEAD,使用:

o git submodule add -b <branch> <repository> [<path>]. (to specify a branch to follow)
o git submodule update --remotewhich will update the content of the submodule to the latest HEAD from <repository>/<branch>, by default origin/master. Your main project will still track the hashes of the HEAD of the submodule even if --remoteis used though.

git submodule add -b <branch> <repository> [<path>]。(指定要遵循的分支)
o默认情况下git submodule update --remote,它将子模块的内容更新为最新的 HEAD 。即使使用,您的主项目仍将跟踪子模块的 HEAD 的哈希值。<repository>/<branch>origin/master--remote



回答by Feng

submodule is link;

子模块是链接;

subtree is copy

子树是副本

回答by Niklas P

The conceptual difference is:

概念上的区别是:

With git submodulesyou typically want to separate a large repository into smaller ones. The way of referencing a submodule is maven-style- you are referencing a single commit from the other (submodule) repository. If you need a change within the submodule you have to make a commit/push within the submodule, then reference the new commit in the main repository and then commit/push the changed reference of the main repository. That way you have to have access to both repositories for the complete build.

使用git 子模块,您通常希望将大型存储库分成较小的存储库。引用子模块的方式是maven 风格的- 您正在引用来自其他(子模块)存储库的单个提交。如果您需要在子模块中进行更改,则必须在子模块中进行提交/推送,然后在主存储库中引用新提交,然后提交/推送主存储库的更改引用。这样,您必须有权访问完整构建的两个存储库。

With git subtreeyou integrate another repository in yours, including its history. So after integrating it, the size of your repository is probably bigger (so this is no strategy to keep repositories smaller). After the integration there is no connection to the other repository, and you don't need access to it unless you want to get an update. So this strategy is more for code and history reuse - I personally don't use it.

使用git subtree,您可以将另一个存储库集成到您的存储库中,包括其历史记录。所以在集成之后,你的存储库的大小可能更大(所以这不是保持存储库更小的策略)。集成后与其他存储库没有连接,除非您想获得更新,否则您不需要访问它。所以这个策略更适合代码和历史重用——我个人不使用它。

回答by Maciek Rek

sub-module
pushing a main repo to a remote doesn't push sub-module's files

子模块
推主仓库到远程不压子模块的文件

sub-tree
pushing a main repo to remote pushes sub-tree's files

子树
将主存储库推送到远程推送子树的文件