Programming

How to set up a git project to use an external repo submodule

27 September 2026 · 11 min read

How to set up a git project to use an external repo submodule

Managing dependencies in software projects can be a complex undertaking. When your project relies on external libraries or components maintained in separate repositories, Git submodules offer a powerful solution. Learning how to set up a git project to use an external repo submodule allows you to integrate these dependencies directly into your project’s repository, ensuring version control and simplifying collaboration. This blog post will guide you through the process step-by-step, covering everything from adding a submodule to updating and managing it effectively. Proper use of Git submodules ensures your team stays synchronized with external dependencies, enhancing stability and reproducibility across development environments. By mastering this technique, you can streamline your workflow and keep your project organized, minimizing integration headaches and improving overall project health.

Understanding Git Submodules

Git submodules are essentially pointers or references to specific commits within another Git repository. Instead of copying the external code directly into your project, you create a link to a specific version of that code. This has several advantages, including reduced repository size (since you’re not duplicating the entire external repo) and the ability to easily update to newer versions of the submodule as they become available. Submodules are essential for managing dependencies, plugins, or shared libraries that are developed and maintained separately from your main project. They allow you to incorporate external codebases without losing track of their origin or sacrificing version control best practices. Think of them as lightweight bookmarks pointing to critical external resources.

One of the key benefits of using submodules is ensuring that everyone on your team is using the same version of the external dependency. When you clone a repository containing submodules, Git doesn’t automatically download the submodule’s contents. Instead, it downloads the pointer to the submodule. You need to explicitly initialize and update the submodules to fetch the actual code. This explicit step guarantees that everyone working on the project has the same version of the external dependency, preventing compatibility issues and integration problems. This also enables reproducible builds, crucial for continuous integration and deployment pipelines. According to a Stack Overflow Developer Survey, approximately 45% of developers use Git for version control, highlighting the importance of mastering its features, including submodules. Stack Overflow Developer Survey

For example, imagine you’re developing a web application that relies on a specific JavaScript library maintained in its own Git repository. Instead of copying the library’s source code directly into your project, you can add it as a submodule. This way, when the library releases a new version, you can easily update the submodule to incorporate those changes without having to manually copy and paste code. This simplifies the update process and ensures that you’re always using the latest version of the library.

Adding a Submodule to Your Git Project

The first step in using Git submodules is adding them to your project. This process involves specifying the URL of the external repository and the location within your project where you want the submodule to reside. The git submodule add command handles this process seamlessly. Make sure you’re in the root directory of your Git project before running the command. After adding the submodule, Git creates a .gitmodules file in your project, which stores information about the submodules, including their URLs and paths. This file is crucial for others to be able to correctly initialize and use the submodules when they clone your project.

To add a submodule, use the following command:

git submodule add <repository_url> <path>

Replace <repository_url> with the URL of the external Git repository you want to include as a submodule, and <path> with the directory within your project where you want to place the submodule. For instance, if you want to add a library called “my-library” from https://github.com/example/my-library.git into a directory named “lib” in your project, the command would be: git submodule add https://github.com/example/my-library.git lib/my-library. After running this command, you’ll need to commit the changes to your main repository to register the submodule and its configuration. The .gitmodules file and the new entry in your project’s index are essential for tracking the submodule.

Here’s a real-world example: let’s say you’re building a website that uses a custom CSS framework hosted on GitHub. You can add this framework as a submodule to your project’s assets/css/framework directory. When the framework updates, you can easily pull in the changes without manually merging them into your project. This approach ensures that your website stays up-to-date with the latest framework features and bug fixes. Using submodules in this way significantly simplifies dependency management and promotes code reuse.

Initializing and Updating Submodules

After adding a submodule, it’s essential to initialize and update it to fetch the actual code from the external repository. The git submodule init command initializes the submodule, setting up the necessary configurations. This command reads the .gitmodules file and prepares the submodule for use. However, it doesn’t actually download the submodule’s code. For that, you need to use the git submodule update command. This command fetches the code from the external repository and checks it out into the specified directory within your project. Failing to initialize and update submodules is a common mistake that can lead to missing dependencies and build errors. To correctly set up a git project to use an external repo submodule, initialize and update the submodule after adding it.

Here’s how to initialize and update submodules:

  1. Run git submodule init to initialize the submodule.
  2. Run git submodule update –init –recursive to fetch the submodule’s code. The –init flag ensures that the submodule is initialized if it hasn’t been already, and the –recursive flag ensures that any nested submodules within the submodule are also initialized and updated.

When someone else clones your project, they will also need to initialize and update the submodules to get the external code. They can do this by running the same commands: git submodule init followed by git submodule update –init –recursive. It’s a good practice to include these commands in your project’s documentation or README file to ensure that everyone knows how to properly set up the project. This helps prevent confusion and ensures that everyone is working with the correct versions of the external dependencies. According to Atlassian, a well-defined workflow for managing Git submodules is crucial for maintaining code integrity and collaboration. Atlassian Git Submodule Tutorial

Consider a scenario where you’re working on a collaborative project with multiple developers. If a new developer clones the repository and forgets to initialize and update the submodules, they will be missing the code from the external dependencies. This can lead to build errors, broken features, and a lot of frustration. By including clear instructions in the project’s README file, you can prevent this situation and ensure that everyone can easily set up the project correctly.

Managing Changes in Submodules

Managing changes within submodules requires a slightly different approach than managing changes in the main repository. Because a submodule is essentially a separate Git repository embedded within your project, you need to navigate into the submodule’s directory to make changes, commit them, and push them to the submodule’s remote repository. Once the changes are pushed to the submodule’s repository, you need to update the submodule’s pointer in your main project to point to the new commit. This process ensures that your main project references the correct version of the submodule.

Here’s a breakdown of the process:

  • Navigate into the submodule’s directory: cd <path_to_submodule>
  • Make your changes to the submodule’s code.
  • Commit the changes within the submodule: git commit -am “Your commit message”
  • Push the changes to the submodule’s remote repository: git push origin <branch_name>
  • Return to the main project’s directory: cd ..
  • Update the submodule’s pointer in the main project: git add <path_to_submodule>
  • Commit the changes in the main project: git commit -am “Updated submodule <path_to_submodule>”
  • Push the changes to the main project’s remote repository: git push origin <branch_name>

It’s important to understand that the main project only stores a reference to a specific commit in the submodule. When you update the submodule, you’re essentially updating that reference to point to the latest commit in the submodule’s repository. This means that if someone else clones your project, they will get the exact version of the submodule that you’ve specified. This ensures consistency and prevents compatibility issues. Failing to properly manage changes in submodules can lead to inconsistencies and integration problems. If you don’t update the submodule pointer in the main project, others will be using an outdated version of the submodule.

Best Practices for Using Git Submodules

Using Git submodules effectively requires following certain best practices to avoid common pitfalls and ensure a smooth workflow. Here are some key recommendations:

  • Always initialize and update submodules after cloning a repository.
  • Regularly update submodules to incorporate the latest changes from the external repositories.
  • Avoid making direct changes to submodule code within the main project. Instead, make changes in the submodule’s repository and then update the submodule pointer in the main project.
  • Use descriptive commit messages when updating submodules to clearly indicate the changes that have been incorporated.
  • Include clear instructions in your project’s README file on how to initialize, update, and manage submodules.

One common mistake is modifying submodule code directly within the main project’s working directory. This can lead to conflicts and make it difficult to track changes. Instead, always navigate into the submodule’s directory, make your changes there, commit them, and push them to the submodule’s repository. Then, update the submodule pointer in the main project. This approach ensures that your changes are properly tracked and that everyone is using the correct versions of the external dependencies. According to GitHub’s documentation, proper submodule management is crucial for maintaining project stability and collaboration. GitHub Git Submodule Documentation

Another best practice is to regularly update your submodules. This ensures that you’re incorporating the latest bug fixes, features, and security updates from the external repositories. You can use the git submodule foreach git pull command to update all submodules in your project simultaneously. This command iterates through each submodule and runs the git pull command within that submodule, fetching the latest changes from the remote repository. This helps keep your project up-to-date and prevents compatibility issues.

Infographic illustrating the Git submodule workflow will be placed here.
FAQ About Git Submodules ------------------------
What is the difference between a Git submodule and a Git subtree?
Git submodules are pointers to specific commits in another repository, while Git subtrees merge the entire history of another repository into your project. Submodules are generally preferred for managing external dependencies that are actively maintained, while subtrees are better suited for incorporating code that is unlikely to change.
How do I remove a Git submodule?
Removing a Git submodule involves several steps, including de-registering the submodule, deleting the submodule's directory from your project, and removing the submodule's entry from the .gitmodules file and the .git/config file. It's important to follow these steps carefully to avoid leaving behind orphaned files or configurations.
How do I update a Git submodule to the latest version?
To update a Git submodule to the latest version, navigate into the submodule's directory and run git pull. Then, return to the main project's directory and add and commit the changes to update the submodule pointer.
[Click here to learn more about Git concepts.](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c)By understanding the nuances of **how to set up a git project to use an external repo submodule**, you can significantly improve your project's dependency management. Using submodules allows for cleaner code organization, better version control of external dependencies, and streamlined collaboration within your team. From adding the initial submodule to managing updates and resolving conflicts, each step contributes to a more robust and maintainable project. Don't hesitate to experiment with submodules in your own projects to fully grasp their power and flexibility.

Ready to take your Git skills to the next level? Start implementing submodules in your projects today and experience the benefits firsthand. Explore related topics like Git subtrees, package managers, and dependency injection Question & Answer :

I’d like to create a repo which pulls in a remote repo.

For example, let’s say jQuery as a submodule:

git://github.com/jquery/jquery.git 

What would be the process of creating a repo with jQuery as a submodule and adding my own external as a remote repo.

Also once this is setup, if I push / pull to my own remote, will the external remain intact?

  1. You have a project – call it MyWebApp that already has a github repo
  2. You want to use the jquery repository in your project
  3. You want to pull the jquery repo into your project as a submodule.

Submodules are really, really easy to reference and use. Assuming you already have MyWebApp set up as a repo, from terminal issue these commands:

cd MyWebApp git submodule add git://github.com/jquery/jquery.git externals/jquery 

This will create a directory named externals/jquery* and link it to the github jquery repository. Now we just need to init the submodule and clone the code to it:

git submodule update --init --recursive 

You should now have all the latest code cloned into the submodule. If the jquery repo changes and you want to pull the latest code down, just issue the submodule update command again. Please note: I typically have a number of external repositories in my projects, so I always group the repos under an “externals” directory.

The online Pro Git Book has some good information on submodules (and git in general) presented in an easy-to-read fashion. Alternately, git help submodule will also give good information. Or take a look at the Git Submodule Tutorial on the git wiki.

I noticed this blog entry which talks about submodules and compares them to Subversion’s svn:externals mechanism: http://speirs.org/blog/2009/5/11/understanding-git-submodules.html

* As a best practice, you should always place your submodules in their own directory, such as Externals. If you don’t, your root project directory can become very cluttered very fast.