AI 执行指令
添加 Submodule
git submodule add <url> <path>
git add .gitmodules <path>
git commit -m "<emoji> 添加 submodule"
克隆后初始化
git clone --recurse-submodules <url>
# Git Submodule AI 执行指南
git clone <url> && git submodule update --init
更新 Submodule
cd <submodule-dir>
git checkout <version> || git pull origin main
cd ..
git add <submodule-dir>
git commit -m "<emoji> 更新 submodule"
两层提交规则
Submodule 有两层独立提交:
- 子仓库:提交实际内容
cd submodule && git commit -m "..." && git push - 父仓库:提交 commit ID 引用
cd .. && git add submodule && git commit -m "..."
核心概念
| 概念 | 说明 |
|---|---|
| 子仓库代码 | submodule 目录的实际文件 |
| 子仓库引用 | .git/modules/<name>/ 指向的 .git |
| 索引记录 | Git index 中的 commit ID (160000 mode) |
常见问题
Q: submodule 目录为空?
git submodule update --init
Q: 完全移除 submodule?
git submodule deinit <name>
git rm <name>
rm -rf .git/modules/<name>
git commit -m "<emoji> 移除 submodule"
命令速查
| 命令 | 用途 |
|---|---|
git submodule add <url> <path> | 添加 |
git submodule update --init | 初始化 |
git submodule update --init <name> | 初始化特定 |
git submodule update --init --recursive | 递归初始化 |
git submodule deinit <name> | 移除本地工作目录 |
git submodule rm <name> | 完全移除 |
相关
- 前置:Git 多 Remote 配置双向同步
- 相关:Git-Commit指南