How do I remove a submodule?

In modern git (I’m writing this in 2022, with an updated git installation), this has become quite a bit simpler:

  • Run git rm <path-to-submodule>, and commit.

This removes the filetree at <path-to-submodule>, and the submodule’s entry in the .gitmodules file. I.e. all traces of the submodule in your repository proper are removed.

As the docs note however, the .git dir of the submodule is kept around (in the modules/ directory of the main project’s .git dir), “to make it possible to checkout past commits without requiring fetching from another repository“.
If you nonetheless want to remove this info, manually delete the submodule’s directory in .git/modules/, and remove the submodule’s entry in the file .git/config. These steps can be automated using the commands

  • rm -rf .git/modules/<path-to-submodule>, and
  • git config --remove-section submodule.<path-to-submodule>.

Older community wiki instructions:

Via the page Git Submodule Tutorial:

To remove a submodule you need to:

  1. Delete the relevant section from the .gitmodules file.
  2. Stage the .gitmodules changes:
    git add .gitmodules
  3. Delete the relevant section from .git/config.
  4. Remove the submodule files from the working tree and index:
    git rm --cached path_to_submodule (no trailing slash).
  5. Remove the submodule’s .git directory:
    rm -rf .git/modules/path_to_submodule
  6. Commit the changes:
    git commit -m "Removed submodule <name>"
  7. Delete the now untracked submodule files:
    rm -rf path_to_submodule

See also: alternative steps below.

Leave a Comment