###### tags: `東京威力 TEL` # VI. GitHub ## <font color = "orange">03. 子模組</font> > 一個專案可能會有很多不同系統、功能的package,如果把他們都放在同個 repo 可能會顯得紊亂。這時候可以用子模組 submodule,方便管理和增加可讀性喔 ~ 以下小節順序是設定子模組的步驟順序,**非常簡單**。 ### <font color = "pink">3-1. 建立步驟</font> 前情提要: 有兩個已經在 Github 上的 repo A、B 。我們想再創一個新的 repo,並以 submodule 的方式連到這兩個子 repo。 <font color = "yellow"> step 1. 於本地端,開一個新的空資料夾,並初始化 git。 </font> ```= git init ``` <font color = "yellow"> step 2. 建立子模組關係。</font> 在 Git Bash 中輸入以下指令。(這個指令包含 git clone A_repo。) ```= git submodule add <A repo> <local path> ``` `<remote repository>` 是子 Repository 的 URL。 `<local path>` 則是子 Repo 在本地端主 Repository 的路徑位置。 示範如下: ```= git submodule add git@github.com:pomelo925/TEL_STM_1.git STM_1 ``` :::success 這個 local path 理解成在當前資料夾中「新增的資料夾的名字」就可以了。 可以參考 4. 的圖片,這個名字也會是將來在 github 上 submodule 的名稱前綴。 ::: <font color = "yellow">step 3. 回到資料夾中確認有子模組資料夾和一個 `.gitmodules`。</font> ![](https://i.imgur.com/7tzOGO8.png) <font color = "yellow">step 4. Push 主 Repo 到 GitHub 上。</font> ![](https://i.imgur.com/pEs7EV5.png) ### <font color="pink">3-2. update submodules</font> > 前面那張的「主 repo」正確名稱是 parent modules。 :::success parent modules 並不會自動更新 submodules,因此也要手動更新。 當然,可以透過指令或是 Github 的圖形化工具。 ::: 移動到 parent module 資料夾層級 ( root folder )。 ``` git submodule update --init ``` 接著移動到 submodules 資料夾層級。 這裡的 `<branch name>` 切換成想要 git pull 的 branch 版本。 ```yaml git checkout <branch_name> git pull origin <branch_name> ```