How do I remove a submodule with Git?

To remove a Git submodule, follow these steps:

  1. 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
  1. 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
  1. Remove the submodule entry from .gitmodules:
    Edit the .gitmodules file to delete the section for the submodule, then stage the change:
   git add .gitmodules
  1. 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
  1. Commit the changes:
   git commit -m "Remove submodule path/to/submodule"
  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *