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

如何将其他Git仓库的指定文件夹作为子模块添加到目标目录?

解决Git子模块仅添加指定子目录的问题

Git原生子模块功能不支持直接只引入远程仓库的某个子目录,它会默认克隆整个仓库。要实现只获取utils/helper文件夹到当前仓库的server/core目录,你可以用**稀疏检出(Sparse Checkout)**配合子模块来实现,步骤如下:

  • 先将整个utils仓库作为子模块添加到目标路径:
    git submodule add -b main git@github.com:bhupender/utils.git server/core
    
  • 进入子模块目录,启用稀疏检出功能:
    cd server/core
    git config core.sparseCheckout true
    
  • 配置只需要检出的utils/helper目录:
    echo "utils/helper/" >> .git/info/sparse-checkout
    
  • 拉取指定目录的内容:
    git pull
    
  • 返回主仓库,提交子模块的配置变更:
    cd ../..
    git add server/core .gitmodules
    git commit -m "Add utils/helper as sparse submodule to server/core"
    

补充说明

如果拥有原utils仓库的权限,也可以考虑将utils/helper目录单独拆分为一个独立Git仓库,这样就能直接用普通子模块命令添加,但这种方式需要修改原仓库结构,适合长期维护的场景。

内容的提问来源于stack exchange,提问作者Bhupender Verma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:22:08