如何删除Git子模块?为什么我不能git子模块rm模块名称?
当前回答
要删除添加的子模块,请使用:
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
其他回答
要删除添加的子模块,请使用:
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
以下是我认为必要或有用的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无关。
您可以使用别名自动化其他人提供的解决方案:
[alias]
rms = "!f(){ git rm --cached \"$1\";rm -r \"$1\";git config -f .gitmodules --remove-section \"submodule.$1\";git config -f .git/config --remove-section \"submodule.$1\";git add .gitmodules; }; f"
将其放入git配置中,然后可以执行:git rms path/to/submodule
我创建了一个bash脚本来简化删除过程。它还检查回购中是否有未保存的更改,并请求确认。它已经在os x上进行了测试,如果知道它在普通linux发行版上是否也能正常工作,将会很有趣:
https://gist.github.com/fabifrank/cdc7e67fd194333760b060835ac0172f
如果您需要使用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中,我如何知道我的当前版本是什么?