Differences between git submodule and subtree

一个人想着一个人 提交于 2019-11-26 03:31:47

问题


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

What are the typical scenarios for each?


回答1:


  • 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.



回答2:


submodule is link;

subtree is copy




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/31769820/differences-between-git-submodule-and-subtree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!