如何删除Git子模块?为什么我不能git子模块rm模块名称?
当前回答
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
其他回答
如果您刚刚添加了子模块,例如,您只是添加了错误的子模块或将其添加到了错误的位置,只需执行git stash,然后删除文件夹。这是假设添加子模块是您在最近的回购中唯一做的事情。
删除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中删除子模块的最佳方法:
$ git submodule deinit -f <submodule-name>
$ rm -rf .git/modules/<submodule-name>
$ git config -f .gitmodules --remove-section submodule.<submodule-name>
$ git config -f .git/config --remove-section submodule.<submodule-name>
$ git rm --cached <submodule-name>
$ git commit -m 'rm submodule: <submodule-name>'
使用Magit的简单解决方案
如果您在Emacs下使用Magit,则可以执行以下操作:
转到项目根目录,然后
M-x,magit列表子模块
then
M-x,删除magit子模块
系统将询问您要删除哪个子模块。
就是这样!
(我的Magit版本是v3.3.0)
如果您需要使用bash脚本在一行命令中执行此操作,如下所示:
$cd/path/to/your/rebo&&/bin/bash$HOME/remove_submodule.sh/path/to-the/submodule
在$HOME目录中创建bash脚本文件,名为remove_submodule.sh:
#!/bin/bash
git config -f .gitmodules --remove-section submodule.$1
git config -f .git/config --remove-section submodule.$1
git rm --cached $1
git add .gitmodules
git commit -m "Remove submodule in $1"
rm -rf $1
rm -rf .git/modules/$1
git push origin $(git rev-parse --abbrev-ref HEAD) --force --quiet
推荐文章
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- “node_modules”文件夹应该包含在git存储库中吗
- 为什么git-rebase给了我合并冲突,而我所做的只是压缩提交?
- 当我试图推到原点时,为什么Git告诉我“没有这样的远程‘原点’”?
- 如何从远程分支中挑选?
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?