4

我已经阅读了如何自己删除 git 子模块

# 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其他的不多。所以拉的人还是得跑

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

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

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

  • .gitmodules在拉之前和之后做差异
  • 删除所有子模块,然后运行git submodule update --init每个部署。
  • 子模块在拉取后确实最终成为一个未跟踪的文件,因此一个可行的选项是在拉取后删除所有包含.git子目录的未跟踪目录,但您可能会删除想要保留的内容。

任何更好的选择表示赞赏。

4

1 回答 1

1

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

至于更新开发者的仓库,运行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
于 2013-02-19T13:50:31.293 回答