gpt4 book ai didi

git - 删除 git 子模块 - 如何在 pull 时自动删除?

转载 作者:太空狗 更新时间:2023-10-29 14:20:27 29 4
gpt4 key购买 nike

我读过 how to remove a git submodule myself :

# Delete the relevant section from the .gitmodules file.
git config -f .gitmodules --remove-section submodule.$submodulepath
# Delete the relevant section from .git/config
git config -f .git/config --remove-section submodule.$submodulepath
git rm --cached path_to_submodule # no trailing slash
git commit
rm -rf path_to_submodule

我可以。但是当其他人执行 git pull 时,我们如何确保子模块随后从他们的系统中删除? .gitmodules 的变化伴随着 pull ,但据我所知,其他的不多。所以 pull 的人还得跑

git config -f .git/config --remove-section submodule.$submodulepath
rm -rf path_to_submodule

是吗?在小型开发团队中,我想您可以告诉每个人都运行这些命令,但这并不理想。

是否有一些神奇的命令可以自动执行此操作?特别是我想要一些在部署脚本中自动执行此操作的标准方法。在我的脑海中,我不确定脚本如何知道比以前少了一个子模块。 (不是特别有吸引力)我想到的选择是:

  • 在 pull 前后对 .gitmodules 进行比较
  • 删除所有子模块,然后在每次部署时运行 git submodule update --init
  • 子模块在 pull 后最终成为一个未跟踪的文件,因此一个可行的选项是在 pull 后删除所有包含 .git 子目录的未跟踪目录,但你可能会删除您想保留的内容。

感谢任何更好的选择。

最佳答案

对于将代码部署到具有 git clean 在 pull 后运行的服务器(例如通过 post-checkout Hook )是一个安全的选择。

至于更新开发人员的存储库,运行 git clean 是危险的,除了手动修剪,我别无他法。

然而,以下(未经测试的)脚本会自动执行此操作,将其命名为 git prune-submodules。涉及到在git pull之后,被移除的子模块仍然在.git/config中列出,所以你可以找出要归档的是哪个。

我是 shell 脚本的新手,所以请仔细检查。

#!/bin/sh
#get the list of submodules specified in .git/config
#http://stackoverflow.com/questions/1260748/how-do-i-remove-a-git-submodule#comment9411108_7646931
submodules_cfg=`git config -f .git/config -l | cut -d'=' -f1 | grep "submodule.$MODPATH" | sed 's/^submodule\.//' | sed 's/\.url$//'`

#get the list of current submodules
submodules=`git submodule | cut -b 2- | cut -d' ' -f 2`

#archive submodule if listed in .git/config but doesn't exist anymore
for submodule_cfg in $submodules_cfg; do
submodule_cfg_exists=0
for submodule in $submodules; do
if [ "$submodule" == "$submodule_cfg" ]; then
submodule_cfg_exists=1
fi
done

if ["$submodule_cfg_exists" == 0]; then
mkdir -p archived-submodules
mv $submodule_cfg archived-submodules/
git config -f .git/config --remove-section submodule.$submodule_cfg
fi
done

关于git - 删除 git 子模块 - 如何在 pull 时自动删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14877420/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com