How do I remove a submodule?
Categories:
How to Properly Remove a Git Submodule

Learn the comprehensive steps to completely remove a Git submodule from your repository, ensuring a clean and error-free state.
Git submodules are powerful tools for managing external dependencies within a parent repository. However, removing a submodule isn't as straightforward as deleting a regular file. A simple rm -rf
can leave behind references and configuration entries that can cause issues later. This guide will walk you through the correct, multi-step process to fully detach and remove a submodule, ensuring your repository remains clean and functional.
Understanding the Challenge of Submodule Removal
When you add a submodule, Git records its presence in several places: the .gitmodules
file, the .git/config
file, and as a special entry in the Git index. Simply deleting the submodule directory will not remove these references, leading to 'dangling' entries that can confuse Git and other developers. The process involves carefully removing these references in a specific order.
flowchart TD A[Start Removal Process] --> B{Is submodule directory present?} B -- Yes --> C[Remove submodule entry from .git/config] C --> D[Remove submodule directory from .git/modules/] D --> E[Remove submodule entry from .gitmodules] E --> F[Remove submodule directory from working tree] F --> G[Remove submodule from Git index] G --> H[Commit changes] H --> I[End Removal Process] B -- No --> E
Flowchart illustrating the comprehensive steps for removing a Git submodule.
Step-by-Step Removal Process
The following steps outline the complete procedure for removing a Git submodule. It's crucial to follow these steps in order to avoid leaving behind orphaned references or corrupting your repository's state.
1. 1. Deinitialize the Submodule
First, you need to deinitialize the submodule. This command removes the submodule's entry from the .git/config
file and deletes its working tree. Replace path/to/submodule
with the actual path to your submodule.
2. 2. Remove the Submodule Entry from .gitmodules
Edit the .gitmodules
file and manually remove the section related to your submodule. This file tracks all submodules in your repository. After editing, save the file.
3. 3. Remove the Submodule Directory from .git/modules/
Git stores the submodule's repository data under .git/modules/
. You need to manually remove this directory. Replace path/to/submodule
with the actual path to your submodule.
4. 4. Remove the Submodule Directory from the Working Tree
Delete the actual submodule directory from your project's working tree. This is the directory that contained the submodule's files.
5. 5. Remove the Submodule from the Git Index
Even after deleting the directory, Git might still have a reference to it in its index. You need to explicitly remove it from the index. Replace path/to/submodule
with the actual path.
6. 6. Commit the Changes
Finally, commit all the changes you've made. This includes the modification to .gitmodules
and the removal of the submodule directory from the index.
git submodule deinit path/to/submodule
git rm --cached path/to/submodule
# Manually edit .gitmodules to remove the submodule's section
# Example: nano .gitmodules or your preferred editor
rm -rf .git/modules/path/to/submodule
rm -rf path/to/submodule
git add .gitmodules
git commit -m "Removed submodule: path/to/submodule"
Consolidated commands for removing a Git submodule.
Troubleshooting Common Issues
Sometimes, even after following the steps, you might encounter issues. Here are a few common problems and their solutions:
git submodule deinit
fails or you encounter issues, manually check and clean up the .git/config
file for any lingering submodule entries. Look for sections like [submodule "path/to/submodule"]
and remove them.If you find that git status
still shows the submodule directory as untracked or modified, double-check that you've removed it from the Git index using git rm --cached path/to/submodule
and that the .gitmodules
file is correctly updated and staged for commit.