git remove submodule

How to Remove Submodule in Git

Git submodule is a record in a git repository that points to a commit in another repository. They allow you to keep a snapshot of a repository in another repository. Since they are static and don’t get updated when the host repo is updated, some developers might want to remove submodules from their repositories. In this article, we will learn how to remove submodule in Git.


How to Remove Submodule in Git

If you have downloaded the latest git version (any version released in 2022), you can easily remove a submodule using the following command. Replace <path-to-submodule> with the path to submodule.

$ git rm <path-to-submodule>

You need to commit changes after you run the above command. This will remove the filetree at the specified path as well as its entry in .gitmodules file. But the .git directory of submodule is still retained after the above command.

If you want to remove that also, then delete the submodule’s directory at .git/modules/ as well as in .git/config. You can do so using the following commands.

$ rm -rf .git/modules/<path-to-submodule>
$ git config --remove-section submodule.<path-to-submodule>

Alternative Method to Remove Submodules

If the above commands are not available in your git version, you can use the older method to do the same.

Let us say you have a submodule subA in repository repoA. First, we backup the submodule files so that they are not referenced. You can also delete them if you don’t want them in future.

$ cp repoA/subA /home/ubuntu/subA_tmp

Next, we denitialize the submodule from repository.

$ git submodule deinit -f -- repoA/subA

Next, remove the submodule-related directory from .git/modules.

$ rm -rf .git/modules/repoA/subA

Lastly, remove the submodule related files.

$ git rm -f repoA/subA

In this article, we have learnt how to remove submodule in git. Generally, it is advisable not to use git submodules since they are not dynamic and can cause code conflicts. They are useful in case of commonly used libraries that don’t change frequently.

Also read:

How to Merge Two Git Repositories
How to Setup Username & Password for Multiple Repos
How to Reset Git Credentials
How to Add Images in README.md in GitHub
How to Cache HTTPS Credentials for Pushing Commits

Leave a Reply

Your email address will not be published.