Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do git sub modules and why to use git sub module

I have found an github project and it uses git submodules.

What is the usage of git sub modules ?

How to create git sub modules ?

How they differ it from sub-tree ?

GitSubModule

Thanks

like image 671
Manju Avatar asked Aug 03 '15 14:08

Manju


People also ask

Why do we need git submodule?

Git submodules allow you to keep a git repository as a subdirectory of another git repository. Git submodules are simply a reference to another repository at a particular snapshot in time. Git submodules enable a Git repository to incorporate and track version history of external code.

When should I use a git submodule?

In most cases, Git submodules are used when your project becomes more complex, and while your project depends on the main Git repository, you might want to keep their change history separate. Using the above as an example, the Room repository depends on the House repository, but they operate separately.

Is git submodule a good idea?

Git submodules may look powerful or cool upfront, but for all the reasons above it is a bad idea to share code using submodules, especially when the code changes frequently. It will be much worse when you have more and more developers working on the same repos.

What does git submodule init do?

Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.


1 Answers

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.

like image 193
Aruna M Avatar answered Sep 20 '22 08:09

Aruna M