Useful git commands – part 3

Submodules, you either love them or hate them. The support is getting better for each new Git version, but for some reason there is still no command to remove a submodule from a project. Here is a script to fix that problem.

Most of the script consists of sanity checks. The interesting part is in lines 57-60. The submodule information needs to be removed from two different config files, then removed from the main Git repo, and finally the checked out files are removed. You can of course do this manually, but using this script is easier and less error prone.

Add this to the alias section of your .gitconfig file to make it even more convenient:

subrm = !~/bin/git-subrm.sh

Now you can simply use git subrm <submodule path> and be done with it. Awesome!