[git] 12. submodules

$ git submodule add <git path> <directory name for submodule>

For example,

$ git submodule add https://github.com/TheForceJB/GitPracticeSub.git GitPracticeSub

In .gitmodules, we will find the following content,

[submodule "GitPracticeSub"]
path = GitPracticeSub
url = https://github.com/TheForceJB/GitPracticeSub.git

Creating Pull Request

However, if you would like to create some PRs for the submodule.
Instead of adding the target repository as submodule directly, we fork it and add the one in our repository as submodule.

For example, there are 2 repositories in my Github:

  1. KarateJB/GitPractice
  2. KarateJB/GitPracticeSub which is forked from TheForceJB/GitPracticeSub

Add KarateJB/GitPracticeSub as sub-module into KarateJB/GitPractice.
Modify and create PR at KarateJB/GitPracticeSub.

Clone a repository with Submodule

$ git clone <repository_url>
$ cd <repository_name>
$ git submodule init
$ git submodule update

The above commands equals to

$ git clone <repository_url> --recursive

Remove Submodule

Reference : myusuf3/delete_git_submodule.md

  1. Stage for removal and untrack submodule files
$ git rm --cached <path_to_submodule>
  1. Remove files in .git/modules/<submodule>
$ rm -rf .git/modules/<path_to_submodule>
  1. Commit
$ git commit -m "Removed submodule"
  1. Delete the untracked submodule files
$ rm -rf <path_to_submodule>

Reference