Git

Remove Git Submodule

September 23, 2019

Have you ever tried Git submodule for your project? And then you found that it’s annoying to build CI/CD with it?
Instead of using Git submodule, it’s better to create the submodule as NPM package.

Here is how to remove Git submodule from project:

# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule

Reference: https://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule/36593218#36593218


Manual and cleaner way:

  • Delete the section referring to the submodule from the .gitmodules file
  • Stage the changes via git add .gitmodules
  • Delete the relevant section of the submodule from .git/config.
  • Run git rm --cached path_to_submodule (no trailing slash)
  • Run rm -rf .git/modules/path_to_submodule
  • Commit the changes with “`git commit -m “Removed submodule “
  • Delete the now untracked submodule files rm -rf path_to_submodule

Reference: https://www.freecodecamp.org/forum/t/how-to-remove-a-submodule-in-git/13228