iT邦幫忙

5

在Git上使用另一個Git-Git Submodules(子模組)

前言

在某家A開頭的公司曾經遇到一個問題,其實我們登入都是撈取另外一個資料庫去比對帳號密碼,但是每次我們寫不同系統的時候,都還要每次Copy一份;這時候我的一個前輩找到一個解法:打包成Dll然後架設私人的Nuget Package Server,希望所有人能一起更改Code而且不用對版本號;有一天又聊這件事情的時候,他又突然說可以用Git 子模組就好了!後來聽聽概念後也沒有時間實作。在我離開公司後,把他搭配VS實作出來,前一陣子約出來跟他聊天後,結果他..給..我..忘..記..這..件..事..情!還..一..臉..驚..呆!

環境架構

windows10
git 2.17.1
TortoiseGit 2.7

前置安裝

1.安裝Git

Git 介紹與在Windows系統下安裝

2.安裝TortoiseGit(如果習慣用Command可以不用安裝)

TortoiseGit | 傻瓜GIT教學

操作方式

現在方式是使用TortoiseGit的方式,如果要用其他想要Command Line之後補上

1.找尋一個有要加入子模組的Git和子模組的Git

如下圖,有三個Git
TestTestSonTestGrandson
那我們設定的邏輯 TestGrandson 要在 TestSon 底下,然後TestSon要在Test底下
https://ithelp.ithome.com.tw/upload/images/20190129/201135424fQHsElCiu.png

我們先看三個人內容
Test裡面有Test.txt
https://ithelp.ithome.com.tw/upload/images/20190129/20113542L6w68dq1ar.png

TestSon裡面有TestSon.txt
https://ithelp.ithome.com.tw/upload/images/20190129/20113542qJDAv3Sywc.png

TestGrandson裡面有TestGrandson.txt
https://ithelp.ithome.com.tw/upload/images/20190129/20113542JVTCLNMHUC.png

2.開始加入子模組

先以把TestGrandson加入TestSon為例

到TestSon底下的資料夾按右鍵,
1.先選TortoiiseGit
2.然後選Submodule Add
https://ithelp.ithome.com.tw/upload/images/20190129/20113542xy8LinvMZ9.png

加入子模組設定
1.你要加入的子模組
2.你要把子模組加入到哪一個路徑(切記指定的路徑不能有同名的資料夾)
3.確認
https://ithelp.ithome.com.tw/upload/images/20190129/201135425ZLooDtWnm.png

完成後出現的圖示
1.剛剛你設定子模組的路徑
2..gitmodulesGit:關於子模組的紀錄檔
https://ithelp.ithome.com.tw/upload/images/20190129/20113542KN1RzGVz4O.png

檢查一下TestGrandSon裡面是不是有東西
https://ithelp.ithome.com.tw/upload/images/20190129/20113542JerBGTBP4l.png

恩~看起來沒錯
然後把加入後記得把**.gitmodulesGit** 和加入的子模組 Commit上去
這樣就大功告成啦!
https://ithelp.ithome.com.tw/upload/images/20190129/20113542XirWsWFErX.png

進階操作

如果你想加入的子模組裡面又帶一個子模組,會怎麼樣子呢?

操作方式如 前面的操作方式一樣

我以把TestSon加入到Test為例
直接顯示結果

恩~Test裡面有TestSon沒錯
https://ithelp.ithome.com.tw/upload/images/20190129/20113542RhKsHTlqQU.png

恩~TestSon資料夾裡面有TestGrandSon 也沒錯
https://ithelp.ithome.com.tw/upload/images/20190129/20113542Xm3SthacZY.png

在TestGrandSon 資料夾裡面居然是空的!!
https://ithelp.ithome.com.tw/upload/images/20190129/20113542YDpoW1QN2V.png

這時候小技巧就來了
到TestGrandSon資料夾裡面按右鍵
1.先選TortoiiseGit
2.然後選Submodule Update
https://ithelp.ithome.com.tw/upload/images/20190129/20113542FhyEKR3BxU.png

選取Whole Project
https://ithelp.ithome.com.tw/upload/images/20190129/20113542XNUlRnKXPu.png

選完後會這樣
1.選取你想要Update的Submodule
2.選擇Initialize submodules (--init) 初始化Submodule
3.按確認
https://ithelp.ithome.com.tw/upload/images/20190129/20113542fcYY5diNgN.png

確認完後,她就幫你更新
就出現資料啦,大功告成
https://ithelp.ithome.com.tw/upload/images/20190129/20113542rmpuIoLvnI.png

感想

子模組是一個開發不錯的方法,有時候你專案需要把東西切成模組,但是又想要能隨時更改,使用Git子模組是一個不錯的方式,感謝之前的前輩總是想一些怪點子、KeyWord(他每次講完都常常忘記),讓我到現在能回去找相關資料研究起來。

參考文件

Git 工具 - 子模組 (Submodules)
Git 介紹與在Windows系統下安裝
TortoiseGit | 傻瓜GIT教學


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言