使用Git Submodule管理子模塊


轉自:https://segmentfault.com/a/1190000003076028

使用場景

基於公司的項目會越來越多,常常需要提取一個公共的類庫提供給多個項目使用,但是這個library怎么和git在一起方便管理呢?
我們需要解決下面幾個問題:

  • 如何在git項目中導入library庫?

  • library庫在其他的項目中被修改了可以更新到遠程的代碼庫中?

  • 其他項目如何獲取到library庫最新的提交?

  • 如何在clone的時候能夠自動導入library庫?

解決以上問題,可以考慮使用git的 Submodule來解決。

什么是Submodule?

git Submodule 是一個很好的多項目使用共同類庫的工具,他允許類庫項目做為repository,子項目做為一個單獨的git項目存在父項目中,子項目可以有自己的獨立的commitpushpull。而父項目以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指定Submodulegit 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父項目,再更新子項目。

  1. 采用遞歸參數 --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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM