使用Git Submodule管理子模組

來源:互聯網
上載者:User
 

執行個體代碼: 父項目:https://github.com/jjz/pod-project 子項目:https://github.com/jjz/pod-library 使用情境

基於公司的多項目,我們提取了一個公用的類庫提供給多重專案使用,但是這個library怎麼和git在一起方便的管理呢。 需要解決以下的幾個問題: 如何在git項目中匯入library庫? library庫在其他的項目中被修改了如何push? 其他項目如何擷取到library庫最新的提交? 如何在clone的時候能夠自動匯入library庫?

解決以上問題,我使用git 的Submodule來解決。 什麼是Submodule?

git Submodule 是一個很好的項目協作工具,他允許類庫項目做為repository,子項目做為一個單獨的git項目存在父項目中,子項目可以有自己的獨立的commit,push,pull。而父項目以Submodule的形式包含子項目,父項目可以指定子項目header,父項目中會提交 Submodule的資訊,在clone父項目的時候可以把Submodule初始化。 在項目中添加Submodule

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

使用 git status命令可以看到

git statusOn branch masterChanges to be committed:    new file:   .gitmodules    new file:   pod-library

多了兩個需要提交的檔案
gitmodules 內容

[submodule "pod-library"]    path = pod-library    url = git@github.com:jjz/pod-library.git

這裡記錄了子項目的目錄和子項目的git資訊

pod-libraySubproject commit 4ac42d2f8b9ba0c2f0f2f2ec87ddbd529275fea5

這裡是子項目的commit的id,git並不會記錄Submodule的檔案變動,它是按照這個commit的git來對比Submodule的變動的

這兩個檔案都需要提交到父項目的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    modified:   pod-library/UseAFHTTP.h

commit submodule

git commit -a -m'test submodule'

push 到遠端
>git push

然後再回到父目錄:

cd ..git statuson branch master    modified:   pod-library (new commits)

可以看到pod-library已經變更為最新的commit id

Subproject commit 330417cf3fc1d2c42092b20506b0d296d90d0b5f
我們需要把推送到父項目的遠端

git commit -m'update submodule'git push
更新Submodule

更新的方法有兩種: 在父項目的目錄下運行

git submodule foreach git pull

在Submodule的目錄下面更新

cd pod-library
git pull

注意更新Submodule的時候如果有新的commit id產生,需要在父項目產生一個新的提交,pod-libray檔案中的 Subproject commit會變為最新的commit id 在clone的時候初始化Submodule 採用遞迴參數 --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 0Receiving 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 0Receiving 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'

會自動init Submodule

或者使用第二種方法
先clone父項目

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'

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 0Receiving 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-projectgit rm --cached pod-libraryrm -rf pod-libraryrm .gitmodules vim .git/config  [submodule "pod-library"]      url = git@github.com:jjz/pod-library.git   刪除submodule相關的內容 git commit -a -m 'remove pod-library submodule'


作者:薑家志
連結:http://www.jianshu.com/p/d433d3417a19
來源:簡書
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.