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

如何在无分支名时将Git子模块更新至指定提交?

直接通过子模块URL和提交ID更新Git子模块的方法

针对已存在的子模块

如果子模块已在主项目的.gitmodules中配置,可直接执行以下操作:

  • 从指定URL拉取目标提交:
    git -C path/to/submodule fetch <子模块URL> <commit_id>
    
  • 强制切换到该提交:
    git -C path/to/submodule reset --hard <commit_id>
    
  • 回到主项目提交子模块的版本变更:
    git add path/to/submodule
    git commit -m "Update submodule to commit <commit_id>"
    

子模块未配置或需更新URL时

如果子模块还没添加,或者需要修改其远程URL:

  • 添加子模块(或更新已有子模块的URL):
    # 新增子模块
    git submodule add <子模块URL> path/to/submodule
    # 更新已有子模块的URL
    git submodule set-url path/to/submodule <子模块URL>
    
  • 拉取并切换到目标提交:
    git -C path/to/submodule fetch origin <commit_id>
    git -C path/to/submodule reset --hard <commit_id>
    
  • 提交主项目的配置和子模块版本变更:
    git add .gitmodules path/to/submodule
    git commit -m "Add/Update submodule to commit <commit_id>"
    

关键注意事项

  • 目标提交ID必须存在于子模块的远程仓库中,否则fetch操作会失败
  • reset --hard会丢弃子模块目录下所有未提交的本地修改,执行前请确认本地无需要保留的变更

内容的提问来源于stack exchange,提问作者KLAS R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 19:25:14