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


当前回答

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

其他回答

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

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

删除git子模块

要删除git子模块,需要以下4个步骤。

删除.gitmodules文件中的相应条目。条目可能如下所述

[submodule "path_to_submodule"]
    path = path_to_submodule
    url = url_path_to_submodule

阶段更改gitadd.gitmodules删除子模块目录git-rm-cached<path_to_submodule>。提交gitcommit-m“Removed submodule xxx”并推送。

下面提到的另外两个步骤需要完全清除本地克隆副本中的子模块。

删除.git/config文件中的相应条目。条目可能如下所述

[submodule "path_to_submodule"]
    url = url_path_to_submodule

执行rm-rf.git/modules/path_to_submodule

第5步和第6步不会产生任何需要提交的更改。

使用Magit的简单解决方案

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

转到项目根目录,然后

M-x,magit列表子模块

then

M-x,删除magit子模块

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

就是这样!


(我的Magit版本是v3.3.0)

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