How to remove a Git submodule

Overview

It often happens that while working on one project, you need to use another project from within it. Perhaps it’s a library that a third party developed or that you’re developing separately and using in multiple parent projects. A common issue arises in these scenarios: you want to be able to treat the two projects as separate yet still be able to use one from within the other.

Background

I frequently need to look up the exact series of commands remove a git submodule. This post is primarily for my convenience, but may be useful to others as well.

Modify

Remove the relevant submodule section from each of the following files:

.gitmodules
1
2
3
4
5
...
[submodule "vendor"]
path = vendor
url = git://github.com/some-user/some-repo.git
...
.git/config
1
2
3
4
...
[submodule "vendor"]
url = git://github.com/some-user/some-repo.git
...

Commit

  1. Stage changed .gitmodules file
  2. Unstage and remove submodule path from the index (don’t include a trailing slash – that will lead to an error)
  3. Remove submodule files from index
  4. Commit the changes
  5. Remove submodule files
1
2
3
4
5
$ git add .gitmodules
$ git rm --cached path/to/submodule
$ rm -rf .git/modules/submodule_name
$ git commit
$ rm -rf path/to/submodule

Summary

Removing a submodule can be a tedious task, but can be accomplished by following the tasks in this post.

((( - Enjoy your lighter repository. - )))

References

| Git Reference / git-submodule
| Pro Git > Git Tools / Submodules
| David Walsh / Remove a Submodule