如何删除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

其他回答

如果您需要使用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

您可以使用别名自动化其他人提供的解决方案:

[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

要删除添加的子模块,请使用:

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

总而言之,这是您应该做的:

设置path_to_submodule变量(无尾随斜杠):path_to_submodule=路径/到/子模块从.gitmodules文件中删除相关行:git-config-f.gitmodules—删除节子模块$路径_子模块从.git/config中删除相关部分git-config-f.git/config—删除节子模块$路径_子模块仅从索引中取消存储并删除$path_to_submodule(以防止丢失信息)git rm--缓存的$path_to_submodule跟踪对.gitmodules所做的更改git-add.gitmodules提交超级项目gitcommit-m“删除子模块子模块名”删除现在未跟踪的子模块文件rm-rf$path_to_submodulerm-rf.git/modules/$path_to_submodule

我最近发现了一个git项目,其中包含许多有用的git相关命令:https://github.com/visionmedia/git-extras

安装并键入:

git-delete-submodule submodule

然后事情就完成了。子模块目录将从repo中删除,并且仍然存在于文件系统中。然后可以提交更改,如:gitcommit-am“Remove the submodule”。