Git Submodules vs Git Subtrees
Git Submodules vs Git Subtrees
Subtrees vs Submodules
The simplest way to think of subtrees and submodules is that a subtree is a copy of a repository that is pulled into a parent repository while a submodule is a pointer to a specific commit in another repository.
This difference means that it is trivial to push updates back to a submodule, because we’re just pushing commits back to the original repository that is pointed to, but more complex to push updates back to a subtree, because the parent repository has no knowledge of the origin of the contents of the subtree.
It also means that subtrees are much easier for other people to come and pull, as they are just part of the parent repository.
So an ultra-dumbed-down ELI5 comparison of submodules to subtrees could be:
- Submodules are easier to push but harder to pull – This is because they are pointers to the original repository
- Subtrees are easier to pull but harder to push – This is because they are copies of the original repository
Summary
In my opinion subtrees are not a direct replacement for submodules. The way I believe you should split your shared code between subtrees and submodules is this:
- Is the external repository something you own yourself and are likely to push code back to? Then use a submodule. This gives you the quickest and easiest way for you to push your changes back.
- Is the external repository third party code that you are unlikely to push anything back to? Then use a subtree. This gives the advantage of not having to give people permissions to an extra repo when you are giving them access to the code base, and also reduces the chance that someone will forget to run a
git submodule update
.
If you think I’m a complete idiot who has totally misunderstood and misrepresented submodules or subtrees, please let me know in the comments.
GIT: SUBMODULES VS. SUBTREES
submodule
Remember, a Git submodule is just a link to a specific ref in another repository. When another person clones your repository, it won’t see the Pikaday source there. In order to have that, they will have to run:
git submodule init
git submodule update
An alternative is cloning with the --recursive
option:
git clone --recursive <repo-path>
subtree
Subtrees are much simpler than submodules. As opposed to submodules, subtrees’ sources files are stored in the repo. It’s not just a link, the code is really there. There’s also fewer steps required and fewer changes to the workflow.
As opposed to submodules, someone that clones your repo won’t have to do anything else to have all the code.
Recap
Submodules | Subtrees |
---|---|
Harder (specially for Git beginners) | Easier |
It’s just a link to a commit ref in another repository | Code is merged in the outer repository’s history |
Requires the submodule to be accessible in a server (like GitHub) | Decentralized |
Requires additional steps | Just clone, pull and push in a similar way you are already familiar |
Smaller repository size | Bigger repository size |
Differences between git submodule and subtree
- 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)
What if I want the links to always point to the HEAD of the external repo?
You can make a submodule to follow the HEAD of a branch of a submodule remote repo, with:
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.
- 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.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-06-02 RadioButton的check改变的时候
2017-06-02 Docs-->.NET-->API reference-->System.Web.UI.WebControls-->Repeater
2017-06-02 Docs-->.NET-->API reference-->System.Web.UI-->Control-->Methods-->FindControl
2017-06-02 通过Debug-->Attach to Process的方式来调试网站
2017-06-02 Tools-->SQL Server Profiler监视数据库
2017-06-02 WebForms简介
2016-06-02 Don't Block on Async Code