How to Remove a Git Submodule

Git submodule is used for keeping one git repository as a subdirectory of another git repository. Usually, Git submodule depends on an external code, which is incorporated in various ways. You can easily copy and paste it into the primary repository.

The disadvantage of this method is that upstream changes can be lost to the external repository.

Git submodule represents a record in a host git repository pointing to a particular commit in another external repository.

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.

In this tutorial we will show you how to do it.

To remove the submodule follow the steps below:

  1. In the .gitmodules file delete the segment that refers to the submodule. It will look like this:
    [submodule "vendor"]
    	path = vendor
    	url = git://github.com/some-user/some-repo.git
  2. Next step is staging the changes via
    git add .gitmodules

    It will look like this:

         [submodule "vendor"]
    	url = git://github.com/some-user/some-repo.git
    
  3. In the framework of the next step, you should use git rm:
    .git/config
  4. Now you need to delete the appropriate section from
    git rm --cached path/to/submodule
  5. Then, invoke:
    rm -rf .git/modules/submodule_name
  6. Go on with committing your changes using git commit.
    git commit -m "Removed submodule <name>"
  7. Delete the untracked submodule files like this:
    rm -rf path/to/submodule

Following the above-given steps, you will manage to remove an unwanted submodule easily.

Description of 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.>/p>

Description of Git Commit

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.


Do you find this helpful?

Related articles