動機
公司有若干個項目,并且各個項目有關聯(代碼層次),這時我們需要抽取出一個公共模塊供其它項目使用,這時我們就需要使用
submodule(子模塊)
使用步驟
進入項目目錄(.git 目錄)
-
使用命令添加子模塊
git submodule add git@git.xxxxxx.com:app-labs/swift-mkit.git swift-mkit
- 這時會 clone 子模塊,并且生成兩個文件,如圖:
- git_submodule_add_status.png
.gitmodules
中包含子模塊的詳細信息
clone 包含子模塊的項目
方式一:遞歸 clone 完整的項目
git clone --recursive https://git.coding.net/chernyog/XXX.git
方式二:先 clone 父項目,再更新子項目
- clone 父項目
git clone https://git.coding.net/chernyog/XXX.git
- 此時子模塊也會出現,只是空目錄而已!
- 初始化子模塊
git submodule init
- 更新子模塊
git submodule update
- 注意:必須先初始化子模塊,否則報錯!
- git_submodule_update_error.png