Git Submodule Remove

A Git submodule is a Git repository inside another Git repository. Sometimes you need to remove one, for example when it is not needed anymore, or when the update is not working and you want to clean it and add it again.

Removing a submodule needs a few steps, because its data is in three places: the working folder, the .gitmodules file and the hidden folder .git/modules. First we make a project with a submodule to practice:

Terminal output

$ git submodule status
c61d9dcb43b1c0515422e270481321b0a8028ca4 libs/lib (heads/main)
$ cat .gitmodules
[submodule "libs/lib"]
path = libs/lib
url = ../lib

Steps to remove the submodule

Terminal output

$ git submodule deinit -f libs/lib
Cleared directory 'libs/lib'
Submodule 'libs/lib' (../lib) unregistered for path 'libs/lib'
$ git rm -f libs/lib
rm 'libs/lib'
$ rm -rf .git/modules/libs/lib
$ git commit -m "Remove libs/lib submodule"
[main a2ea29e] Remove libs/lib submodule
2 files changed, 4 deletions(-)
delete mode 160000 libs/lib
$ git status -s

How it works

  • git submodule deinit -f removes the submodule from the Git config of the project and clears its files.
  • git rm -f removes the folder and the entry from .gitmodules, and stages both changes.
  • rm -rf .git/modules/... deletes the hidden copy of the submodule repository. Without it, adding the same submodule again can fail.
  • The commit saves the removal. If you need to push, run git push after this.
Note: Be careful with rm -rf. Check the path two times before you press Enter. Read more in Git Submodule.

Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.