如何删除Git子模块?为什么我不能git子模块rm模块名称?
当前回答
所有的答案看起来都过时了。我使用的是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)
其他回答
以下是我认为必要或有用的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无关。
要删除添加的子模块,请使用:
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
我不得不进一步执行John Douth的步骤,并将其cd到子模块的目录中,然后删除Git存储库:
cd submodule
rm -fr .git
然后,我可以将文件作为父Git存储库的一部分提交,而无需对子模块的旧引用。
删除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步不会产生任何需要提交的更改。
所有的答案看起来都过时了。我使用的是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)
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别