W3docs

How to Pull the Latest Git Submodule

Learn more about git submodules in this short tutorial. Here you will also find out how to pull the latest git submodule in a relatively fast and easy way.

It is possible to include different git repositories into a single repository in Git. The included repositories are known as submodules. Submodules are repositories that are involved in the parent repository at a particular path in the working directory of the parent repository.

Steps to pulling the latest submodule

While working with submodules, it is often necessary to pull the latest changes. Follow the steps below:

Initializing and updating submodules

To initialize and update submodules to the commits recorded in the parent repository, run:

git submodule update --init --recursive

Updating to the latest commits on remote branches

To update submodules to the latest commit on their remote tracking branches, use the <kbd class="highlighted">--remote</kbd> parameter:

git submodule update --recursive --remote

The <kbd class="highlighted">--remote</kbd> parameter fetches the latest changes from the remote repository and updates the submodule to the newest commit on its default branch.

Pulling with recursive submodules

You can also pull changes in the parent repository while automatically updating all submodules:

git pull --recurse-submodules

Pushing updates in git submodule

As the submodule represents a separate repository, it allows running the git push command in the directory of the submodule.

If you invoke the git status command in your parent repository, the submodule will show as “Changes not staged for commit” with the message “modified content”. This happens because the submodule is checked out at a different commit than the one your parent repository is pointing at. If you want the parent repository to track the new commit, you need to invoke the git add command, commit, and finally push the changes in the parent repository.

Cloning git submodules

Another common action for git submodules is cloning a project with submodules. You can proceed by using the git clone command. By default, cloning only downloads the parent repository. To automatically fetch the submodule contents, use:

git clone --recurse-submodules <repository-url>

Alternatively, you can clone the parent repository first and then run <kbd class="highlighted">git submodule init</kbd> followed by <kbd class="highlighted">git submodule update</kbd>. The first command updates your local .git/config file with the mappings from <kbd class="highlighted">.gitmodules </kbd>. The second command will fetch the data from the submodule projects and check out the recorded commits in the parent project.

Generally, if you want to keep your projects in different repositories, using submodules is the right decision. However, you should take into consideration that working with submodules can be tricky, and they are not suitable for all projects.