Update Git submodule to latest commit on origin
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Git submodules are a powerful tool used in software development, enabling a Git repository to incorporate and track versions of other Git repositories. This feature is especially handy when your project depends on third-party libraries or when you want to separate different parts of a project into distinct repositories. One common task you may need to perform with submodules is updating them to their latest commit on their respective origins (i.e., the remote repositories they track). Here’s a detailed guide on how to update a Git submodule to the latest commit on origin.
Understanding Git Submodules
Before diving into updating a submodule, it's essential to grasp what a submodule is and how it functions within a Git project. A submodule in Git is essentially a link to a specific commit in another repository, allowing you to keep separate version control projects within a single repository without merging them. This is beneficial for managing dependencies and modularizing projects.
Updating Git Submodule to Latest Commit
Step 1: Clone the Main Repository
Before you update any submodules, you need to have the main Git repository cloned on your local machine. If the repository already has submodules, you can clone it using the --recurse-submodules option to automatically initialize and update each submodule:
Step 2: Update Submodule
Assuming you are in the main project directory, switch to the submodule directory:
Once you’re in the submodule directory, fetch the latest changes from the original repository. You won’t see the changes in your files yet, as these commands only fetch changes and update internal references:
To actually update the submodule's files and reference to the latest commit in its master branch (or another branch if specified), you can check out the desired branch and pull in the changes:
Step 3: Add and Commit Changes
After pulling the most recent changes into the submodule, return to the main project directory. You'll notice the changes in the submodule's directory are reflected as modifications:
The submodule directory will appear modified. Add the submodule changes and commit them:
This action updates the main project to reference the new commit of the submodule.
Step 4: Push Changes
To share these updates with other developers or maintain a consistent main repository, push the changes to your remote:
Best Practices and Additional Tips
- Always Document Changes: Make sure you commit each change to the submodule with a clear message. This practice is essential for tracking the history of changes in submodules.
- Consider Using Tags for Stable Versions: If the submodule is part of a production environment, consider using tags or specific commits known for stability.
- Regularly Synchronize Submodules: To avoid complications, regularly synchronize your submodules with their respective upstream projects.
Summary Table
| Action | Command | Description |
| Clone Main Repo | git clone --recurse-submodules <repo_url> | Clones the repository and its submodules. |
| Enter Submodule | cd path/to/submodule | Navigates to the submodule directory. |
| Fetch Latest Changes | git fetch | Fetches the latest changes but does not merge. |
| Update Local Branch | git checkout master
git pull | Updates the current branch to latest commit. |
| Record Changes | git add .
git commit -m "Update submodule" | Stages and records the submodule update. |
| Push Changes | git push | Publishes changes to the remote repository. |
By understanding and utilizing these steps, developers can effectively update their Git submodules to the latest commits on origin, ensuring that their projects are up-to-date and maintain optimum integration with any external code dependencies or modular components.

