How to Update Submodule in Git

Abdul Jabbar Feb 02, 2024
How to Update Submodule in Git

Submodules are a way to keep a Git repository as a subdirectory in a current branch. Submodules are usually imported from a third-party repository. For example, a large project may have a submodule that contains a library.

The submodule can be used in any part of the project. It can have its submodules and so on, depending on the requirements. They are Git repositories fixed inside a parent Git repository at a particular path in the parent repository’s working directory.

To develop a submodule, we use git submodule init to create a .gitmodules file in the project’s root directory, which contains a list of submodules we intend to utilize in the upcoming tasks. Then, we will use the command git submodule update to pull in our submodule.

To update a submodule, we should specify the submodule path in the parent repository. To access a submodule, we must always specify the path relative to the parent repository. The submodule path is thus relative to the project.

To avoid the need to specify the submodule path, we can set the submodule path in the gitmodules file. The gitmodules file is a plain text file at the root of the repository. The gitmodules file defines the mapping between the current repository’s parent repository and the submodule.

Update Git Submodules

We will show you how to update the Git submodules in our workspace with the recent commits on the server.

  • Clone the remote repository, if we haven’t already.
  • Publish a git submodule update -remote command.
  • List any new files pulled from the repository to the Git index.
  • Execute a git commit.
  • Push back to the origin.

The git submodule command has a parameter called the --update that can be used to get the latest code from the submodule.

git submodule --update 
SomeSubmodule $ git submodule --sync
SomeSubmodule $ git submodule update --init 

We can update the submodule to the latest commit with the update parameter. To avoid a lot of complicated checkouts and push commands, it is much simpler to make a quick alias for the submodule update command.

git submodule update --remote --merge

The above command updates all the submodules, ensuring that the working tree is clean and in sync with the remote branch. To avoid getting a merge commit every time, use the -merge flag to merge the updates. Using Git submodules is like a double-edged sword because it can make our development life a lot easier, but it can also make our life a bit harder if we don’t know how it works. The more we work with it, the more we will get to know how they work, and therefore be able to leverage them to our advantage.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Submodule