How to Pull the Latest Git Submodule

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.

Watch a course Git & GitHub - The Practical Guide

Steps to pulling the latest submodule

While working with submodules, it is often necessary to pull the latest git submodule. In case you are doing it the first time, you should initiate submodules on their repository. Follow the steps below:

Setting up a submodule

Create a new submodule by running:

git submodule update --init --recursive

Updating to latest tips of remote branch:

If the Git version, you are working on is 1.8.2 or above, you can add the --remote parameter, and run the following command:

git submodule update --recursive --remote

The --remote parameter will add your non-default branches to particular files in .git/config or .gitmodules.

If your Git version is 1.7.3. Or above, you need to run:

git submodule update --recursive

or

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 principal repository, the submodule will be in the “Changes not staged for commit” with the following text: “modified content. It will check out the code of the submodule on another commit, not the one that your main repository is pointing at. If you want the principal repository to point at the new commit, you need to invoke the git add command, commit, and finally push it.

Cloning git submodules

Another common action for git submodules is cloning a project with submodules. You can proceed by using the git clone command. With the help of this command, you can clone the directories with their submodules. Note that it won’t clone the files within them. You need to run git submodule init andgit submodule update. The first command updates your local .git/config file along with the mapping from the .gitmodules. The second command will fetch the overall data from the submodule project, checking out the mapped commit in the parent project.

Generally, if you want to keep your projects in different repositories, using submodules is the right decision. Anyway, you should take into consideration the fact that working with submodules is quite tricky, and you can’t use them for all your projects.