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


当前回答

从git中删除子模块的最佳方法:

$ git submodule deinit -f <submodule-name>
$ rm -rf .git/modules/<submodule-name>
$ git config -f .gitmodules --remove-section submodule.<submodule-name>
$ git config -f .git/config --remove-section submodule.<submodule-name>
$ git rm --cached <submodule-name>
$ git commit -m 'rm submodule: <submodule-name>'

其他回答

这个问题的大多数答案都过时、不完整或不必要的复杂。

使用git 1.7.8或更高版本克隆的子模块将在您的本地存储库中最多留下四条自己的痕迹。删除这四个轨迹的过程由以下三个命令给出:

# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f 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

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

git rm --cached path/to/submodule

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

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

我遵循了同一指南的指示如何删除子模块?

$ git submodule deinit -f <submodule-name>
$ rm -rf .git/modules/<submodule-name>
$ git config -f .gitmodules --remove-section submodule.<submodule-name>
$ git config -f .git/config --remove-section submodule.<submodule-name>
$ git rm --cached <submodule-name>
$ git commit -m 'rm submodule: <submodule-name>'

但它一直在说:

fatal: no submodule mapping found in .gitmodules for path

因此,我所做的是像这样在.gitignore中包含路径(路径末尾没有星号):

<path>

然后我修改了任何文件并进行了简单的推送:

$ git add .
$ git commit -m "Ignoring sharedlibs folder <path> on .gitignore"
$ git push -u origin master