如何删除Git子模块?为什么我不能git子模块rm模块名称?


当前回答

如果你想删除子模块而不从本地文件系统中删除文件夹,我可以这样做:

MOD=example
git rm --cached -f apps/$MOD
git config -f .gitmodules --remove-section submodule.$MOD

其他回答

您必须删除.gitmodules和.git/config中的条目,并从历史记录中删除模块的目录:

git rm --cached path/to/submodule

如果你在git的邮件列表上写东西,很可能有人会为你编写一个shell脚本。

在现代git中(我在2022年编写了这篇文章,并更新了git安装),这变得非常简单:

运行git-rm<path to submodule>并提交。

这将删除<path to submodule>处的文件树,以及.gitmodules文件中的子模块条目。即,删除存储库中子模块的所有痕迹。

然而,正如文档所指出的,子模块的.git目录(在主项目.git目录的模块/目录中)被保留了下来,“这样就可以在不需要从另一个存储库获取的情况下签出过去的提交”。如果您仍然想删除此信息,请手动删除.git/modules/中的子模块目录,并删除.git/config文件中的子模板条目

rm-rf.git/modules/<子模块路径>,以及git-config—删除节子模块<子模块路径>。



较旧的社区wiki说明:

通过Git子模块教程页面:

要删除子模块,您需要:

从.gitmodules文件中删除相关部分。暂存.gitmodules更改:git-add.gitmoduls从.git/config中删除相关部分。从工作树中删除子模块文件,并索引:git rm--cached path_to_submodule(无斜杠)。删除子模块的.git目录:rm-rf.git/modules/path_to_submodule提交更改:gitcommit-m“已删除子模块<name>”删除现在未跟踪的子模块文件:rm-rf path_to_submodule

另请参见:以下替代步骤。

要删除添加的子模块,请使用:

REPOSITORY=blah@blah.com:repos/blah.git
MOD_DIR=lib/blah
git submodule add $REPOSITORY $MOD_DIR

Run:

git rm $MOD_DIR

就是这样。

对于旧版本的git(约为1.8.5,实际上甚至在2.26.2中),请使用:

git submodule deinit $MOD_DIR
git rm $MOD_DIR
git config -f .gitmodules --remove-section submodule.$MOD_DIR

如果你想删除子模块而不从本地文件系统中删除文件夹,我可以这样做:

MOD=example
git rm --cached -f apps/$MOD
git config -f .gitmodules --remove-section submodule.$MOD

使用git v2.7.4,简单的3个步骤就可以了。

git submodule deinit -f -- a/submodule    
git rm -f a/submodule
git commit