'Differences between git submodule and subtree
What are the conceptual differences between using git submodule and subtree?
What are the typical scenarios for each?
Solution 1:[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.
Plus, as noted by philb in the comments, git subtree
is a contrib/
, as opposed to git submodule
(core command)
Solution 2:[2]
submodule is link;
subtree is copy
Solution 3:[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.
Solution 4:[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
Solution 5:[5]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Feng |
Solution 3 | waldyrious |
Solution 4 | Maciek Rek |
Solution 5 | Pervaiz Iqbal |