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


当前回答

以下是我所做的:

1.)从.gitmodules文件中删除相关部分。您可以使用以下命令:

git config -f .gitmodules --remove-section "submodule.submodule_name"

2.)准备.gitmodules更改

git add .gitmodules

3.)从.git/config中删除相关部分。您可以使用以下命令:

git submodule deinit -f "submodule_name"

4.)删除gitlink(无尾斜杠):

git rm --cached path_to_submodule

5.)清理.git/模块:

rm -rf .git/modules/path_to_submodule

6.)承诺:

git commit -m "Removed submodule <name>"

7.)删除现在未跟踪的子模块文件

rm -rf path_to_submodule

其他回答

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中(我在2022年编写了这篇文章,并更新了git安装),这变得非常简单:

运行git-rm<path to submodule>并提交。

这将删除<path to submodule>处的文件树,以及.gitmodules文件中的子模块条目。即,删除存储库中子模块的所有痕迹。

然而,正如文档所指出的,子模块的.git目录(在主项目.git目录的模块/目录中)被保留了下来,“这样就可以在不需要从另一个存储库获取的情况下签出过去的提交”。如果您仍然想删除此信息,请手动删除.git/modules/中的子模块目录,并删除.git/config文件中的子模板条目

rm-rf.git/modules/<子模块路径>,以及git-config—删除节子模块<子模块路径>。



较旧的社区wiki说明:

通过Git子模块教程页面:

要删除子模块,您需要:

从.gitmodules文件中删除相关部分。暂存.gitmodules更改:git-add.gitmoduls从.git/config中删除相关部分。从工作树中删除子模块文件,并索引:git rm--cached path_to_submodule(无斜杠)。删除子模块的.git目录:rm-rf.git/modules/path_to_submodule提交更改:gitcommit-m“已删除子模块<name>”删除现在未跟踪的子模块文件:rm-rf path_to_submodule

另请参见:以下替代步骤。

如果您刚刚添加了子模块,例如,您只是添加了错误的子模块或将其添加到了错误的位置,只需执行git stash,然后删除文件夹。这是假设添加子模块是您在最近的回购中唯一做的事情。

使用git v2.7.4,简单的3个步骤就可以了。

git submodule deinit -f -- a/submodule    
git rm -f a/submodule
git commit

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