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


当前回答

总而言之,这是您应该做的:

设置path_to_submodule变量(无尾随斜杠):

path_to_submodule=path/to/submodule

从.gitmodules文件中删除相关行:

git config -f .gitmodules --remove-section submodule.$path_to_submodule

从.git/config中删除相关部分

git config -f .git/config --remove-section submodule.$path_to_submodule

仅从索引中取消存储并删除$path_to_submodule(以防止丢失信息)

git rm --cached $path_to_submodule

跟踪对.gitmodules所做的更改

git add .gitmodules

提交超级项目

git commit -m "Remove submodule submodule_name"

删除现在未跟踪的子模块文件

rm -rf $path_to_submodule

rm -rf .git/modules/$path_to_submodule

另请参见:备选指导线

其他回答

使用Magit的简单解决方案

如果您在Emacs下使用Magit,则可以执行以下操作:

转到项目根目录,然后

M-x,magit列表子模块

then

M-x,删除magit子模块

系统将询问您要删除哪个子模块。

就是这样!


(我的Magit版本是v3.3.0)

如果由于您添加、提交和推送了一个已经是Git存储库(包含.Git)的文件夹而意外添加了子模块,那么您将没有.gitmodules文件可供编辑,或者.Git/config中的任何内容。在这种情况下,您只需要:

git rm --cached subfolder
git add subfolder
git commit -m "Enter message here"
git push

FWIW,在执行git添加之前,我还删除了.git文件夹。

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

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

在最新的git中,删除git子模块只需要4个操作。

删除.gitmodules中的相应条目阶段更改gitadd.gitmodules删除子模块目录git rm--cached<path_to_submodule>Commit git Commit-m“已删除子模块xxx”

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

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