Git submodule remove

Git Submodule is a Git repository as a subdirectory inside another Git repository.

Sometimes we need to remove a submodule because of following reasons:

  1. When Git submodule is no longer required.
  2. When submodule update is not working because of some issue. Then, you would like to remove (clean) and add submodule again.

Steps to Remove Git Submodules:

These steps will clean all files of submodule from git repository.

  • Delete the submodule section (similar to following) from the .gitmodules file
[submodule "inc/sample-submodule-path"]
	path = inc/sample-submodule-path
	url = git@gitlab.one.com:project/inc/sample-submodule-path.git
  • Stage the changes via command: git add .gitmodules
  • Delete the submodule section from file: .git/config
[submodule "inc/sample-submodule-path"]
	path = inc/sample-submodule-path
	url = git@gitlab.one.com:project/inc/sample-submodule-path.git

.git is hidden git folder. If folder is not visible check your folder settings to show it.

  • Run command: git rm --cached inc/sample-submodule-path

Sometimes we got issue while adding submodule: “submodule already exists in the index”. Above command fix it by removing a submodule from cache index. Try rm -r to remove recursively.

  • Run command: rm -rf .git/modules/inc/sample-submodule-path

This command will remove submodules folders from .git

  • Commit the changes with command: git commit -m "Removed submodule"
  • Delete untracked submodule files: rm -rf inc/sample-submodule-path

If you are able to run above commands, you submodule has been removed successfully.

If you were trying to fix or clean existing submodule, you can add a new submodule now.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.