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


当前回答

git子模块deinit<子模块路径>从.gitmodules中删除节git rm<子模块路径>删除需要从项目中删除的模块文件。将删除的文件添加到git并调用gitadd.gitmodules提交并推送

其他回答

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

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

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

使用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

在最新的git中,删除git子模块只需要4个操作。

删除.gitmodules中的相应条目阶段更改gitadd.gitmodules删除子模块目录git rm--cached<path_to_submodule>Commit git Commit-m“已删除子模块xxx”

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

我发现deinit对我很好:

git submodule deinit <submodule-name>    
git rm <submodule-name>

从git文档:

解交织注销给定子模块,即删除整个子模块$名称git/config中的部分及其工作树。