轉自:https://segmentfault.com/a/1190000003076028
使用場景
基於公司的項目會越來越多,常常需要提取一個公共的類庫提供給多個項目使用,但是這個library
怎么和git
在一起方便管理呢?
我們需要解決下面幾個問題:
-
如何在git項目中導入
library庫
? -
library庫
在其他的項目中被修改了可以更新到遠程的代碼庫中? -
其他項目如何獲取到
library庫
最新的提交? -
如何在clone的時候能夠自動導入
library庫
?
解決以上問題,可以考慮使用git的 Submodule
來解決。
什么是Submodule?
git Submodule
是一個很好的多項目使用共同類庫的工具,他允許類庫項目做為repository
,子項目做為一個單獨的git項目
存在父項目中,子項目可以有自己的獨立的commit
,push
,pull
。而父項目以Submodule
的形式包含子項目,父項目可以指定子項目header
,父項目中會的提交信息包含Submodule
的信息,再clone父項目
的時候可以把Submodule
初始化。
在項目中使用Submodule
使用git
命令可以直接添加Submodule
:
git submodule add git@github.com:jjz/pod-library.git pod-library
使用 git status
命令可以看到
git status
On branch master
Changes to be committed: new file: .gitmodules new file: pod-library
可以看到多了兩個需要提交的文件:.gitmodules
和 pod-library
.gitmodules
內容包含Submodule
的主要信息,指定reposirory
,指定路徑:
[submodule "pod-library"] path = pod-library url = git@github.com:jjz/pod-library.git
可以看到記錄了子項目的目錄和子項目的git
地址信息。pod-libray
內容只保護子項目的commit id
,就能指定到對於的git header
上,例如:
Subproject commit 4ac42d2f8b9ba0c2f0f2f2ec87ddbd529275fea5
4ac42d2f8b9ba0c2f0f2f2ec87ddbd529275fea5
就是子項目的commit id
,父項目的git並不會記錄Submodule
的文件變動,它是按照commit git
指定Submodule
的git header
。
另外,這兩個文件都需要提交到父項目的git中。
還可以這樣使用命令添加Submodule
git add .gitmodules pod-ibrary git commit -m "pod-library submodule" git submodule init
修改Submodule
首先需要確認有對Submodule的commit權限。
進入Submodule
目錄里面:
cd pod-library/
修改其中的一個文件看下文件的可以用git status
查看變動:
git status
modified: pod-library/UseAFHTTP.h
提交Submodule
的更改內容:
git commit -a -m'test submodule'
然后push
到遠程服務器:
git push
然后再回到父目錄,提交Submodule
在父項目中的變動:
cd ..
git status
on branch master
modified: pod-library (new commits)
可以看到pod-library
中已經變更為Submodule
最新的commit id
:
Subproject commit 330417cf3fc1d2c42092b20506b0d296d90d0b5f
需要把Submodule
的變動信息推送到父項目的遠程服務器
git commit -m'update submodule'
git push
這樣就把子模塊的變更信息以及子模塊的變更信息提交到遠程服務器了,從遠程服務器上更新下來的內容就是最新提交的內容了。
更新Submodule
更新Submodule
有兩種方式:
在父項目的目錄下直接運行
git submodule foreach git pull
在Submodule的目錄下面更新
>cd pod-library git pull
可以看到在Submodule
的目錄中,使用git
和單獨的一個項目是一樣的,注意更新Submodule
的時候如果有新的commit id
產生,需要在父項目產生一個新的提交,pod-libray文件中的 Subproject commit
會變為最新的commit id
。
clone Submodule
clone Submodule
有兩種方式 一種是采用遞歸的方式clone整個項目,一種是clone父項目,再更新子項目。
-
采用遞歸參數
--recursive
git clone git@github.com:jjz/pod-project.git --recursive
輸出結果:
loning into 'pod-project'... remote: Counting objects: 57, done. remote: Compressing objects: 100% (45/45), done. remote: Total 57 (delta 13), reused 49 (delta 8), pack-reused 0 Receiving objects: 100% (57/57), 18.79 KiB | 0 bytes/s, done. Resolving deltas: 100% (13/13), done. Checking connectivity... done. Submodule 'pod-library' (git@github.com:jjz/pod-library.git) registered for path 'pod-library' Cloning into 'pod-library'... remote: Counting objects: 34, done. remote: Compressing objects: 100% (25/25), done. remote: Total 34 (delta 8), reused 30 (delta 7), pack-reused 0 Receiving objects: 100% (34/34), 12.95 KiB | 0 bytes/s, done. Resolving deltas: 100% (8/8), done. Checking connectivity... done. Submodule path 'pod-library': checked out '330417cf3fc1d2c 42092b20506b0d296d90d0b5f'
可以看到init Submodule
會自動被clone
下來
2.第二種方法先clone父項目,再初始化Submodule
git clone git@github.com:jjz/pod-project.git
cd pod-project
git submodule init
輸出:
Submodule 'pod-library' (git@github.com:jjz/pod-library.git) registered for path 'pod-library'
更新Submodule
:
git submodule update
運行結果:
Cloning into 'pod-library'... remote: Counting objects: 34, done. remote: Compressing objects: 100% (25/25), done. remote: Total 34 (delta 8), reused 30 (delta 7), pack-reused 0 Receiving objects: 100% (34/34), 12.95 KiB | 0 bytes/s, done. Resolving deltas: 100% (8/8), done. Checking connectivity... done. Submodule path 'pod-library': checked out '330417cf3fc1d2c42092b20506b0d296d90d0b5f'
刪除Submodule
git
並不支持直接刪除Submodule
需要手動刪除對應的文件:
cd pod-project
git rm --cached pod-library rm -rf pod-library rm .gitmodules
更改git的配置文件
config
:
vim .git/config
可以看到Submodule
的配置信息:
[submodule "pod-library"] url = git@github.com:jjz/pod-library.git
刪除submodule相關的內容,然后提交到遠程服務器:
git commit -a -m 'remove pod-library submodule'
代碼地址:
父項目:https://github.com/jjz/pod-project
子項目:https://github.com/jjz/pod-library