Git - Submodule

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 - Example

Used by arrow:

git submodule update --init --recursive # Needed for flight

4 - Management

4.1 - Create

git submodule add <url> <path>

This command create a .gitmodules file.

4.2 - Update Submodule to a new Version

git -C <path> checkout <new version>
git add <path>
git commit -m "update submodule to new version"


4.3 - List

To list the submodules, use the status command

git submodule status

4.4 - Commit

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

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

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

4.6 - .gitmodules

The .gitmodules file stores the configuration such as:

  • the mapping between the project’s URL and the local subdirectory.
  • the active status

[submodule "subModuleName"]
        path = targetSubDirectoryName
        url =
[submodule "foo"]
  active = false
  url =
[submodule "bar"]
  active = true
  url =
[submodule "baz"]
  url =

4.7 - Relocate


git config submodule.subModuleName.url YOUR_URL

4.8 - Diff

4.8.1 - Before commit

git diff --cached --submodule

4.9 - Changes

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

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

5 - Documentation / Reference

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap