Git - Submodule

> Code - (Programming|Computer) Language > Code Versioning > Git

1 - About

Submodules allow you to keep a Git repository as a subdirectory of another Git repository.

If you don't want to modify the code, use a package manager instead (such as Maven or Rubygems).

A submodule is materialzied as:

  • a subdirectory in your working directory.
  • with its metadata into the gitmodules file.

Git sees it as a submodule and doesn’t track its contents when you’re not in that directory. Instead, Git sees it as a particular commit from that repository.

3 - Management

3.1 - Create

git submodule add <repoId> targetSubDirectoryName

This command add a .gitmodules file.


3.2 - Commit

After a commit of the first submodule, we see a:

2 files changed, 4 insertions(+)
 create mode 100644 .gitmodules
 create mode 160000 elastic

3.3 - Clone

git clone --recurse-submodules

Without the –recurse-submodules option, you get the directories that contain submodules, but none of the files within them. The submodule directories are there, but empty. You must run two commands to get the files:

  • 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.

3.4 - .gitmodules

The .gitmodules file stores the mapping between the project’s URL and the local subdirectory.

[submodule "subModuleName"]
        path = targetSubDirectoryName
        url =

3.5 - Relocate


git config submodule.subModuleName.url YOUR_URL

3.6 - Diff

3.6.1 - Before commit

git diff --cached --submodule

3.7 - Changes

3.7.1 - Pulling in Upstream Changes

  • For all module:
cd parentDirectory
git submodule update --remote # By default from the master branch 
# Modifying the branch 
# Track the change for the subModule not on master but on the branchv1
# -f .gitmodules add the config to the gitmodule file (for everybody then)
git config -f .gitmodules submodule.SubModuleName.branch branchv1 
  • For one module:
cd subdirectoryModule
git fetch
git merge


git config --global diff.submodule log #  setting the otuput default format of diff by setting the diff.submodule config value to “log”.
cd parentDirectory
git diff

3.7.2 - Applying its own changes

By default, submodule are running in a detached HEAD state.

It means that there is no local working branch (like “master”, for example) tracking changes. With no working branch tracking changes, even if you commit changes to the submodule, those changes will be lost the next time you run git submodule update. To track its own change, we need to create a branch.

cd subModuleDir
git checkout myBranch 
git submodule update --remote --merge
... to continue ... to difficult to follow today

4 - Documentation / Reference

code/version/git/submodule.txt · Last modified: 2017/12/06 12:09 by gerardnico