diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index d183672db..d7e7c3cbb 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -49,6 +49,7 @@ Callable, Dict, Iterator, + List, Mapping, Sequence, TYPE_CHECKING, @@ -1267,6 +1268,36 @@ def remove( return self + @unbare_repo + def deinit(self, force: bool = False) -> "Submodule": + """Run ``git submodule deinit`` on this submodule. + + This is a thin wrapper around ``git submodule deinit ``, paralleling + :meth:`add`, :meth:`update`, and :meth:`remove`. It unregisters the + submodule (removes its entry from ``.git/config`` and empties the + working-tree directory) without deleting the submodule from + ``.gitmodules`` or its checked-out repository under ``.git/modules/``. + A subsequent :meth:`update` will re-initialize the submodule from the + retained contents. + + :param force: + If ``True``, pass ``--force`` to ``git submodule deinit``. This + allows deinitialization even when the submodule's working tree has + local modifications that would otherwise block the command. + + :return: + self + + :note: + Doesn't work in bare repositories. + """ + args: List[str] = [] + if force: + args.append("--force") + args.extend(["--", self.path]) + self.repo.git.submodule("deinit", *args) + return self + def set_parent_commit(self, commit: Union[Commit_ish, str, None], check: bool = True) -> "Submodule": """Set this instance to use the given commit whose tree is supposed to contain the ``.gitmodules`` blob.