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


当前回答

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

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

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

其他回答

可以通过运行git rm<submodule path>和git commit来删除子模块。这可以使用git-restore撤消。删除将删除超级项目的跟踪数据,这些数据既是gitlink条目,也是.gitmodules文件中的部分。子模块的工作目录从文件系统中删除,但Git目录被保留下来,以便可以在不需要从另一个存储库获取的情况下签出过去的提交。要完全删除子模块,请另外手动删除$GIT_DIR/modules/<name>/。

来源:git help子模块

2012年12月我正在做的事情(结合了这些答案中的大部分):

oldPath="vendor/example"
git config -f .git/config --remove-section "submodule.${oldPath}"
git config -f .gitmodules --remove-section "submodule.${oldPath}"
git rm --cached "${oldPath}"
rm -rf "${oldPath}"              ## remove src (optional)
rm -rf ".git/modules/${oldPath}" ## cleanup gitdir (optional housekeeping)
git add .gitmodules
git commit -m "Removed ${oldPath}"

这对我有用。上面的答案在终端中显示了这一点,没有发生其他事情

'fatal: not removing 'demolibapp' recursively without -r'

demolibapp是我要删除的子模块名称git子模块deinit分解git rm—缓存的demolibapp-rrm-rf.git/modules/demolibappgit add--全部gitcommit-m“删除额外的子模块”数字推送rm-rf脱模剂

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

设置path_to_submodule变量(无尾随斜杠):path_to_submodule=路径/到/子模块从.gitmodules文件中删除相关行:git-config-f.gitmodules—删除节子模块$路径_子模块从.git/config中删除相关部分git-config-f.git/config—删除节子模块$路径_子模块仅从索引中取消存储并删除$path_to_submodule(以防止丢失信息)git rm--缓存的$path_to_submodule跟踪对.gitmodules所做的更改git-add.gitmodules提交超级项目gitcommit-m“删除子模块子模块名”删除现在未跟踪的子模块文件rm-rf$path_to_submodulerm-rf.git/modules/$path_to_submodule

只是一张纸条。从git1.8.5.2开始,有两个命令可以执行:

git rm -r the_submodule
rm -rf .git/modules/the_submodule

正如@Mark Cheverton的回答正确指出的那样,如果没有使用第二行,即使您现在删除了子模块,剩余的.git/modules/the_submodule文件夹也将阻止将来添加或替换相同的子模块。此外,正如@VonC所提到的,gitrm将在子模块上完成大部分工作。

--更新(07/05/2017)--

为了澄清,_submodule是项目中子模块的相对路径。例如,如果子模块位于子目录subdir内,则为subdir/my_submodule。

正如评论和其他答案中正确指出的,这两个命令(虽然功能上足以删除子模块)确实在.git/config的[submodule“the_submodule”]部分留下了痕迹(截至2017年7月),可以使用第三个命令删除:

git config -f .git/config --remove-section submodule.the_submodule 2> /dev/null