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


当前回答

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

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

其他回答

我不得不进一步执行John Douth的步骤,并将其cd到子模块的目录中,然后删除Git存储库:

cd submodule
rm -fr .git

然后,我可以将文件作为父Git存储库的一部分提交,而无需对子模块的旧引用。

以下是我认为必要或有用的4个步骤(首先是重要步骤):

git rm -f the_submodule
rm -rf .git/modules/the_submodule
git config -f .git/config --remove-section submodule.the_submodule
git commit -m "..."

理论上,第1步中的git rm应该处理它。希望OP问题的第二部分有一天能够得到肯定的回答(这可以在一个命令中完成)。

但从2017年7月起,第2步必须删除.git/modules/中的数据。否则,您不能在将来重新添加子模块。

正如tinlyx的回答所指出的那样,您可能可以省去上面的两个git1.8.5+步骤,因为所有的git子模块命令似乎都有效。

步骤3删除了.git/config文件中_submodule的部分。为了完整起见,应该这样做。(该条目可能会对较旧的git版本造成问题,但我没有要测试的条目)。

为此,大多数答案建议使用git子模块deinit。我发现使用git-config-f.git/config--remove部分更加明确,也不那么令人困惑。根据git子模块文档,git deinit:

注销给定的子模块。。。如果确实要删除存储库中的子模块,并使用gitrm[1]提交相反

最后但并非最不重要的是,如果您没有gitcommit,那么在执行git子模块摘要时(从git2.7开始),您可能会遇到错误:

fatal: Not a git repository: 'the_submodule/.git'
* the_submodule 73f0d1d...0000000:

这与执行步骤2或3无关。

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

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

简单的步骤

删除配置项:git-config-f.git/config--删除节子模块$submodulename git-config-f.gitmodules—删除部分子模块$子模块名称从索引中删除目录:git rm--cached$submodule路径犯罪删除未使用的文件:rm-rf$submodulepathrm-rf.git/modules/$submodulename

请注意:$submodulepath不包含前导或尾随斜杠。

出身背景

当您执行git子模块add时,它只将其添加到.gitmodules中,但是完成git子模块init后,它将添加到.git/config中。

因此,如果您希望删除模块,但能够快速恢复,那么就这样做:

git rm --cached $submodulepath
git config -f .git/config --remove-section submodule.$submodulepath

首先执行git rebase HEAD和git commit是一个好主意最后,如果你把这个写在脚本中。

还可以看看我可以取消填充Git子模块吗?的答案?。

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