How to update submodules in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Git submodules are a feature that allows you to include and manage a repository within another repository. This is particularly useful for including libraries or other common components that are developed in separate projects. However, managing submodules - especially updating them - can be complex. This guide explains how to update submodules in Git, covering various commands and scenarios to ensure your submodules stay up-to-date.

A submodule in Git is essentially a link to a specific commit in another repository. It allows you to keep a separate project within your repository, while maintaining its own lifecycle and version control. This is ideal for dependencies or shared codebases.

If you're cloning a repository that uses submodules, you'll need to initialize and update the submodules as well:

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

If you've already cloned the repository and you just need to update the submodules, you can initialize them with:

Terminal
git submodule update --init

This command copies the necessary .gitmodules file into the .git/config file of your local repository. This file contains the URLs and paths for each submodule, specifying where the submodule's repository data can be fetched from.

To update the submodules to the latest commits available in their respective repositories, use:

Terminal
git submodule update --remote

This command fetches the latest changes from the remote submodules but does not update the main project to track the latest commits. It updates the submodules based on the commit specified in the .gitmodules file.

For complex projects with nested submodules (submodules within submodules), use:

Terminal
git submodule update --init --recursive

This command ensures that all nested submodules are initialized and updated.

If you need to update a submodule to a specific commit, navigate to the submodule's directory and check out the desired commit:

Terminal
cd path/to/submodule
git checkout <commit-hash>
cd ../..
git add path/to/submodule
git commit -m "Update submodule to specific commit"

If you want to update your submodule and merge any upstream changes, you can combine submodule update with a merge:

Terminal
git submodule update --remote --merge

This will fetch the latest changes from the submodule's remote repository and try to merge them into the current branch of the submodule.

Sometimes you may want to update all submodules to the latest commit on their respective tracked branches:

Terminal
git submodule update --remote

This command is particularly useful when submodules are set to track branches, as it ensures that the submodules are synchronized with the latest development.

  • Regularly synchronize: Regular updates ensure that your project remains compatible with external dependencies and reduces integration issues.
  • Document submodule usage: Clearly document how to clone and initialize submodules for new developers on your project.
  • Commit submodule changes: When you update a submodule to a new commit, remember to commit that change in the main repository. This ensures that other developers working on the project are aware of the submodule update.

For further reading on Git submodules, see the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2