| title | Remove a Submodule |
|---|---|
| description | Step-by-step guide to fully removing a Git submodule — deinit, git rm, and cleaning up cached module data. |
| section | playbook/remove-submodule |
| order | 92 |
Removing a submodule is not a single command — it requires three cleanup steps to fully clear the submodule from your repository.
$ git submodule deinit <path> # 1. unregister from .git/config
$ git rm <path> # 2. remove from working tree and index
$ rm -rf .git/modules/<path> # 3. delete cached clone
$ git commit -m "Remove submodule"
deinit— removes the submodule entry from.git/configand clears the working directory at<path>. The submodule's URL remains in.gitmodulesuntil the next step removes it.git rm— removes the submodule entry from.gitmodulesand from the index (staging area). Also deletes the working directory ifdeinitdid not already.rm -rf .git/modules/<path>— deletes the cached bare clone that Git keeps under.git/modules/. Without this step, re-adding a submodule at the same path can use stale data.
- Forgetting step 3 — the cached clone stays behind. If you later add a submodule at the same path, Git may reuse the old checkout and you get confusing state.
- Uncommitted changes in the submodule —
deinitwill refuse to run. Use--forceif you are certain the changes are not needed. - Nested submodules — if the submodule itself contains submodules,
you need to deinit recursively or clean up
.git/modules/manually.
For other submodule operations, see the Submodules recipe.