Try   HackMD

🛠️ 【軟體】版控工具 Git 🛠️

Git Submodule

一個專案可能會有很多不同系統的檔案,全放在同個 repo 裡可能顯得紊亂。

這時可以用子模組 submodule,方便管理和增加可讀性。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

上圖,點擊 submodule 後會跳轉至對應的 Github Repo。



1. 建立步驟

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

step 1. 於本地端,開一個新的空資料夾,並初始化 git。

git init

step 2. 建立子模組關係。

在 Git Bash 中輸入以下指令。(這個指令包含 git clone A_repo。)

git submodule add <A repo> <local path>
  • <remote repository> :子模組的 URL。
  • <local path> :本地端中,子模組相對於父模組的路徑位置。
參考指令
git submodule add git@github.com:pomelo925/TEL_STM_1.git STM_1

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


step 3. 回到資料夾中確認有子模組資料夾和一個 .gitmodules

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


step 4.將 parent module 推送到 GitHub 上。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


2. 子模組更新

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>