git 子模块管理(一个仓库中有多个子仓库)
使用 Git 子模块管理 B 和 C 仓库
在A仓库中维护B和C仓库
-
进入 A 仓库:
-
添加 B 和 C 作为子模块:
git submodule add https://your-repo-url/B.git B-repo
git submodule add https://your-repo-url/C.git C-repo
git commit -m "Add B and C as submodules"
git push origin main
- 同步和更新子模块(第一次):
# --init:初始化未初始化的子模块。
# --recursive:递归更新所有嵌套子模块(如果子模块中还有子模块)
git submodule update --init --recursive
- 同步子模块(后续维护)
# --remote:从远程仓库拉取最新提交。
# --merge:将最新的子模块变更合并到当前分支。
git submodule update --remote --merge
- 推送到远程仓库
粘贴远程仓库的同步指令
git remote add origin https://github.com/fly-t/bootloader_sdio.git
git branch -M main
git push -u origin main
拉取指定分支的子模块
- 进入子模块目录
cd B
- 检查分支列表
查看子模块中的可用分支:
git branch -a # 查看本地和远程分支
- 切换到指定分支
git checkout -b b1 origin/b1
- 更新子模块指向的分支
要让子模块始终跟踪 b1 分支,可以在主仓库的 .gitmodules
文件中指定分支:
[submodule "B"]
path = B
url = <子模块的URL>
branch = b1 # 这里指定要跟踪的分支
然后执行以下命令更新:
git submodule update --remote --merge
- 提交更新到主仓库
cd ..
git add B
git commit -m "Update B submodule to branch b1"
git push