使用需求
存在一套公共的使用庫(kù),被很多其它項(xiàng)目使用,又或者很多APP使用的H5頁(yè)面需要掛到PC項(xiàng)目靜態(tài)文件中,單純的代碼放到PC項(xiàng)目下,維護(hù)又需要拉取整個(gè)PC項(xiàng)目修改后進(jìn)行提交,很不友好,這時(shí)候submodule就可以很友好的解決這些問(wèn)題!
開始
- 準(zhǔn)備兩個(gè)庫(kù):
一個(gè)公共庫(kù)
https://github.com/public.git
一個(gè)主線庫(kù)
https://github.com/masters.git
使用
git clone https://github.com/masters.git //拉取主線
git submodule add https://github.com/public.git public// 在主線庫(kù)中添加submodule
通過(guò)以上兩步,主線根目錄下會(huì)出現(xiàn).gitmodules,是配置文件(有多個(gè)子模塊,則此文件中將有多個(gè)條目),用于存儲(chǔ)項(xiàng)目的URL和您將其放入其中的本地子目錄之間的映射:
[submodule "public"]
path = public
url = https://github.com/public.git
主線目錄下查看.git文件(如果隱藏,勾選查看隱藏文件),文件中有一個(gè)config文件也會(huì)有對(duì)應(yīng)的配置
- (以上)兩個(gè)地方會(huì)出現(xiàn)對(duì)應(yīng)配置
初始化
克隆一個(gè)包含子倉(cāng)庫(kù)的倉(cāng)庫(kù)目錄,并不會(huì)clone下子倉(cāng)庫(kù)的文件,只是會(huì)克隆下.gitmodule描述文件,需要進(jìn)一步克隆子倉(cāng)庫(kù)文件。
cd masters
git submodule init
git submodule update
或者用一行命令
cd masters
git submodule update --init --recursive
此時(shí)子目錄在一個(gè)未命名分支,此時(shí)子倉(cāng)庫(kù)有改動(dòng)并沒(méi)有檢測(cè)到。
拉取submodule子模塊內(nèi)容
在子倉(cāng)庫(kù),切換到master分支,并git pull最新代碼之后,回到主倉(cāng)庫(kù)目錄,會(huì)顯示子倉(cāng)庫(kù)修改,需要在主倉(cāng)庫(kù)提交修改,即修改指定的commit id。
// public 相當(dāng)于子模塊目錄(子模塊倉(cāng)庫(kù))直接在當(dāng)前文件夾下
git pull
更新
如果在本地修改子倉(cāng)庫(kù),在主倉(cāng)庫(kù) git status會(huì)顯示子倉(cāng)庫(kù)有修改。
git status
//信息
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
(commit or discard the untracked or modified content in submodules)
modified: public (new commits, untracked content)
no changes added to commit (use "git add" and/or "git commit -a")
需要現(xiàn)在子倉(cāng)庫(kù)提交,然后再到主倉(cāng)庫(kù)提交代碼。
刪除子模塊
- 刪除.git/config 文件里的關(guān)聯(lián) (主線)
- 刪除.gitmodules里的關(guān)聯(lián)
- rm -rf .git/modules/public (也可手動(dòng)去刪除) .git文件夾下面modules還存在子模塊
1.手動(dòng)刪除子模塊,并提交主線,再一次add子模塊出現(xiàn)錯(cuò)誤
A git directory for 'submoduleSon' is found locally with remote(s):
origin https://github.com/public.git
If you want to reuse this local git directory instead of cloning again from
https://github.com/public.git
use the '--force' option. If the local git directory is not the correct repo
or you are unsure what this means choose another name with the '--name' option.
出現(xiàn)這樣的報(bào)錯(cuò).git/config文件和.gitsubmodule文件存在當(dāng)前public關(guān)聯(lián)(未刪除);
手動(dòng)再去刪除兩個(gè)文件中的關(guān)聯(lián)也不行;
用 git Base here 窗口
cd .git/modules // 切到.git文件下的modules
ls // 查看目錄
目錄: C:\工作文件夾\測(cè)試\submodule\submoduleFather\.git\modules
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/12/15 9:39 submodulePublic
d----- 2020/12/15 14:40 pulic
//發(fā)現(xiàn)public還存在(執(zhí)行刪除)
rm -rf public
ls
目錄: C:\工作文件夾\測(cè)試\submodule\submoduleFather\.git\modules
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/12/15 9:39 submodulePublic
//三個(gè)地方刪除完畢
git submodule add https://github.com/public.git (OKle)
如果關(guān)聯(lián)的子模塊名稱和目錄文件一致 還需要?jiǎng)h除掉中心生成如node_modules
// 常見五步走 完美去掉關(guān)聯(lián)關(guān)系
rm -rf 子模塊目錄 刪除子模塊目錄及源碼
.gitmodules 刪除項(xiàng)目目錄下.gitmodules文件中子模塊相關(guān)條目
.git/config 刪除配置項(xiàng)中子模塊相關(guān)條目
rm .git/module 刪除模塊下的子模塊目錄,每個(gè)子模塊對(duì)應(yīng)一個(gè)目錄,注意只刪除對(duì)應(yīng)的子模塊目錄即可
git rm —cached 子模塊名稱