Differences between git submodule and subtree

2019-01-03 19:53发布

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

What are the typical scenarios for each?

4条回答
▲ chillily
2楼-- · 2019-01-03 20:24

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-03 20:34

The conceptual difference is:

With git submodules you 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.

With git subtree you 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.

查看更多
Lonely孤独者°
4楼-- · 2019-01-03 20:40

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.


查看更多
做个烂人
5楼-- · 2019-01-03 20:49

submodule is link;

subtree is copy

查看更多
登录 后发表回答