To remove a Git submodule, follow these steps:
- Deinitialize the submodule:
This removes the submodule’s configuration from.git/config
and cleans up its working directory.
git submodule deinit -f -- path/to/submodule
- Remove the submodule from the index and working tree:
This deletes the gitlink (reference) and the submodule’s directory.
git rm -f -- path/to/submodule
- Remove the submodule entry from
.gitmodules
:
Edit the.gitmodules
file to delete the section for the submodule, then stage the change:
git add .gitmodules
- Remove the submodule’s repository from
.git/modules
:
Delete the internal storage for the submodule (optional but recommended):
rm -rf .git/modules/path/to/submodule
- Commit the changes:
git commit -m "Remove submodule path/to/submodule"
- Manually delete any remaining submodule files (if needed):
rm -rf path/to/submodule
Notes:
- Replace
path/to/submodule
with the actual path to your submodule. - Use
--
to avoid ambiguity in paths (e.g., if the path starts with a hyphen). - The
-f
flag forces removal even if the submodule has local modifications.