主题
Git 子模块
Git 子模块允许一个 Git 仓库作为另一个仓库的子目录存在,用于管理项目依赖的外部代码库。
添加子模块
在主仓库中添加子模块:
bash
git submodule add <仓库地址> <路径>
例如:
bash
git submodule add https://github.com/example/lib.git libs/lib
这会将外部仓库作为子目录引入。
初始化与更新子模块
克隆含有子模块的仓库后,需要初始化并更新子模块:
bash
git submodule init
git submodule update
或者使用一条命令:
bash
git submodule update --init --recursive
子模块的提交和同步
对子模块代码进行修改后,需要进入子模块目录提交,并回到主仓库提交子模块的引用更新:
bash
cd libs/lib
git add .
git commit -m "更新子模块代码"
cd ../../
git add libs/lib
git commit -m "更新子模块引用"
删除子模块
删除子模块需要从 .gitmodules
文件中移除配置,删除子模块目录并提交。
合理使用子模块有助于项目模块化管理,提高复用和维护效率。