First let's explain what is the main difference between subtree and submodule:
both of them are used for having another repo inside existing repo. The main difference is that git submodule
is independent self-contained repository while subtree
store the date in the parent (original) repo.
Now let's dig in and explain in more details:
Is there any simpler example that I can follow?
Submodule
is a standalone git project so the code will be checked out to a new folder under the root folder and it's not part of your master branch.
Your root folder will contain a submodule file and you will have to init && update
it on every clone you make.
# Add the desired submodule to your code base
git submodule add <url>
You must run two commands:
git submodule init
to initialize your local configuration file, and
git submodule update
to fetch all the data from that project and check out the appropriate commit listed in your superproject:
So the full script is this:
git submodule add <url>
git submodule init
git submodule update
You simply need to be in your root folder and then add the submodule folder.
git submodule add <url>
Now when you clone the project you simply need to init and update the submodule
git submodule init
git submodule update
Git 1.8.2 features a new option --remote
git submodule update --remote --merge
will fetch the latest changes from upstream in each submodule, merge them in, and check out the latest revision of the submodule.

git subtree
Git subtree allows you to insert any repository as a sub-directory of another one
Very similar to submodule
but the main difference is where your code is managed. In submodules the content is placed inside a separate repo and is managed there which allow you to clone it to many other repos as well.
subtree
is managing the content as part of the root project and not in a separate project.
Instead of writing down how to set it up and to understand how to use it you can simply read this excellent post which will explain it all.
https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/