git submodules

 git submodules allows to link one or more related git projects, which allows to build and execute commands in the dependent projects in a seamless way both on development machines and CI servers.

This post explains about some basic commands required with git repositories containing git submodules.

Add a new submodule:

git submodule add <git_repo_url> <submodule_path>

git add .

git commit -m "Added Submodule"

git push

Remove an existing submodule:

git submodule deinit <submodule_path>

git rm <submodule_path>

rm -rf .git/modules/<submodule_path>

git add .

git commit -m "Removed Submodule"

git push

Pull changes to submodules in already cloned repository:

git pull --recurse-submodules

git submodule update --init --recursive

Pull changes to submodules in parallel:

The -j parameters specifies the maximum number of parallel jobs to pull the submodules. In the below example -j8 means, up to 8 submodules will be pulled in parallel. The will speed up the pull operation as the submodules will be updated parallely.

git pull --recurse-submodules -j8

git submodule update --init --recursive -j8

Clone a repo with submodules:

git clone --recurse-submodules <git_url>

Clone a repo with submodules in parallel:

The -j parameters specifies the maximum number of parallel jobs to pull the submodules. In the below example -j8 means, up to 8 submodules will be pulled in parallel. This will speed up the cloning time of the parent repo.

git clone --recurse-submodules -j8 <git_url>

Clone a repo with submodules in CI:

The --shallow-submodules makes the clone command to pull the submodules only with the configured commit id and avoids cloning the submodules with entire history. This is a very handy option in CI servers to speed up the clone time and ultimately reduced the CI build time.

git clone --recurse-submodules --depth 1 --shallow-submodules -j8 <git_url>

Update the commit to latest of the submodule from remote server:

All submodules

git submodule update --init --remote

git add .

git commit -m 'Updating all submodules'

git push

Specific submodule

git submodule update --init --remote <submodule_path>

git add .

git commit -m 'Updating submodule'

git push

Update remote branch of the submodule:

Set to a specific branch

git submodule set-branch --branch <branch_name> <submodule_path>

git add .

git commit -m 'Updating branch'

git push

Set to a default branch (HEAD)

git submodule set-branch --default <submodule_path>

git add .

git commit -m 'Updating to default branch and HEAD'

git push

Update the remote url of the submodule:

git submodule set-url <submodule_path> <new_url>

git add .

git commit -m 'Updating remote url'

git push

Print status of the submodule:

All submodules

git submodule status --recursive

Specific submodule

git submodule status --recursive <submodule_path>

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux