What are the conceptual differences between using git submodule and subtree?
What are the typical scenarios for each?
A Git subtree is a replica of a Git repository that has been dragged into the main repository. A Git submodule is a reference to a particular commit in a different repository. Git subtrees, which were first introduced in Git 1.7. 11, help you make a copy of any repo into a subdirectory of another.
This is because of some major drawbacks around git submodules, such as being locked to a specific version of the outer repo, the lacking of effective merge management, and the general notion that the Git repository itself doesn't really know it's now a multi-module repository.
Its more accurate to say that git submodules are useful when you want to share code that you also need change along with the consumer of that code. If you're not trying to change the shared code along with the consumer of that code, there are better options for sharing your code.
submodule is link;
subtree is copy
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.
Plus, as noted by philb in the comments, git subtree
is a contrib/
, as opposed to git submodule
(core command)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With