How to change the remote repository for a git submodule?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with Git submodules, you might find yourself in a situation where you need to change the remote repository from which a submodule is being fetched. This could be due to a number of reasons such as the original repository being moved, changes in project requirements, or accessing the submodule from a different source for improved reliability or speed. Below, you will find detailed steps on how to accomplish this task, along with a technical explanation and an example to guide you through the process.
Understanding Git Submodules
Before diving into how to change a submodule's remote repository, it's essential to have a basic understanding of what Git submodules are. A Git submodule allows you to keep a Git repository as a subdirectory of another Git repository. This is useful for incorporating and tracking external projects and libraries within your main repository.
Steps to Change the Remote Repository for a Git Submodule
- Navigate to the Submodule Directory: Start by going into the directory of the submodule within your main project.
- Check the Current Remote: Before making changes, it's helpful to check the current remote repository for the submodule.
This command will show you the URL of the currently configured remote repository.
- Update the Remote Repository URL: To change the remote URL, use the
git remote set-urlcommand.
Replace new-remote-repository-url with the URL of the new remote repository.
- Fetch the Latest Changes from the New Repository: Once the remote URL is updated, fetch all the branches and changes from the new repository.
- Check Out the Appropriate Branch or Commit: If needed, you can switch to the appropriate branch or reset to a specific commit from the new repository.
- Commit the Changes in the Main Project: After updating the submodule, return to your main project directory and commit the changes.
- Push Changes to the Main Repository: Finally, push the changes to your main repository to ensure that others working on the project will fetch the submodule from its new location.
Example
Suppose you have a submodule at lib/cool-feature which was originally cloned from https://github.com/oldrepo/cool-feature.git, and you need to change it to https://github.com/newrepo/cool-feature.git:
Summary Table
| Task | Command |
| Navigate to Submodule Directory | cd path/to/submodule |
| View Current Remote | git remote -v |
| Change Remote URL | git remote set-url origin new-url |
| Fetch Changes from New Remote | git fetch |
| Switch to a Desired Branch | git checkout desired-branch |
| Add & Commit Changes in Main Project | cd .., git add path/to/submodule, git commit -m "commit-message" |
| Push Changes | git push |
Additional Tips
- Check submodule status: Use
git submodule statusto check the SHA of the currently checked-out commit. - Recursive Submodule Update: If your submodule contains other submodules (
nested submodules), usegit submodule update --init --recursiveto ensure all submodules are correctly initialized and updated.
By following these steps and using the provided examples, you can effectively change the remote repository for a Git submodule within your projects.

