【工具】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>
相关推荐
乐观的冷风33 分钟前
我们需要将文件添加到暂存区才能提交,而移除文件后是无法添加到暂存区的,那么怎么移除一个文件让GIT不再将其纳入版本控制呢?上图中GIT已经给出了说明:
git
tjl521314_212 小时前
Git SSH Host Key Verification Failed 问题排查与解决
运维·git·ssh
嘶哈哈哈4 小时前
[特殊字符] Git 协作指南
git
say_fall4 小时前
【Git 精品详解】企业规范:企业级开发模型、Git Flow、发版流程规范、Code Owner 制度、事故应急回滚
大数据·linux·服务器·git·学习·elasticsearch
江畔柳前堤1 天前
第13章:docker生产环境部署实战
运维·git·docker·容器·代码复审
GuHenryCheng1 天前
【ESP32】ESP-IDF开发环境搭建(cursor)
git·stm32·单片机·学习
kaka。。1 天前
GIT安装+代码推送
git
明理的路灯1 天前
安装 git 开发工具
git
偏爱自由 !1 天前
5:IDEA中git的使用--git fetch 和 git pull 和解决冲突
git·elasticsearch·intellij-idea
偏爱自由 !1 天前
2:IDEA中git的使用--基础操作
java·git·intellij-idea