git submodules: how to remove a submodule

2021-07-25

 | 

~1 min read

 | 

155 words

Deleting a git submodule is a potentially confusing aspect of working with them. This is because while they can be removed from some branches, it’s possible that other branches will still expect them to be there — which results in “git yelling at you.”

One approach to this problem I found was:

  1. Deinitialize the submodule,
  2. Remove the submodule’s directory from from .git/modules in the main project
  3. Finally, delete the directory related to the (now former) submodule.

As an example, if we have a .gitmodules that looks like this:

[submodule "my-submodule"]
    path = my-submodule
    url = git@github.com:stephencweiss/my-submodule.git

How might we remove it? A condensed view would look like:

Removing\
$ git submodule deinit -f my-submodule
$ rm -rf .git/modules/my-submodule
$ git rm -f my-submodule

This would need to be updated if the name of the submodule or its path were not the defaults. Git Submodules: Exploring the Add API


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!