A Submodule is a Git Repository that resides as a subdirectory in another Git Repository. Submodules allow a Git Repository to track the history of another repository. Submodules are helpful when we want to use the code from a frequently changing external repo.
When we add a submodule to our repository, a new entry for the submodule is added to the .git/config file. Also, a new .gitmodules file is created(or updated) with an entry for the submodule.
Removing a submodule is a little tedious and requires a few steps. Let's learn how to delete a submodule in Git.
As mentioned above, an entry for the submodule is created in the .gitmodules file. We need to remove that entry from the .gitmodules file and stage the changes. Use the Git Add command to stage the changes in the .gitmodules file.
git add .gitmodules
The contents of a .gitmodules file are shown below. It contains a single entry for a submodule DemoSubmodule located in the libraries folder.
.submodules file:
​
[submodule "libraries/DemoRepo"]
   path = libraries/DemoSubmodule
   url = https://github.com/alice/DemoSubmodule
Next, we need to alter the .git/config file. Remove the entry of the submodule from the .git/config file. A typical .git/config file is shown below. We can see the entry for the submodule under [submodule "libraries/DemoSubmodule"] heading.
.git/config file
​
[core]
   repositoryformatversion = 0
   filemode = false
   bare = false
   logallrefupdates = true
   symlinks = false
   ignorecase = true
[remote "origin"]
   url = https://github.com/alice/Demo
   fetch = +refs/heads/*:refs/remotes/origin/*
[submodule "libraries/DemoSubmodule"]
   url = https://github.com/alice/DemoSubmodule
   active = true
The next step is to remove the submodule from the working directory and the staging area(or index). The Git Rm command with the --cached option is perfect for this. We need to pass the path to the submodule.
git rm --cached <path-to-submodule>
Next, we need to remove the submodule's .git folder. We will use the Rm command but with the -r and -f options. The -r option allows us to recursively delete all the files. The -f option(short for --force) allows us to forcefully delete any conflicting changes.
rm -rf .git/modules/<path-to-submodule>
We now have multiple changes staged for a commit. These changes include the modification of the .gitmodules file and the removal of the submodule. Commit these staged changes by using the Git Commit command.
git commit -m "Submodule Removed"
The submodule is now untracked. The final step is to delete this untracked submodule. Use the Rm command with the -r and -f options.
rm -rf <path-to-submodule>
There is an alternate and shorter way of removing a submodule. We will use the Git Rm and the Rm command along with a new Git Submodule Deinit command.
Use the Git Submodule Deinit command to remove the submodule entry from the .git/config file.
git submodule deinit -f <path-to-submodule>
Next, use the Rm command to remove the submodule folder from the .git/modules directory of the parent repo. It removes the .git folder of the submodule. We will use the -r and -f options.
rm -rf .git/modules/<path-to-submodule>
Finally, remove the submodule from the working directory using the Git Rm command. It will also remove the entry from the .gitmodules file.
git rm -f <path-to-submodule>
Submodules allow us to incorporate frequently changing external projects in our repository. We can use a stable version of the external project with the help of submodules. When we add a new submodule(using git submodule add [remote-repo-url] [subdirectory-name]), an entry for that submodule in created in the .gitmodules file and the .git/config file. To remove a submodule, we need to remove these entries. We also need to delete other files related to the submodule. The Git Rm command and Unix's Rm command help us remove the submodule. In this tutorial, we discussed two methods to remove a submodule from our repository. These methods are summarized below.
Method 1:
Delete submodule entry from the .gitmodules file.
Stage the changes of the .gitmodules file using Git Add.
git add .gitmodules
Delete the submodule entry from the .git/config file.
Remove submodule files from the working directory and the staging area(index).
git rm --cached <path-to-submodule>
Remove submodule from the .git/modules folder of the parent repo.
rm -rf .git/modules/<path-to-submodule>
Commit the changes
git commit -m "Commit Message"
Delete the untracked submodule.
rm -rf <path-to-submodule>
Method 2:
The second method is pretty straightforward. Run the following three commands to remove a submodule.
git submodule deinit -f <path-to-submodule>
rm -rf .git/modules/<path-to-submodule>
git rm -f <path-to-submodule>