Sub topic 8
Removing a submodule cleanly
This chapter assumes you can add and clone a submodule, and that you know what the .gitmodules file and the gitlink entry record. A gitlink is the special tree entry in the parent repository that pins a submodule to one commit hash. If those ideas are unfamiliar, review the earlier chapters on the submodule model first.
By the end you will be able to:
- Remove a submodule so that no trace remains in the working tree, the index,
.gitmodules,.git/config, or.git/modules. - Verify that the removal is complete.
- Convert a submodule into ordinary tracked files while keeping its content.
- Recognize and repair the errors that incomplete removals cause.
Why removal is a multi-step job
Adding a submodule is one command, git submodule add. Removing one has no matching single command, because the submodule's state lives in four separate places:
| Location | What it holds | Shared with collaborators? |
|---|---|---|
| Parent repository tree | The gitlink entry (mode `160000`) that pins a commit | Yes, committed |
| `.gitmodules` | Path-to-URL mapping | Yes, committed |
| `.git/config` | Local copy of the URL, written by `git submodule init` | No, local only |
| `.git/modules/<path>` | The submodule's actual Git directory (its object database) | No, local only |
The submodule's working directory (for example vendor/libfoo) holds only a small .git file that points into .git/modules. Git keeps the real repository inside the parent's .git directory so that you can delete and restore the working tree without losing the clone.
That design explains the usual mistake. If you delete the working directory and commit, the committed state is incomplete or inconsistent. The local state, .git/config and .git/modules, is also left behind. Each of the four locations needs its own step.
The standard removal procedure
Run these commands from the top level of the parent repository. The path is the one shown by git submodule status, not the URL.
git submodule deinit -f vendor/libfoo
git rm -f vendor/libfoo
rm -rf .git/modules/vendor/libfoo
git commit -m "Remove libfoo submodule"Here is what each step does.
git submodule deinit -f vendor/libfoounregisters the submodule locally. It deletes thesubmodule.vendor/libfoo.*section from.git/configand empties the working directory. The path stays in the index and in.gitmodules, so the parent repository still considers it a submodule. The-fflag lets deinit proceed even if the submodule has uncommitted changes. Without-f, Git refuses in that case, which protects you from discarding work. Drop-fon the first attempt if you are not sure the submodule is clean.git rm -f vendor/libfooremoves the gitlink from the index and the (now empty) directory from disk. It also removes the matching section from.gitmodulesand stages that change. Use-fonly if the submodule still contains changes you are willing to lose.rm -rf .git/modules/vendor/libfoodeletes the submodule's Git directory. This is the only step that discards the local clone and any unpushed commits in it, so it is the point of no return. The path under.git/modulesnormally matches the submodule's name, which defaults to its path. Check withls .git/modulesif the submodule was renamed or moved at some point.git commitrecords the removal. The commit contains two changes: the deleted gitlink and the edited.gitmodules.
If .gitmodules listed other submodules, only the section for vendor/libfoo disappears. If it listed no others, Git leaves an empty .gitmodules or removes it, depending on your Git version. An empty file is harmless, but you can delete it with git rm -f .gitmodules if you prefer.
Verifying the result
After the commit, check each location from the table.
git submodule status
git config --get-regexp '^submodule\.'
cat .gitmodules
ls .git/modules
git statusA clean removal looks like this:
git submodule statusno longer listsvendor/libfoo.git config --get-regexp '^submodule\.'prints nothing for that path..gitmoduleshas no section for it, or the file is gone..git/moduleshas novendorentry for it.git statusreports a clean working tree, with no leftovervendor/libfoodirectory.
Run this check before you push. Fixing the local state later is easy, but a committed .gitmodules that still lists the path confuses every collaborator.
Variant: keep the files as ordinary content
Sometimes you do not want the code gone. You want to stop treating it as an external dependency and make it part of your repository, perhaps because the upstream project is abandoned. That keeps the files but drops the link to the upstream history. Do not run git rm on the submodule in this case, because it deletes the files.
git submodule deinit -f vendor/libfooDeinit empties the working tree, so first restore the content you want to keep. A simple route is to copy it out beforehand. A full sequence looks like this:
cp -r vendor/libfoo /tmp/libfoo-snapshot
rm -rf /tmp/libfoo-snapshot/.git
git submodule deinit -f vendor/libfoo
git rm -f vendor/libfoo
rm -rf .git/modules/vendor/libfoo
mkdir -p vendor
cp -r /tmp/libfoo-snapshot vendor/libfoo
git add vendor/libfoo
git commit -m "Vendor libfoo as plain files and drop the submodule"The snapshot's .git file is deleted before the copy. If it were left in place, Git would treat the directory as a nested repository and git add would not stage the individual files. The result is a commit that deletes the gitlink and adds the files as regular blobs. History from the upstream project is not carried over. If you want to keep that history, use a subtree instead, which a later chapter covers.
What collaborators see
Once you push the removal commit, other developers pull it. For them:
- Git removes the gitlink and
.gitmodulesentry from their tree. - Git deletes the old working directory if it is clean. If it holds untracked files or local modifications, Git leaves it in place and may report that it could not remove the directory. They can inspect it and delete it by hand.
- Their
.git/configentry and.git/modules/vendor/libfooare not touched, because those files are local only.
Collaborators can clear the leftovers with:
git config --remove-section submodule.vendor/libfoo
rm -rf .git/modules/vendor/libfooLeaving the leftovers is mostly harmless, but it wastes disk space and can cause the re-adding problem described below. If your team removes a submodule, mention the cleanup in the pull request or release notes.
Common errors and their fixes
The path was deleted by hand
If someone ran rm -rf vendor/libfoo and committed, the gitlink may still be in the index or .gitmodules may still list the path. Symptoms include fatal: no submodule mapping found in .gitmodules for path 'vendor/libfoo' or a submodule that reappears as empty after a fresh clone. Repair it with the standard steps. If the directory is already gone, git rm -f vendor/libfoo still works. If Git says it is not tracked, edit .gitmodules by hand, delete the section, and run git add .gitmodules.
Re-adding fails after removal
Running git submodule add for the same path after an incomplete removal fails with a message like:
A git directory for 'vendor/libfoo' is found locally with remote(s):
origin https://github.com/example/libfoo.git
If you want to reuse this local git directory instead of cloning again from
https://github.com/example/libfoo.git
use the '--force' option. If the local git directory is not the correct repo
or you are unsure what this means choose another name with the '--name' option.The cause is the leftover .git/modules/vendor/libfoo. Delete it if you do not need it, or pass --force to reuse that clone.
`git rm` complains about local modifications
git rm refuses to remove a submodule whose working tree has modifications and reports that the submodule has local changes. Either commit or discard the changes, or confirm that you want to lose them and add -f.
Old commits still mention the submodule
Removal does not rewrite history. If you check out an older commit, Git expects a submodule at the old path again. The directory may be missing or leftover from a previous checkout, and the submodule will be uninitialized until you run git submodule update --init. Setting git config submodule.recurse true makes checkout and pull update submodules along with the parent, which smooths over moving between commits that do and do not have the submodule.
Choosing a removal approach
| Goal | Approach |
|---|---|
| Drop the dependency completely | `deinit -f`, `git rm -f`, delete `.git/modules/<path>`, commit |
| Keep the code, drop the link | Same, but copy the files out first (minus `.git`) and re-add them as plain files |
| Keep the code and upstream history | Replace the submodule with a subtree |
| Temporarily skip the submodule on one machine | `git submodule deinit <path>` only. This is fully reversible with `git submodule update --init` |
The last row is useful. deinit without the follow-up steps changes only your local clone. The commit history and the parent repository are unaffected, and the submodule can be restored at any time.
Exercise
You ran git submodule deinit -f vendor/libfoo and git rm -f vendor/libfoo, then committed. A week later, git submodule add https://github.com/example/libfoo.git vendor/libfoo fails with "A git directory for 'vendor/libfoo' is found locally". Which step was skipped, what does it contain, and how do you fix the problem if you want a fresh clone?
Show answer
The skipped step was rm -rf .git/modules/vendor/libfoo. That directory holds the submodule's local Git repository (its objects and refs), which neither deinit nor git rm deletes. To get a fresh clone, remove the directory with rm -rf .git/modules/vendor/libfoo and run git submodule add again. Check first that it holds no unpushed commits you need. Alternatively, run git submodule add --force to reuse the existing local clone.
Exercise
A teammate pulls your commit that removes a submodule, but the vendor/libfoo directory is still on their disk, and git config --get-regexp '^submodule\.' still shows an entry for it. Why, and which two things should they clean up?
Show answer
Git does not delete a working directory that has untracked files or local modifications, and .git/config and .git/modules are local files that a pull never touches. They should inspect and delete the leftover directory if it holds nothing they need, then run git config --remove-section submodule.vendor/libfoo and rm -rf .git/modules/vendor/libfoo.