[Git] git submodule 项目形式
![[Git] git submodule 项目形式](https://cdn.hashnode.com/res/hashnode/image/upload/v1749887183287/f298a35a-badf-42c6-ae54-7b8a0ecf3575.png)
简介
个人感觉 git submodule 是一种相对于 monorepo 的多模块项目管理方式
对于一些可能在多个仓库/业务中复用的模块,可以考虑将其拆出来单独放到一个仓库中,通过 git submodule 的方式引入
一些通用的非业务代码,例如配置文件、 proto 、统一的类型声明文件等,比较适合拆成 git submodule
用法
参考文档:https://git-scm.com/book/en/v2/Git-Tools-Submodules
初次拉取 git submodule
使用 git submodule update --init 拉取,git 会根据 .gitmodules 文件将远程仓库拉取为 submodule
更新 git submodule
可以在主仓库中使用
git pull,会自动递归拉取所有的子模块,并将子模块的引用更新到远程仓库的最新引用:

在主仓库中执行
git submodule update --remote,也会将子模块的引用更新到远程仓库的最新引用如果在执行了
git submodule update --remote后出现了冲突,那么你可能需要git submodule update --remote --rebase来在rebase的过程中手动解决冲突(或者用merge):
在主仓库中提交 git submodule 的变更
当使用了 git submodule 后,每次更新仓库的远程引用(通过 fetch)时,都会显示 submodule 的变更
需要你在主仓库中去将这些子模块都添加到暂存区中,然后提交
![[JS] fetch 不捕获 4xx](https://cdn.hashnode.com/res/hashnode/image/upload/v1752292253648/9db0ffc4-101a-41a9-9484-b3891c2d81b3.png)
![[Node] Mac m 芯片使用 node 14](https://cdn.hashnode.com/res/hashnode/image/upload/v1752243750001/cf2a586a-c5f8-488c-84a7-a3f7068f34b2.jpeg)
![[其他] WSL Interop](https://cdn.hashnode.com/res/hashnode/image/upload/v1752241086475/51441fc0-e5d9-4423-b009-ac9af9a51c04.jpeg)