Sub topic 8
Removing a submodule cleanly
This chapter assumes you can add and clone a submodule, and that you know a submodule is a gitlink (a pointer to one commit) plus a .gitmodules entry. Here you will remove one without leaving stale state behind.
Why removal takes several steps
A submodule leaves traces in four places:
| Location | What it holds |
|---|---|
| Working tree | The checked-out files in `vendor/libfoo` |
| Parent's index and tree | The gitlink entry that pins a commit |
| `.gitmodules` | The path and URL, which is committed and shared |
| `.git/config` and `.git/modules/` | Your local registration and the submodule's full clone |
There is no single command that clears all four. If you delete only the directory, Git still tracks the gitlink and reports the submodule as deleted. If you leave the local clone behind, adding a submodule at the same path later can fail.
The removal sequence
Run these commands from the root of the parent repository.
git submodule deinit -f vendor/libfoo
git rm -f vendor/libfoo
rm -rf .git/modules/vendor/libfoo
git commit -m "Remove libfoo submodule"git submodule deinit -f vendor/libfoounregisters the submodule. It empties the directory and deletes thesubmodule.vendor/libfoo.*settings from.git/config. The-fflag lets it proceed when the submodule has local modifications.git rm -f vendor/libfooremoves the gitlink from the index and the directory from disk. It also deletes the matching section from.gitmodulesand stages that change.rm -rf .git/modules/vendor/libfoodeletes the submodule's own repository. Git keeps it there so that a latergit submodule addor checkout of an old commit can reuse it without cloning again.git commitrecords the removal for everyone else.
Verify the result
After the commit, check each location.
git status
git submodule status
git config --get-regexp '^submodule\.'
ls .git/modulesgit statusshould show a clean tree.git submodule statusshould not listvendor/libfoo.- The
git configcommand should print nothing for that path. .git/modulesshould no longer containvendor, unless other submodules still use that path.
If the .gitmodules file ends up empty, git rm still leaves it staged as an empty file. You can remove it with git rm .gitmodules if no other submodules remain.
Common errors
- Directory deleted by hand. If you ran
rm -rf vendor/libfoofirst,git statusshows the path as deleted and the gitlink is still tracked. You can still rungit rm vendor/libfooand the cleanup steps afterward. git rmrefuses to run. It stops when the submodule has uncommitted changes. This is the reason the sequence uses-f. Check that you do not need those changes before forcing it.- Other clones keep an empty directory. After teammates pull your removal commit, their
vendor/libfoomay remain as an untracked directory, and their.git/configstill has the old registration. They can rungit submodule deinit -f vendor/libfoobefore pulling. They can also delete the leftover directory and.git/modules/vendor/libfoothemselves. - Re-adding fails. If you run
git submodule addat the same path later, you may seea git directory for 'vendor/libfoo' is found locally. That message means step 3 was skipped. Either delete the old directory under.git/modules, or rungit submodule add --forceto reuse it.
Removing history is a separate task
The commit above removes the submodule from the current and future commits. Older commits still contain the gitlink, so checking out an old revision brings the submodule reference back. Rewriting history to erase it is a different, disruptive operation and is not needed for a normal removal.
Exercise
You ran git rm -f vendor/libfoo and committed, but never ran git submodule deinit. Later git config --get-regexp '^submodule\.' still prints submodule.vendor/libfoo.url. Which location holds this entry, and how do you remove it?
Show answer
The entry is in .git/config, the local registration that deinit normally deletes. The commit does not touch it. Remove it with git config --remove-section submodule.vendor/libfoo. Also delete .git/modules/vendor/libfoo if it is still present, after confirming that it has no unpushed commits.
Exercise
Why does the removal sequence run git submodule deinit before git rm, and what does it cost you if you skip the rm -rf .git/modules/... step?
Show answer
deinit clears the local registration and working tree, so git rm then only has to remove the gitlink and the .gitmodules section. Skipping the rm -rf step leaves the old clone in .git/modules. It wastes disk space and can make a later git submodule add at the same path fail with "a git directory ... is found locally".