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


当前回答

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

[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

其他回答

除了这些建议之外,我还必须rm-Rf.git/modules/path/to/submodule,才能添加一个同名的新子模块(在我的例子中,我用原来的fork替换了一个fork)

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

[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

所有的答案看起来都过时了。我使用的是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)

自git1.8.3(2013年4月22日)起:

一旦你用“gitsubmoduleInit”表达了对某个子模块的兴趣,就没有瓷的方式来表示“我不再对这个子模块感兴趣了”。“git submodule deinit”是这样做的方法。

删除过程还使用gitrm(自2013年10月git1.8.5起)。

总结

然后,三步移除过程将是:

0. mv a/submodule a/submodule_tmp

1. git submodule deinit -f -- a/submodule    
2. rm -rf .git/modules/a/submodule
3. git rm -f a/submodule
# Note: a/submodule (no trailing slash)

# or, if you want to leave it in your working tree and have done step 0
3.   git rm --cached a/submodule
3bis mv a/submodule_tmp a/submodule

解释

rm-rf:丹尼尔·施罗德的回答中提到了这一点,Eonil在评论中总结道:

这将使.git/modules/<子模块路径>/保持不变。所以,若您使用此方法删除了一个子模块并再次添加它们,那个么就不可能了,因为存储库已经损坏。


git rm:请参阅提交95c16418:

当前在子模块上使用“gitrm”会将子模块的工作树从超级项目的工作树中删除,并将gitlink从索引中删除。但是.gitmodules中的子模块部分保持不变,这是现在删除的子模块的剩余部分,可能会激怒用户(与.git/config中的设置相反,这必须保持提醒用户对该子模块感兴趣,以便稍后在签出旧的提交时重新填充)。

让“gitrm”不仅从工作树中删除子模块,还可以从.gitmodules文件中删除“submodule.<submodulename>”部分,并将两者都暂存,从而帮助用户。


git子模块deinit:它源于这个补丁:

使用“gitsubmodule init”,用户可以告诉git他们关心一个或多个子模块,并希望在下次调用“gitsubmodule update”时填充它。但目前,他们无法简单地告诉git他们不再关心子模块,并希望摆脱本地工作树(除非用户非常了解子模块的内部结构,并将“submodule.$name.url”设置与工作树一起从.git/config中删除)。

通过提供“deinit”命令帮助这些用户。这将删除整个子模块<对于给定的子模块(或所有已初始化的子模块,如果给定“.”)。如果当前工作树包含修改,则失败,除非强制。如果在命令行上给定的子模块的url设置在.git/config中找不到,但仍然不会失败,请进行投诉。

如果(de)初始化步骤(.git/config和.git/modules/xxx)

从git1.8.5开始,gitrm还负责:

在.gitmodules文件中记录子模块url的“add”步骤:需要为您删除它。子模块特殊条目(如这个问题所示):gitrm将其从索引中删除:git rm—缓存的path_to_submodule(无尾斜杠)这将以特殊模式“160000”删除索引中存储的目录,并将其标记为子模块根目录。

如果您忘记了最后一步,并尝试将子模块添加为常规目录,则会收到如下错误消息:

git add mysubmodule/file.txt 
Path 'mysubmodule/file.txt' is in submodule 'mysubmodule'

注意:自从Git 2.17(2018年第二季度)以来,Git子模块deinit不再是shell脚本。这是对C函数的调用。

见Prathamesh Chavan(pratham pc)的承诺2e61273、承诺1342476(2018年1月14日)。(于2018年2月13日由Junio C Hamano--gitster--在commit ead8dbe中合并)

git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
  ${GIT_QUIET:+--quiet} \
  ${prefix:+--prefix "$prefix"} \
  ${force:+--force} \
  ${deinit_all:+--all} "$@"

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

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