Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between git submodule and subtree

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

What are the typical scenarios for each?

like image 456
Nathan H Avatar asked Aug 02 '15 08:08

Nathan H


People also ask

What is a git subtree?

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.

Why you should not use git submodules?

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.

Is git submodule good?

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.


2 Answers

submodule is link;

subtree is copy

like image 138
Feng Avatar answered Sep 18 '22 05:09

Feng


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

like image 20
VonC Avatar answered Sep 21 '22 05:09

VonC