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


当前回答

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

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

其他回答

除了这些建议之外,我还必须rm-Rf.git/modules/path/to/submodule,才能添加一个同名的新子模块(在我的例子中,我用原来的fork替换了一个fork)

我最近发现了一个git项目,其中包含许多有用的git相关命令:https://github.com/visionmedia/git-extras

安装并键入:

git-delete-submodule submodule

然后事情就完成了。子模块目录将从repo中删除,并且仍然存在于文件系统中。然后可以提交更改,如:gitcommit-am“Remove the submodule”。

project dir:     ~/foo_project/
submodule:       ~/foo_project/lib/asubmodule
- - - - - - - - - - - - - - - - - - - - - - - - -
run:
  1.   cd ~/foo_project
  2.   git rm lib/asubmodule && 
          rm .git/modules/lib/asubmodule && 
            git submodule lib/asubmodule deinit --recursive --force

只是一张纸条。从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

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

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