如何删除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 stash,然后删除文件夹。这是假设添加子模块是您在最近的回购中唯一做的事情。

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

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

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

从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>'

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

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

[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