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


当前回答

如果您需要使用bash脚本在一行命令中执行此操作,如下所示:

$cd/path/to/your/rebo&&/bin/bash$HOME/remove_submodule.sh/path/to-the/submodule

在$HOME目录中创建bash脚本文件,名为remove_submodule.sh:

#!/bin/bash

git config -f .gitmodules --remove-section submodule.$1
git config -f .git/config --remove-section submodule.$1
git rm --cached $1
git add .gitmodules
git commit -m "Remove submodule in $1"
rm -rf $1
rm -rf .git/modules/$1
git push origin $(git rev-parse --abbrev-ref HEAD) --force --quiet

其他回答

您可以使用别名自动化其他人提供的解决方案:

[alias]
  rms = "!f(){ git rm --cached \"$1\";rm -r \"$1\";git config -f .gitmodules --remove-section \"submodule.$1\";git config -f .git/config --remove-section \"submodule.$1\";git add .gitmodules; }; f"

将其放入git配置中,然后可以执行:git rms path/to/submodule

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

所有的答案看起来都过时了。我使用的是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)

我刚刚找到了.submodule(忘记了确切名称)隐藏文件,它有一个列表。。。你可以用这种方式逐个删除它们。我刚刚有一个,所以我删除了它。很简单,但它可能会搞乱Git,因为我不知道子模块是否附加了什么。到目前为止,除了libetpan通常的升级问题外,似乎还可以,但这(希望)是无关的。

注意到没有人发布手动擦除,因此添加

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}"