---
# System prepended metadata

title: 【軟體】Git Submodule
tags: [TTennis Pickup Robot]

---

---
title: 【軟體】Git Submodule
tags: TTennis Pickup Robot
disqus: hackmd
---

<h1 style="text-align: center; color: orange;">
🛠️ 【軟體】版控工具 Git 🛠️
</h1>

<h2 style="text-align: center; color: skyblue;">Git Submodule</h2>

<center>
一個專案可能會有很多不同系統的檔案，全放在同個 repo 裡可能顯得紊亂。
	
這時可以用子模組 submodule，方便管理和增加可讀性。
	
![image](https://hackmd.io/_uploads/BJBYVwXsR.png)

如[上圖](https://github.com/pomelo925/tel2022_DoItTomorrow)，點擊 submodule 後會跳轉至對應的 Github Repo。

</center>

---

</br>

<h3><font color ="magenza"> 1. 建立步驟</font></h3>

:::info
**前情提要：**
有兩個已經在 Github 上的 repository  A、B 。
現在要再新創一個 repo（父模組），並以 submodule 的方式連接此兩個 repo。
:::

<font color = "yellow"> step 1. 於本地端，開一個新的空資料夾，並初始化 git。
</font>
```=
git init
```

</br>

<font color = "yellow"> step 2. 建立子模組關係。</font>

在 Git Bash 中輸入以下指令。（這個指令包含 git clone A_repo。）
```=
git submodule add <A repo> <local path>
```
* `<remote repository>` ：子模組的 URL。
* `<local path>` ：本地端中，子模組相對於父模組的路徑位置。

:::spoiler 參考指令
```=
git submodule add git@github.com:pomelo925/TEL_STM_1.git STM_1
```
:::


:::success
這個 local path 理解成在當前資料夾中「新增資料夾的名字」就可以了。
可參考 step 4. 的圖片，這個名字也會是 github 上 submodule 的名稱前綴。
:::

</br>

<font color = "yellow">step 3. 回到資料夾中確認有子模組資料夾和一個 `.gitmodules`。</font>

![](https://i.imgur.com/7tzOGO8.png)

</br>

<font color = "yellow">step 4.將 parent module 推送到 GitHub 上。</font>

![](https://i.imgur.com/pEs7EV5.png)


</br>

<h3><font color ="magenza">2. 子模組更新</font></h3>

:::warning
submodule 紀錄的是 commit 版本，在 `@` 後方的就是版本的流水碼。
submodule 更新後，paremnt module 導向的仍是過去特定提交紀錄的版本。
這個章節是在說明，如何更新 parent module 裡導向 submodule 的版本。
:::

1. 移至 parent module 資料夾層級 ( root folder )。
	```=
	git submodule update --init
	```

2. 移至 submodules 資料夾層級。
`<branch name>` 切換成要提取的分支名稱。
	```=
	cd <submodule>
	git checkout <branch_name>
	git pull origin <branch_name>
	```


</br> </br> </br> </br> </br>