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


当前回答

简单的步骤

删除配置项:git-config-f.git/config--删除节子模块$submodulename git-config-f.gitmodules—删除部分子模块$子模块名称从索引中删除目录:git rm--cached$submodule路径犯罪删除未使用的文件:rm-rf$submodulepathrm-rf.git/modules/$submodulename

请注意:$submodulepath不包含前导或尾随斜杠。

出身背景

当您执行git子模块add时,它只将其添加到.gitmodules中,但是完成git子模块init后,它将添加到.git/config中。

因此,如果您希望删除模块,但能够快速恢复,那么就这样做:

git rm --cached $submodulepath
git config -f .git/config --remove-section submodule.$submodulepath

首先执行git rebase HEAD和git commit是一个好主意最后,如果你把这个写在脚本中。

还可以看看我可以取消填充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>'

但它一直在说:

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

所有的答案看起来都过时了。我使用的是git版本2.28.0。一行回答是,

git rm path-to-submodule

然而,即使从源代码管理中删除了子模块,.git/modules/path到子模块仍然包含子模块存储库,.git/config包含其URL,因此您仍然需要手动删除这些子模块:

git config --remove-section submodule.path-to-submodule
rm -rf .git/modules/path-to-submodule

有时,必须使用-f标志:

$ git rm -f img2vec

例如,因为您可能会收到如下错误:

$ git rm img2vec/
error: the following file has changes staged in the index:
    img2vec
(use --cached to keep the file, or -f to force removal)

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

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

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

使用Magit的简单解决方案

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

转到项目根目录,然后

M-x,magit列表子模块

then

M-x,删除magit子模块

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

就是这样!


(我的Magit版本是v3.3.0)