How to Remove a Git Submodule

Git submodules are a feature in Git that allows you to include other Git repositories within your main repository as a reference to a specific commit. This is useful when you want to manage external dependencies, libraries, or components within your main repository without merging their entire history.

The file of the .gitmodule includes meta data on how to map amid the URL of the submodule project and local directory. It is necessary to remove the submodule in case it is not required anymore.

Watch a course Git & GitHub - The Practical Guide

Steps to removing submodule

Here is the step-by-step guide to delete the unnecessary submodule:

  1. Open your terminal and navigate to the root directory of your Git repository.

  2. List the submodules of your repository using the following command:

git submodule

This will list all the submodules in your repository.

  1. Remove the submodule using the deinit command:
git submodule deinit <path_to_submodule>

Replace <path_to_submodule> with the relative path to the submodule you want to remove. For example, if your submodule is located in the submodules/mysubmodule directory, you would use:

git submodule deinit submodules/mysubmodule

This will remove the submodule from Git's tracking information.

  1. Remove the submodule from Git's directory using the following command:
git rm <path_to_submodule>

This will remove the submodule from the Git repository and delete its files from the disk. You can also use the --cached option to keep the submodule's files on the disk:

In the framework of the next step, you should delete the relevant section from:

git rm --cached <path_to_submodule>
  1. Commit the changes using the following command:
git commit -m "Removed submodule <path_to_submodule>"

That's it! Your submodule is now removed from your Git repository.

'git submodule deinit' command

To remove a submodule from your Git repository, you can use the git submodule deinit command.

Here's how you can do it:

  1. Open your terminal and navigate to the root directory of your Git repository.

  2. Use the following command to deinit the submodule:

git submodule deinit <path_to_submodule>

Replace <path_to_submodule> with the relative path to the submodule you want to remove. For example, if your submodule is located in the submodules/mysubmodule directory, you would use:

git submodule deinit submodules/mysubmodule

'git submodule uninit' command

Alternatively, you can use the git submodule uninit command which removes the submodule from the repository's .git/config file without deleting it from tgit submodule uninit he filesystem:

git submodule uninit <path_to_submodule>

This avoids needing to edit the .git/config file manually, which could break something if the user doesn't know what they're doing.

The git rm Command

The git rm command helps to remove particular files or a group of files from a repository. It is mainly aimed at removing tracked files from the index. It can also remove files from the working directory and staging index. But, note, that you can’t remove it merely from the working directory. Besides, git rm can’t remove branches.

The git commit Command

The git commit command is mainly used for saving the overall current changes of your project. You can create commits for capturing the ongoing state of your project. The committed captures are safe, as Git asks before starting to change them. The git add command is invoked for promoting changes to the project, before running git commit.