使用Git Submodule管理子模塊

使用場景

基於公司的項目會愈來愈多,經常須要提取一個公共的類庫提供給多個項目使用,可是這個library怎麼和git在一塊兒方便管理呢?
咱們須要解決下面幾個問題:git

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

  • library庫在其餘的項目中被修改了能夠更新到遠程的代碼庫中?vim

  • 其餘項目如何獲取到library庫最新的提交?服務器

  • 如何在clone的時候可以自動導入library庫?工具

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

什麼是Submodule?

git Submodule 是一個很好的多項目使用共同類庫的工具,他容許類庫項目作爲repository,子項目作爲一個單獨的git項目存在父項目中,子項目能夠有本身的獨立的commitpushpull。而父項目以Submodule的形式包含子項目,父項目能夠指定子項目header,父項目中會的提交信息包含Submodule的信息,再clone父項目的時候能夠把Submodule初始化。code

在項目中使用Submodule

使用git命令能夠直接添加Submodule遞歸

git submodule add git@github.com:jjz/pod-library.git pod-libraryrem

使用 git status命令能夠看到get

git status

On branch master
    Changes to be committed:
    
        new file:   .gitmodules
        new file:   pod-library

能夠看到多了兩個須要提交的文件:.gitmodulespod-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下來

  1. 第二種方法先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-pr...
子項目:https://github.com/jjz/pod-li...

相關文章
相關標籤/搜索