This tutorial demonstrates how to remove a submodule in git.
When developing a software project, we often use external frameworks and libraries required by our project. These frameworks and libraries may be open-source and kept in a git repository.
We may want to keep the source code of such external libraries in our project directory in our git repository. The submodules feature of git allows us to keep such external sources, which are separate Git repositories, as sub-directories in our project Git repository.
We may then decide to remove such Git submodules as those may no longer be relevant to our project.
We will now explain it with an example.
Remove a Submodule in Git
When working on software projects, we often need to use external sources of the libraries. The external libraries may be developed by a third party or a different team.
Such external libraries may be tracked in a separate Git repository.
We can use the submodule feature of the Git, to incorporate such an external Git repository as a subdirectory of our project’s Git repository. Thus, we can then clone another repository into our project and keep the commits of each separate.
Typically, we may replace or remove such external libraries. We may feel that our project no longer needs such an external library.
Thus, we then decide to remove the external library from our project Git repository. For this, we need to remove the Git submodule of the external library.
Suppose we have a submodule
good-ext-lib present in our project Git repository. We now want the submodule
good-ext-lib to be removed from the Git repository and the file system.
We need to run the Git commands as follows to remove the submodule.
$ git submodule deinit -f path/to/good-ext-lib $ rm -rf .git/modules/path/to/good-ext-lib $ git rm -f path/to/good-ext-lib
git submodule deninit deregisters the submodule
good-ext-lib. It removes the whole
submodule.$name section from the
git/config file. Also, it removes the working tree of the submodule.
rm -rf with the
good-ext-lib submodule path in the
.git folder deletes the submodule directory from our project’s
git rm with the path of the
good-ext-lib submodule in the project directory removes the tracking data of the super project (our project). It removes the
gitlink entry. Also, it eliminates the submodule section present in the
.gitmodules file and stages the file.
Finally, we need to commit the removal of the
good-ext-lib submodule as follows.
$ git commit -m "removed submodule good-ext-lib"
Thus, now we have removed the
good-ext-lib submodule from the project directory in Git.
Thus, we have shown how to remove a submodule in Git.
For more information, please visit -