W3docs

How to Remove a Git Submodule

With the help of this tutorial, you will learn how to remove an unwanted Git submodule easily.

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 .gitmodules file contains metadata that maps the submodule's URL to its local directory. You may need to remove a submodule if it is no longer required.

Steps to removing submodule

Here is the step-by-step guide to delete an 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:

How to remove a Git submodule

git submodule

This will list all the submodules in your repository.

  1. Remove the submodule from Git's tracking information using the deinit command:

How to remove a Git 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:

How to remove a Git submodule

git submodule deinit submodules/mysubmodule
  1. Remove the submodule from Git's index using the following command:

How to remove a Git submodule

git rm --cached <path_to_submodule>

This removes the submodule from the Git index without deleting the files from your working directory.

  1. Manually remove the submodule entry from the .gitmodules file and the corresponding section from the .git/config file. You can edit these files directly in a text editor.

  2. Remove the submodule's directory from your working tree:

How to remove a Git submodule

rm -rf <path_to_submodule>
  1. Commit the changes using the following command:

How to remove a Git submodule

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 stop tracking a submodule in your working directory, you can use the git submodule deinit command. It is typically the first step in the removal process.

git submodule deinit <path_to_submodule>

'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 the filesystem:

git submodule uninit <path_to_submodule>

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. When removing a submodule, use the --cached flag to remove it from the index while keeping the local files intact.

The git commit Command

The git commit command is mainly used for saving the current changes of your project. After removing the submodule files and updating the configuration, use it to finalize the removal:

git commit -m "Removed submodule <path_to_submodule>"