【工具】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>
相关推荐
echome8887 小时前
Git 日常救急:撤销提交、找回误删文件和解决冲突
git·版本控制
赵民勇12 小时前
Git命令详解
git
不老刘1 天前
Antigravity 的 New Worktree 模式:给 AI 代理一个安全的“平行工位”
人工智能·git·ai编程·antigravity
光谱SpectrAI1 天前
Windows 也中招:Git Bash 和 Node 仍信任泄露的 eMudhra 证书
git
YIN_尹1 天前
【git原理与使用】git初识与安装
大数据·git·elasticsearch
Leo_Mr1 天前
Vue3 项目配置 Git 提交规范(企业通用方案:husky + commitlint + cz-git + lint-staged)
git
jyOverQ2 天前
Git rebase 和 merge 有什么区别?什么时候该用哪个?
git
晗夜未央2 天前
跨仓库 Skill 换台电脑就失效?用 Git remote 校验项目
人工智能·git·python
Lsetea2 天前
Git访问HTTPS证书不受信任:用URL级sslCAInfo限定私有CA范围
git·https·开发工具·ssl证书·ca证书
এ慕ོ冬℘゜2 天前
Git 基础入门|和 SVN 对比 + 四大区域 + 常用命令详解
git·svn