You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Git仓库的全部历史记录中移除子模块?

从Git仓库所有历史记录中移除子模块关联

要彻底清除仓库所有历史中的子模块关联,git-filter-repo是最靠谱的工具,你可以按以下步骤操作:

  1. 先备份仓库(改写历史不可逆,一定要做)

    git clone --mirror your-repo-url backup-repo.git
    

    这会克隆包含所有分支、标签的完整镜像,作为备份。

  2. 移除历史中所有子模块目录的痕迹
    遍历所有提交,彻底删除指定子模块路径的所有记录:

    git filter-repo --path path/to/submodule --invert-paths
    
  3. 清理历史中.gitmodules文件里的子模块条目
    创建一个替换规则文件(比如命名为submodule-cleanup.txt),内容如下:

    [regexp]\[submodule "path/to/submodule"\]
    replace:
    [regexp]\s*path = .*
    replace:
    [regexp]\s*url = .*
    replace:
    

    然后执行命令,批量清理所有历史提交里的对应子模块配置:

    git filter-repo --replace-text submodule-cleanup.txt
    
  4. 清理本地仓库的残留配置和文件
    执行你已经熟悉的命令,清理当前工作区和本地Git配置中的子模块关联:

    # 从.git/config中移除子模块条目
    git submodule deinit -f path/to/submodule
    
    # 删除.git/modules中的子模块目录
    rm -rf .git/modules/path/to/submodule
    
    # 删除.gitmodules中的条目和本地子模块目录
    git rm -f path/to/submodule
    
  5. 强制推送修改到远程仓库
    因为改写了仓库历史,需要强制推送所有分支和标签:

    git push --force --all
    git push --force --tags
    

注意:所有协作开发者需要重新克隆仓库,本地旧的历史版本无法和修改后的远程仓库正常合并,不要用git pull,直接重新克隆即可。

内容的提问来源于stack exchange,提问作者Dmitrii Motorygin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 16:23:11