git submodule 和 subtree 的区别 [英] Differences between git submodule and subtree
问题描述
使用 git submodule 和 subtree 有什么概念上的区别?
What are the conceptual differences between using git submodule and subtree?
每种情况的典型场景是什么?
What are the typical scenarios for each?
推荐答案
- submodule 更适合基于组件的开发,您的主要项目取决于在另一个组件的固定版本(repo)上.
您只保留父存储库中的引用(gitlinks、索引中的特殊条目) - submodule is a better fit for component-based development, where your main project depends on a fixed version of another component (repo).
You keep only references in your parent repo (gitlinks, special entries in the index) - subtree 更像是一个基于系统的开发,其中您的所有存储库包含所有内容一次,您可以修改任何部分.
请参阅此答案中的示例. - subtree is more like a system-based development, where your all repo contains everything at once, and you can modify any part.
See an example in this answer.
如果我希望链接始终指向外部存储库的 HEAD 怎么办?
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:
o git submodule add -b
.(指定要遵循的分支)
o git submodule update --remote
它将子模块的内容更新到
的最新 HEAD,默认为 origin/主
.即使使用了 --remote
,您的主项目仍将跟踪子模块的 HEAD 的哈希值.
o git submodule add -b <branch> <repository> [<path>]
. (to specify a branch to follow)
o git submodule update --remote
which 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 --remote
is used though.
另外,正如 philb 在 评论,git subtree
是一个 contrib/
,而不是 git submodule
(核心命令)
Plus, as noted by philb in the comments, git subtree
is a contrib/
, as opposed to git submodule
(core command)
这篇关于git submodule 和 subtree 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!