405

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

What are the typical scenarios for each?

3

6 Answers 6

466

submodule is link;

subtree is copy

0
245

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)

10
  • 2
    your answer seems to go against the voted answer here: stackoverflow.com/questions/10443627/…
    – Nathan H
    Aug 6, 2015 at 9:10
  • 1
    @NathanH this (the possibility to track HEAD) has been added a year later (March 2013, git 1.8.2: github.com/git/git/blob/…)
    – VonC
    Aug 6, 2015 at 9:11
  • 2
    another point that might be useful: git submodule is a "core" Git command, it's part of the Git codebase. git subtree is in the "contrib" directory, it's not installed by Git's Makefile (though some distros do ship it), so it's less developed and less maintained.
    – philb
    Jul 12, 2021 at 16:24
  • 2
    @philb Good point. I have included your comment in the answer for more visibility.
    – VonC
    Jul 12, 2021 at 17:10
  • 1
    "HEAD" of a branch is nonsensical. It should be "You can make a submodule to follow a branch of a submodule remote repo...". Nov 5, 2021 at 14:50
162

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.

6
  • 4
    But with git subtree you still can also push - if you wanted - right?
    – User
    Jan 21, 2018 at 23:22
  • 2
    @lxx If you know the repository URL… Jan 24, 2018 at 23:05
  • @FranklinYu Why would he not know that? can't get that info from the local git meta data?
    – adi518
    Mar 19, 2018 at 22:53
  • 2
    @adi518 Yes, if you are the one who created the subtree. However, if you pushed your repository to GitHub and others clone it down, I don’t think he/she automatically knows the subtree URL. Mar 20, 2018 at 4:33
  • 2
    @NiklasP - can you elaborate on "reference the new commit in the main repository"? That's the one step I'm not clear on how to execute and therefore "changed reference" isn't something I understand either. Apr 4, 2018 at 16:07
30

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

2
  • 7
    "pushing a main repo to remote pushes sub-tree's files" No, it doesn't.
    – J Bramble
    Jan 16, 2017 at 16:22
  • @JBramble I should probably mention that it's done with the SourceTree app eg: git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree push -v --tags production refs/heads/master:refs/heads/master
    – Maciek Rek
    Feb 23, 2017 at 14:10
9

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 or branch in another repository.

1
4

[Git Submodule - Atlassian]

Git submodule is useful when you want to keep the embedded repository's commit history separate from the main repository. However, using submodules can be complex and difficult to manage, especially when you need to update the embedded repository.

[Git Subtree and comparison with Submodule - Atlassian]

Git subtree is a solution that allows merging one repository into another as a subdirectory, but keeping the entire commit history. It is useful when you want to share a set of files between different projects without the need to maintain a separate repository. Using a subtree is simpler than using a submodule and is generally easier to manage.

In short, if you need to keep the shared repository's commit history separate from the main repository, git submodule might be the best choice. If you need to share a set of files between different projects without the need to maintain a separate repository, git subtree might be the best choice.


Get/Update Workflow Comparison

Let's compare the commands for sending and receiving updates:

1. Submodule

#push updates:
cd path/to/submodule
1. git add .
2. git commit -m "Submodule Update"
3. git push origin master
cd ..
4. git add submodule
5. git commit -m "Submodule ref update"
6. git push origin master
# >Needs to be in this order! Easy to get trouble<

#pull:
git submodule update --remote

2. Subtree

#push updates:
cd path/to/shared/repo
1. git add .
2. git commit -m "Subtree update"
3. git push origin master
#then
4. git subtree push --prefix=path/to/shared/repo shared-repo master

#pull:
git subtree pull --prefix=path/to/shared/repo shared-repo master
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.