What is the current way to remove a git submodule?
Solution 1:
You have the git submodule deinit
git submodule deinit <asubmodule>
git rm <asubmodule>
# Note: asubmodule (no trailing slash)
# or, if you want to leave it in your working tree
git rm --cached <asubmodule>
rm -rf .git/modules/<asubmodule>
deinit
Un-register the given submodules, i.e. remove the whole
submodule.$name
section from.git/config
together with their work tree.Further calls to
git submodule update
,git submodule foreach
andgit submodule sync
will skip any unregistered submodules until they are initialized again, so use this command if you don’t want to have a local checkout of the submodule in your work tree anymore.If you really want to remove a submodule from the repository and commit that use
git rm
instead.If
--force
is specified, the submodule’s work tree will be removed even if it contains local modifications.
Solution 2:
I'm using Git version 2.16.2 and git rm
does the job mostly well:
git rm path-to-submodule
You can verify with git status
and git diff --cached
that this deinitializes the submodule and modifies .gitmodules
automatically. As always, you need to commit the change.
However, even though the submodule is removed from source control, .git/modules/path-to-submodule
still contains the submodule repository and .git/config
contains its URL, so you still have to remove those manually:
git config --remove-section submodule.path-to-submodule
rm -rf .git/modules/path-to-submodule
Keeping the submodule repository and configuration is intentional so that you can undo the removal with e.g. git reset --hard
.