Git: submodule指令
之前有鑑於工作處理的專案愈來愈多,面臨到程式碼在不同專案重複的問題。基於程式碼只能在公司內部發布使用,找了一些方法,如果不想用引入套件的方式去共用程式碼,可以用git submodule去處理。
基本操作指令
子模組路徑 <submodule_dir> 要是不存在的目錄,不可先建立空目錄
# 新增submodule
$ git submodule add <remote_repo_url> <submodule_dir>
# 查看子模組目前版本
$ git submodule
$ git submodule status
# 更新(取得)子模組remote repo狀態
$ git submodule update --remote
$ git submodule update --recursive --remote
# 移除子模組
$ git submodule deinit
$ git submodule deinit -f
刪除submodule步驟
# step 1. 刪除子模組的"子目錄樹(filetree)"和.gitmodules's submodule entry
$ git rm <path_to_submodule>
# step 2. 刪除./git/modules內的子模組
$ rm -rf .git/modules/<path_to_submodule>
# step 3. 刪除git config的子模組資訊
$ git config --remove-section submodule.<path_to_submodule>
Clone有submodule的專案
# 方法1. 完整clone一個有子模組的專案(建議)
$ git clone --recursive
# 方法2. 兩步驟clone
$ git clone
$ git submodule init
$ git submodule update --remote --recursive
References