Search code examples
gitgit-submodules

How to do git submodules and why to use git submodules


I have found a GitHub project and it uses git submodules.

What is the usage of git submodules?

How to create git submodules?

How do they differ from subtree?

GitSubModule


Solution

  • Git Submodule:

    A submodule in a git repository is like a sub-directory which is really a separate git repository in its own right. This is a useful feature when you have a project in git which depends on a particular versions of other projects

    Git Subtree:

    Git subtree allows you to insert any repository as a sub-directory of another one.the sub-directory would become permanent part of the super project

    Git Subtree and submodule:

    A subtree merge permanently integrates the contents and history of the subtree into the parent repository at the subtree of the merge.

    A submodule is simply a reference to a specific commit in the submodule. A history of changes to referenced commit are kept in the parent module, but no contents or history of the submodule are tracked in the parent module.

    Configure Git submodule:

    • You have a project -- call it MyWebApp that already has a github repo

    • You want to use the jquery repository in your project

    • You want to pull the jquery repo into your project as a submodule.

    • Submodules are really, really easy to reference and use. Assuming you already have MyWebApp set up as a repo, from terminal issue these commands:

      1. cd MyWebApp

      2. git submodule add git://github.com/jquery/jquery.git externals/jquery This will create a directory named externals/jquery and link it to the github jquery repository. Now we just need to init the submodule and clone the code to it:

      3. git submodule update --init --recursive You should now have all the latest code cloned into the submodule. If the jquery repo changes and you want to pull the latest code down, just issue the submodule update command again.

        Please note:

        I typically have a number of external repositories in my projects, so I always group the repos under an externals directory.