【工具】Git 子仓库管理

文章目录

    • [添加 submodule](#添加 submodule)
    • [克隆带 submodule 的仓库](#克隆带 submodule 的仓库)
    • [更新 submodule](#更新 submodule)
    • [删除 submodule](#删除 submodule)

更好的阅读体验https://wiki.dwj601.cn/develop/tools/git/submodule/


当项目依赖另一个仓库(例如公共工具库、第三方组件、自家多个项目共用的模块)时,可以用 git submodule 将它挂载进来,而不是复制代码。这样的好处在于:

  • 独立更新或回退子仓库版本;
  • 避免将子仓库的提交历史混入主仓库;
  • 保持版本可控,而不是直接拉取最新代码。

添加 submodule

bash 复制代码
git submodule add <submodule_remote_url> <submodule_local_path>

克隆带 submodule 的仓库

标准方法:

bash 复制代码
# 克隆主仓库
git clone <remote_url>

# 进入主仓库
cd <project>

# 初始化子模块配置
git submodule init

# 同步数据
git submodule update

一步到位(克隆时):

bash 复制代码
git clone --recurse-submodules <remote_url>

一步到位(克隆后):

bash 复制代码
# 克隆主仓库
git clone <remote_url>

# 进入主仓库
cd <project>

# 克隆子仓库
git submodule update --init

更新 submodule

submodule 并不会自动同步最新代码,需要手动更新。

bash 复制代码
# 进入子仓库目录
cd <submodule>

# 同步代码
git pull <submodule_remote_name> <submodule_branch>

删除 submodule

bash 复制代码
# 删除子仓库
git rm -rf <submodule>

# 删除子仓库缓存
rm -rf .git/modules/<submodule>

# 删除子仓库配置
git config -f .git/config --remove-section submodule.<submodule>
相关推荐
吹牛不交税1 天前
visualstudio把项目绑定git仓库
git
c238561 天前
git常见错误和ssh验证推送
运维·git·ssh
火山上的企鹅1 天前
Codex实战:APP远程升级服务搭建(四)Node 服务端自动识别 APK 信息
android·服务器·git·github·qgc
徐子元竟然被占了!!1 天前
Git学习
git·学习·elasticsearch
l1o3v1e4ding1 天前
windows安装Claude Code,并接入Deepseek-v4模型 ,提供离线安装包
git·npm·node.js·claude code·cc-switchcc
Dontla2 天前
git bash打开Claude code报错:Claude Code on Windows requires git-bash.(别把git装其他位置,严格按照默认安装)找不到claude code
windows·git·bash
一个梦醒了2 天前
安装git bash选项推荐
开发语言·git·bash
呆萌的代Ma2 天前
Git不强行推送,撤销最近几次的提交
git
透明的玻璃杯2 天前
git应用
git
炸炸鱼.2 天前
Git+Jenkins实战(一):从零搭建自动化发布与回滚系统(附完整代码)
git·jenkins