Skip to main content

Command Palette

Search for a command to run...

Git: submodule指令

Updated
1 min readView as Markdown

之前有鑑於工作處理的專案愈來愈多,面臨到程式碼在不同專案重複的問題。基於程式碼只能在公司內部發布使用,找了一些方法,如果不想用引入套件的方式去共用程式碼,可以用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

More from this blog

Vite: vite-plugin-html的bugs和坑

因為工作要用Vite去生成一個mpa專案且根據頁面不同,動態生成不同 <title> 的HTML,所以就找了一個套件──vite-plugin-html,結果因為套件好像很久沒維護,隨著Vite升級又多了一些有的沒的bug,順手來記錄一下。 template delete 像我第一個就是遇到生成的時候template HTML也會被刪掉,後來看到issue上也有人反映,還好有好心人士解決這個問題,

Feb 26, 20261 min read4

Lun's Blog

82 posts

Notes for web development