git - 从存储库中删除所有 Git 缓存的子模块

标签 git git-submodules

我们的团队已经很长时间没有使用子模块了,我们希望通过删除所有缓存的子模块来释放一些存储库空间。

我们是否只需要删除以下内容?

rm -rf .git/modules

或者有更推荐的方法吗?此外,如果可能,我们希望将删除推送到服务器。

注意:使用 git 2.5.4 和当前状态已将所有内容 merge 到一个分支中。

注意:我们不记得旧提交中使用的子模块的各个名称。

最佳答案

我在关于“不使用子模块”的问题中发现了一个可能的小混淆:是否还有未使用的子模块文件夹,或者这些文件夹是否已经删除并且只剩下缓存?

好吧,我认为我们可以通过首先遵循干净的删除过程来解决这两种情况,然后假设某些操作不正确并执行手动清理。

删除一个子模块的必要步骤在How do I remove a submodule?中有解释。 .但是为了回答这个问题,我们将立即删除所有子模块,而不假设我们知道子模块的名称。

# deinit all submodules from .gitmodules
git submodule deinit .

# remove all submodules (`git rm`) from .gitmodules
git submodule | cut -c43- | while read -r line; do (git rm "$line"); done

# delete all submodule sections from .git/config (`git config --local --remove-section`) by fetching those from .git/config
git config --local -l | grep submodule | sed -e 's/^\(submodule\.[^.]*\)\(.*\)/\1/g' | while read -r line; do (git config --local --remove-section "$line"); done

# manually remove leftovers
rm .gitmodules
rm -rf .git/modules

我不知道服务器同步。它可以在下次提交时自动完成,或者我们可能需要这些命令:

git submodule sync
git submodule update --init --recursive --remote

关于git - 从存储库中删除所有 Git 缓存的子模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890313/

相关文章:

git - 找出两个提交字符串中哪个是最新的

git - 我怎样才能让 git log 在一行上打印每个提交的完整哈希和简短统计信息?

git 失败时尽量打补丁

linux - 如何管理git用户?

git - 使用 Git move 子模块

c# - 如何使用 LibGit2Sharp 获取分支?

git - 将 git 管理的子目录切换到子模块

git - Docker-compose:总是收到无法找到指定的 Dockerfile

git - 什么是 'dirty' 子模块?

git - 如何更改 git 子模块跟踪远程分支?