Remove a Submodule within git
For many git-based projects, submodules are useful in avoiding duplicate work and easing utility library updates. There are times, however, when a submodule needs to be removed from a project. Submodules aren't removed with git rm submoduledir, they must be removed in a more tedious, manual fashion. There are many unclear explanations of how to remove a submodule but I found one on Stack Overflow that's concise, so I thought I'd share it. The steps are as follows:
- Delete the relevant section from the
.gitmodulesfile. The section would look similar to:
[submodule "vendor"] path = vendor url = git://github.com/some-user/some-repo.git
- Stage the
.gitmoduleschanges via command line using:
git add .gitmodules
- Delete the relevant section from
.git/config, which will look like:
[submodule "vendor"] url = git://github.com/some-user/some-repo.git
git rm --cached path/to/submodule. Don't include a trailing slash -- that will lead to an error.
rm -rf .git/modules/submodule_name
- Commit the change:
- Delete the now untracked submodule files
rm -rf path/to/submodule
Those steps will get you rid of that unwanted submodule. A lot harder than adding one, eh?
Just an interesting side note, as of git 1.8.3, you can use
git submodule deinitto handle a lot of the heavy lifting of removing a submodule.