1. 程式人生 > 程式設計 >Git Submodule使用完整教程(小結)

Git Submodule使用完整教程(小結)

自從看了蔣鑫的《Git權威指南》之後就開始使用Git Submodule功能,團隊也都熟悉了怎麼使用,多個子系統(模組)都能及時更新到最新的公共資源,把使用的過程以及經驗和容易遇到的問題分享給大家。

Git Submodule功能剛剛開始學習可能覺得有點怪異,所以本教程把每一步的操作的命令和結果都用程式碼的形式展現給大家,以便更好的理解。

1.對於公共資源各種程式設計師的處理方式

每個公司的系統都會有一套統一的系統風格,或者針對某一個大客戶的多個系統風格保持統一,而且如果風格改動後要同步到多個系統中;這樣的需求幾乎每個開發人員都遇到,下面看看各個層次的程式設計師怎麼處理:

假如對於系統的風格需要幾個目錄:css、images、js。

  • 普通程式設計師,把最新版本的程式碼逐個複製到每個專案中,如果有N個專案,那就是要複製N x 3次;如果漏掉了某個資料夾沒有複製…@(&#@#。
  • 文藝程式設計師,使用Git Submodule功能,執行:git submodule update,然後衝一杯咖啡悠哉的享受著。

引用一段《Git權威指南》的話: 專案的版本庫在某些情況蝦需要引用其他版本庫中的檔案,例如公司積累了一套常用的函式庫,被多個專案呼叫,顯然這個函式庫的程式碼不能直接放到某個專案的程式碼中,而是要獨立為一個程式碼庫,那麼其他專案要呼叫公共函式庫該如何處理呢?分別把公共函式庫的檔案拷貝到各自的專案中會造成冗餘,丟棄了公共函式庫的維護歷史,這顯然不是好的方法。

2.開始學習Git Submodule

“工欲善其事,必先利其器”!

既然文藝程式設計師那麼輕鬆就搞定了,那我們就把過程一一道來。

說明:本例採用兩個專案以及兩個公共類庫演示對submodule的操作。因為在一寫資料或者書上的例子都是一個專案對應1~N個lib,但是實際應用往往並不是這麼簡單。

2.1 建立Git Submodule測試專案

2.1.1 準備環境

➜ henryyan@hy-hp ~ pwd
/home/henryyan
mkdir -p submd/repos

建立需要的本地倉庫:

cd ~/submd/repos
git --git-dir=lib1.git init --bare
git --git-dir=lib2.git init --bare
git --git-dir=project1.git init --bare
git --git-dir=project2.git init --bare

初始化工作區:

mkdir ~/submd/ws
cd ~/submd/ws

2.1.2 初始化專案

初始化project1:

➜ henryyan@hy-hp ~/submd/ws git clone ../repos/project1.git 
Cloning into project1...
done.
warning: You appear to have cloned an empty repository.
 
➜ henryyan@hy-hp ~/submd/ws cd project1
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) echo "project1" > project-infos.txt
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ ls
project-infos.txt
 
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git add project-infos.txt 
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: project-infos.txt
#
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git commit -m "init project1"
[master (root-commit) 473a2e2] init project1
 1 files changed,1 insertions(+),0 deletions(-)
 create mode 100644 project-infos.txt
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) git push origin master
Counting objects: 3,done.
Writing objects: 100% (3/3),232 bytes,done.
Total 3 (delta 0),reused 0 (delta 0)
Unpacking objects: 100% (3/3),done.
To /home/henryyan/submd/ws/../repos/project1.git
 * [new branch]  master -> master
</file>

初始化project2:

➜ henryyan@hy-hp ~/submd/ws/project1 cd ..
➜ henryyan@hy-hp ~/submd/ws git clone ../repos/project2.git 
Cloning into project2...
done.
warning: You appear to have cloned an empty repository.
 
➜ henryyan@hy-hp ~/submd/ws cd project2
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) echo "project2" > project-infos.txt
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ ls
project-infos.txt
 
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git add project-infos.txt 
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: project-infos.txt
#
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git commit -m "init project2"
[master (root-commit) 473a2e2] init project2
 1 files changed,0 deletions(-)
 create mode 100644 project-infos.txt
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) git push origin master
Counting objects: 3,done.
To /home/henryyan/submd/ws/../repos/project2.git
 * [new branch]  master -> master
</file>

2.1.3 初始化公共類庫

初始化公共類庫lib1:

➜ henryyan@hy-hp ~/submd/ws git clone ../repos/lib1.git 
Cloning into lib1...
done.
warning: You appear to have cloned an empty repository.
➜ henryyan@hy-hp ~/submd/ws cd lib1 
➜ henryyan@hy-hp ~/submd/ws/lib1 git:(master) echo "I'm lib1." > lib1-features
➜ henryyan@hy-hp ~/submd/ws/lib1 git:(master) ✗ git add lib1-features 
➜ henryyan@hy-hp ~/submd/ws/lib1 git:(master) ✗ git commit -m "init lib1"
[master (root-commit) c22aff8] init lib1
 1 files changed,0 deletions(-)
 create mode 100644 lib1-features
➜ henryyan@hy-hp ~/submd/ws/lib1 git:(master) git push origin master
Counting objects: 3,227 bytes,done.
To /home/henryyan/submd/ws/../repos/lib1.git
 * [new branch]  master -> master

初始化公共類庫lib2:

➜ henryyan@hy-hp ~/submd/ws/lib1 git:(master) cd ..
➜ henryyan@hy-hp ~/submd/ws git clone ../repos/lib2.git 
Cloning into lib2...
done.
warning: You appear to have cloned an empty repository.
➜ henryyan@hy-hp ~/submd/ws cd lib2 
➜ henryyan@hy-hp ~/submd/ws/lib2 git:(master) echo "I'm lib2." > lib2-features
➜ henryyan@hy-hp ~/submd/ws/lib2 git:(master) ✗ git add lib2-features 
➜ henryyan@hy-hp ~/submd/ws/lib2 git:(master) ✗ git commit -m "init lib2"
[master (root-commit) c22aff8] init lib2
 1 files changed,0 deletions(-)
 create mode 100644 lib2-features
➜ henryyan@hy-hp ~/submd/ws/lib2 git:(master) git push origin master
Counting objects: 3,done.
To /home/henryyan/submd/ws/../repos/lib2.git
 * [new branch]  master -> master

2.2 為主專案新增Submodules

2.2.1 為project1新增lib1和lib2

➜ henryyan@hy-hp ~/submd/ws/lib2 git:(master) cd ../project1
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ls
project-infos.txt
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) git submodule add ~/submd/repos/lib1.git libs/lib1
Cloning into libs/lib1...
done.
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git submodule add ~/submd/repos/lib2.git libs/lib2
Cloning into libs/lib2...
done.
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ ls
libs project-infos.txt
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ ls libs 
lib1 lib2
 
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git status 
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: .gitmodules
# new file: libs/lib1
# new file: libs/lib2
#
 
# 檢視一下公共類庫的內容
 
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) cat libs/lib1/lib1-features
I'm lib1.
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) cat libs/lib2/lib2-features
I'm lib2.
</file>

好了,到目前為止我們已經使用git submodule add命令為project1成功添加了兩個公共類庫(lib1、lib2),查看了當前的狀態發現添加了一個新檔案(.gitmodules)和兩個資料夾(libs/lib1、libs/lib2);那麼新增的.gitmodules檔案是做什麼用的呢?我們檢視一下檔案內容便知曉了:

n@hy-hp ~/submd/ws/project1 git:(master) ✗ cat .gitmodules 
[submodule "libs/lib1"]
 path = libs/lib1
 url = /home/henryyan/submd/repos/lib1.git
[submodule "libs/lib2"]
 path = libs/lib2
 url = /home/henryyan/submd/repos/lib2.git

原來如此,.gitmodules記錄了每個submodule的引用資訊,知道在當前專案的位置以及倉庫的所在。

好的,我們現在把更改提交到倉庫。

➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git commit -a -m "add submodules[lib1,lib2] to project1"
[master 7157977] add submodules[lib1,lib2] to project1
 3 files changed,8 insertions(+),0 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 libs/lib1
 create mode 160000 libs/lib2
 
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) git push
Counting objects: 5,done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4),done.
Writing objects: 100% (4/4),491 bytes,done.
Total 4 (delta 0),reused 0 (delta 0)
Unpacking objects: 100% (4/4),done.
To /home/henryyan/submd/ws/../repos/project1.git
 45cbbcb..7157977 master -> master

假如你是第一次引入公共類庫的開發人員,那麼專案組的其他成員怎麼Clone帶有Submodule的專案呢,下面我們再clone一個專案講解如何操作。

2.3 Clone帶有Submodule的倉庫

模擬開發人員B……

➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) cd ~/submd/ws
➜ henryyan@hy-hp ~/submd/ws git clone ../repos/project1.git project1-b
Cloning into project1-b...
done.
➜ henryyan@hy-hp ~/submd/ws cd project1-b 
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) git submodule 
-c22aff85be91eca442734dcb07115ffe526b13a1 libs/lib1
-7290dce0062bd77df1d83b27dd3fa3f25a836b54 libs/lib2

看到submodules的狀態是hash碼和檔案目錄,但是注意前面有一個減號:-,含義是該子模組還沒有檢出。

OK,檢出project1-b的submodules……

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) git submodule init
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) git submodule update
Cloning into libs/lib1...
done.
Submodule path 'libs/lib1': checked out 'c22aff85be91eca442734dcb07115ffe526b13a1'
Cloning into libs/lib2...
done.
Submodule path 'libs/lib2': checked out '7290dce0062bd77df1d83b27dd3fa3f25a836b54'

讀者可以檢視:.git/config檔案的內容,最下面有submodule的註冊資訊!

驗證一下類庫的檔案是否存在:

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) cat libs/lib1/lib1-features libs/lib2/lib2-features
I'm lib1.
I'm lib2.

上面的兩個命令(git submodule init & update)其實可以簡化,後面會講到!

2.3 修改Submodule

我們在開發人員B的專案上修改Submodule的內容。

先看一下當前Submodule的狀態:

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) cd libs/lib1
➜ henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git status
# Not currently on any branch.
nothing to commit (working directory clean)

為什麼是Not currently on any branch呢?不是應該預設在master分支嗎?別急,一一解答!

Git對於Submodule有特殊的處理方式,在一個主專案中引入了Submodule其實Git做了3件事情:

  • 記錄引用的倉庫
  • 記錄主專案中Submodules的目錄位置
  • 記錄引用Submodule的commit id

在project1中push之後其實就是更新了引用的commit id,然後project1-b在clone的時候獲取到了submodule的commit id,然後當執行git submodule update的時候git就根據gitlink獲取submodule的commit id,最後獲取submodule的檔案,所以clone之後不在任何分支上;但是master分支的commit id和HEAD保持一致。

檢視~/submd/ws/project1-b/libs/lib1的引用資訊:

➜ henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 cat .git/HEAD
c22aff85be91eca442734dcb07115ffe526b13a1
➜ henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 cat .git/refs/heads/master    
c22aff85be91eca442734dcb07115ffe526b13a1

現在我們要修改lib1的檔案需要先切換到master分支:

➜ henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git checkout master
Switched to branch 'master'
➜ henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git:(master) echo "add by developer B" >> lib1-features
➜ henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git:(master) ✗ git commit -a -m "update lib1-features by developer B"
[master 36ad12d] update lib1-features by developer B
 1 files changed,0 deletions(-)

在主專案中修改Submodule提交到倉庫稍微繁瑣一點,在git push之前我們先看看project1-b狀態:

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git status
# On branch 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)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
</file></file>

libs/lib1 (new commits)狀態表示libs/lib1有新的提交,這個比較特殊,看看project1-b的狀態:

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git diff
diff --git a/libs/lib1 b/libs/lib1
index c22aff8..36ad12d 160000
--- a/libs/lib1
+++ b/libs/lib1
@@ -1 +1 @@
-Subproject commit c22aff85be91eca442734dcb07115ffe526b13a1
+Subproject commit 36ad12d40d8a41a4a88a64add27bd57cf56c9de2

從狀態中可以看出libs/lib1的commit id由原來的c22aff85be91eca442734dcb07115ffe526b13a1更改為36ad12d40d8a41a4a88a64add27bd57cf56c9de2

注意:如果現在執行了git submodule update操作那麼libs/lib1的commit id又會還原到c22aff85be91eca442734dcb07115ffe526b13a1,

這樣的話剛剛的修改是不是就丟死了呢?不會,因為修改已經提交到了master分支,只要再git checkout master就可以了。
現在可以把libs/lib1的修改提交到倉庫了:

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ cd libs/lib1
➜ henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git:(master) git push
Counting objects: 5,300 bytes,done.
To /home/henryyan/submd/repos/lib1.git
 c22aff8..36ad12d master -> master

現在僅僅只完成了一步,下一步要提交project1-b引用submodule的commit id:

➜ henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git:(master) cd ~/submd/ws/project1-b
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git add -u
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git commit -m "update libs/lib1 to lastest commit id"
[master c96838a] update libs/lib1 to lastest commit id
 1 files changed,1 deletions(-)
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) git push
Counting objects: 5,done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3),395 bytes,done.
To /home/henryyan/submd/ws/../repos/project1.git
 7157977..c96838a master -> master

OK,大功高成,我們完成了Submodule的修改並把libs/lib1的最新commit id提交到了倉庫。

接下來要看看project1怎麼獲取submodule了。

2.4 更新主專案的Submodules

好的,讓我們先進入project1目錄同步倉庫:

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) cd ../project1
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) git pull
remote: Counting objects: 5,done.
remote: Compressing objects: 100% (3/3),done.
remote: Total 3 (delta 0),done.
From /home/henryyan/submd/ws/../repos/project1
 7157977..c96838a master  -> origin/master
Updating 7157977..c96838a
Fast-forward
 libs/lib1 | 2 +-
 1 files changed,1 deletions(-)
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git status
# On branch 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)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
</file></file>

我們運行了git pull命令和git status獲取了最新的倉庫原始碼,然後看到了狀態時modified,這是為什麼呢?

我們用git diff比較一下不同:

➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git diff
diff --git a/libs/lib1 b/libs/lib1
index 36ad12d..c22aff8 160000
--- a/libs/lib1
+++ b/libs/lib1
@@ -1 +1 @@
-Subproject commit 36ad12d40d8a41a4a88a64add27bd57cf56c9de2
+Subproject commit c22aff85be91eca442734dcb07115ffe526b13a1
從diff的結果分析出來時因為submodule的commit id更改了,我們前面剛剛講了要在主專案更新submodule的內容首先要提交submdoule的內容,然後再更新主專案中引用的submodulecommit id;現在我們看到的不同就是因為剛剛更改了project1-b的submodule commit id;好的,我來學習一下怎麼更新project1的公共類庫。

follow me……
 
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git submodule update
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git status
# On branch 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)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
</file></file>

泥馬,為什麼沒有更新?git submodule update命令不是更新子模組倉庫的嗎?

別急,先聽我解釋;因為子模組是在project1中引入的,git submodule add ~/submd/repos/lib1.git libs/lib1命令的結果,操作之後git只是把lib1的內容clone到了project1中,但是沒有在倉庫註冊,證據如下:

➜ henryyan@hy-hp ~/submd2/ws/project1 git:(master) ✗ cat .git/config
[core]
 repositoryformatversion = 0
 filemode = true
 bare = false
 logallrefupdates = true
[remote "origin"]
 fetch = +refs/heads/*:refs/remotes/origin/*
 url = /home/henryyan/submd/ws/../repos/project1.git
[branch "master"]
 remote = origin
 merge = refs/heads/master

我們說過git submodule init就是在.git/config中註冊子模組的資訊,下面我們試試註冊之後再更新子模組:

➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git submodule init
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git submodule update
remote: Counting objects: 5,done.
From /home/henryyan/submd/repos/lib1
 c22aff8..36ad12d master  -> origin/master
Submodule path 'libs/lib1': checked out '36ad12d40d8a41a4a88a64add27bd57cf56c9de2'
 
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) cat .git/config
[core]
 repositoryformatversion = 0
 filemode = true
 bare = false
 logallrefupdates = true
[remote "origin"]
 fetch = +refs/heads/*:refs/remotes/origin/*
 url = /home/henryyan/submd/ws/../repos/project1.git
[branch "master"]
 remote = origin
 merge = refs/heads/master
[submodule "libs/lib1"]
 url = /home/henryyan/submd/repos/lib1.git
[submodule "libs/lib2"]
 url = /home/henryyan/submd/repos/lib2.git
 
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) cat libs/lib1/lib1-features
I'm lib1.
add by developer B

上面的結果足以證明剛剛的推斷,所以記得當需要更新子模組的內容時請先確保已經執行過git submodule init。

2.5 為project2新增lib1和lib2

這個操作對於讀到這裡的你來說應該是輕車熟路了,action:

➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) cd ~/submd/ws/project2
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) git submodule add ~/submd/repos/lib1.git libs/lib1
Cloning into libs/lib1...
done.
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git submodule add ~/submd/repos/lib2.git libs/lib2
zsh: correct 'libs/lib2' to 'libs/lib1' [nyae]? n
Cloning into libs/lib2...
done.
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ ls
libs project-infos.txt
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git submodule init
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'
 
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: .gitmodules
# new file: libs/lib1
# new file: libs/lib2
#
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git commit -a -m "add lib1 and lib2"
[master 8dc697f] add lib1 and lib2
 3 files changed,0 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 libs/lib1
 create mode 160000 libs/lib2
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) git push
Counting objects: 5,471 bytes,done.
To /home/henryyan/submd/ws/../repos/project2.git
 6e15c68..8dc697f master -> master
 
</file>

我們依次執行了新增submodule並commit和push到倉庫,此階段任務完成。

2.6 修改lib1和lib2並同步到project1和project2

假如開發人員C同時負責project1和project2,有可能在修改project1的某個功能的時候發現lib1或者lib2的某個元件有bug需要修復,這個需求多模組和大型系統中經常遇到,我們應該怎麼解決呢?

假如我的需求如下:

在lib1中新增一個檔案:README,用來描述lib1的功能

在lib2中的lib2-features檔案中新增一寫文字:學習Git submodule的修改並同步功能

2.6.1 在lib1中新增一個檔案:README

➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) cd libs/lib1
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib1 git:(master) echo "lib1 readme contents" > README
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib1 git:(master) ✗ git add README 
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib1 git:(master) ✗ git commit -m "add file README"
[master 8c666d8] add file README
 1 files changed,0 deletions(-)
 create mode 100644 README
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib1 git:(master) git push
Counting objects: 4,done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2),310 bytes,done.
To /home/henryyan/submd/repos/lib1.git
 36ad12d..8c666d8 master -> master

前面提到過現在僅僅只完成了一部分,我們需要在project2中再更新lib1的commit id:

➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git status 
# On branch 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)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git add libs/lib1
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git commit -m "update lib1 to lastest commit id"
[master ce1f3ba] update lib1 to lastest commit id
 1 files changed,1 deletions(-)
</file></file>

我們暫時不push到倉庫,等待和lib2的修改一起push。

2.6.2 在lib2中的lib2-features檔案新增文字

➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) cd libs/lib2
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) echo "學習Git submodule的修改並同步功能" >> lib2-features 
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) ✗ git add lib2-features 
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) ✗ git commit -m "新增文字:學習Git submodule的修改並同步功能"
[master e372b21] 新增文字:學習Git submodule的修改並同步功能
 1 files changed,0 deletions(-)
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) git push
Counting objects: 5,376 bytes,done.
To /home/henryyan/submd/repos/lib2.git
 7290dce..e372b21 master -> master
 
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) echo "學習Git submodule的修改並同步功能" >> lib2-features 
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) ✗ git add lib2-features 
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) ✗ git commit -m "新增文字:學習Git submodule的修改並同步功能"
[master e372b21] 新增文字:學習Git submodule的修改並同步功能
 1 files changed,done.
To /home/henryyan/submd/repos/lib2.git
 7290dce..e372b21 master -> master
➜ henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) cd -
~/submd/ws/project2
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git status 
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# 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)
#
# modified: libs/lib2 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git add libs/lib2 
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) ✗ git commit -m "update lib2 to lastest commit id"
[master df344c5] update lib2 to lastest commit id
 1 files changed,1 deletions(-)
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) git status 
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
nothing to commit (working directory clean)
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) git push
Counting objects: 8,done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6),done.
Writing objects: 100% (6/6),776 bytes,done.
Total 6 (delta 0),reused 0 (delta 0)
Unpacking objects: 100% (6/6),done.
To /home/henryyan/submd/ws/../repos/project2.git
 8dc697f..df344c5 master -> master
</file></file>

2.7 同步project2的lib1和lib2的修改到project1

現在project2已經享受到了最新的程式碼帶來的快樂,那麼既然project1和project2屬於同一個風格,或者呼叫同一個功能,要讓這兩個(可能幾十個)專案保持一致。

➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) cd ../project1
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) git pull
Already up-to-date.

看看上面的結果對嗎?為什麼lib1和lib2更新了但是沒有顯示new commits呢?說到這裡我記得剛剛開始學習的時候真得要暈死了,Git跟我玩捉迷藏遊戲,為什麼我明明提交了但是從project1更新不到任何改動呢?

幫大家分析一下問題,不過在分析之前先看看當前(project1和project2)的submodule狀態:

# project2 的狀態,也就是我們剛剛修改後的狀態
➜ henryyan@hy-hp ~/submd/ws/project2 git:(master) git submodule 
 8c666d86531513dd1aebdf235f142adbac72c035 libs/lib1 (heads/master)
 e372b21dffa611802c282278ec916b5418acebc2 libs/lib2 (heads/master)
 
# project1 的狀態,等待更新submodules
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) git submodule 
 36ad12d40d8a41a4a88a64add27bd57cf56c9de2 libs/lib1 (remotes/origin/HEAD)
 7290dce0062bd77df1d83b27dd3fa3f25a836b54 libs/lib2 (heads/master)

兩個專案有兩個區別:

  • commit id各不相同
  • libs/lib1所處的分支不同

2.7.1 更新project1的lib1和lib2改動

我們還記得剛剛在project2中修改的時候把lib1和lib2都切換到了master分支,目前project1中的lib1不在任何分支,我們先切換到master分支:

➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) cd libs/lib1
➜ henryyan@hy-hp ~/submd/ws/project1/libs/lib1 git checkout master
Previous HEAD position was 36ad12d... update lib1-features by developer B
Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit,and can be fast-forwarded.
➜ henryyan@hy-hp ~/submd/ws/project1/libs/lib1 git:(master) git pull
remote: Counting objects: 4,done.
remote: Compressing objects: 100% (2/2),done.
From /home/henryyan/submd/repos/lib1
 36ad12d..8c666d8 master  -> origin/master
Updating c22aff8..8c666d8
Fast-forward
 README  | 1 +
 lib1-features | 1 +
 2 files changed,2 insertions(+),0 deletions(-)
 create mode 100644 README
➜ henryyan@hy-hp ~/submd/ws/project1/libs/lib1 git:(master) 

果不其然,我們看到了剛剛在project2中修改的內容,同步到了project1中,當然現在更新了project1的lib1,commit id也會隨之變動:

➜ henryyan@hy-hp ~/submd/ws/project1/libs/lib1 git:(master) cd ../../
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git status 
# On branch 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)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git diff
diff --git a/libs/lib1 b/libs/lib1
index 36ad12d..8c666d8 160000
--- a/libs/lib1
+++ b/libs/lib1
@@ -1 +1 @@
-Subproject commit 36ad12d40d8a41a4a88a64add27bd57cf56c9de2
+Subproject commit 8c666d86531513dd1aebdf235f142adbac72c035
</file></file>

現在最新的commit id和project2目前的狀態一致,說明真的同步了;好的,現在可以使用相同的辦法更新lib2了:

➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ cd libs/lib2
➜ henryyan@hy-hp ~/submd/ws/project1/libs/lib2 git:(master) git pull
remote: Counting objects: 5,done.
From /home/henryyan/submd/repos/lib2
 7290dce..e372b21 master  -> origin/master
Updating 7290dce..e372b21
Fast-forward
 lib2-features | 1 +
 1 files changed,0 deletions(-)

2.7.2 更新project1的submodule引用

在2.7.1中我們更新了project1的lib1和lib2的最新版本,現在要把最新的commit id儲存到project1中以保持最新的引用。

➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git status
# On branch 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)
#
# modified: libs/lib1 (new commits)
# modified: libs/lib2 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) ✗ git commit -a -m "update lib1 and lib2 commit id to new version"
[master 8fcca50] update lib1 and lib2 commit id to new version
 2 files changed,2 deletions(-)
➜ henryyan@hy-hp ~/submd/ws/project1 git:(master) git push
Counting objects: 5,397 bytes,done.
To /home/henryyan/submd/ws/../repos/project1.git
 c96838a..8fcca50 master -> master
</file></file>

2.8 更新project1-b專案的子模組(使用指令碼)

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) git pull
remote: Counting objects: 5,done.
From /home/henryyan/submd/ws/../repos/project1
 c96838a..8fcca50 master  -> origin/master
Updating c96838a..8fcca50
Fast-forward
 libs/lib1 | 2 +-
 libs/lib2 | 2 +-
 2 files changed,2 deletions(-)
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git status
# On branch 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)
#
# modified: libs/lib1 (new commits)
# modified: libs/lib2 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
</file></file>

Git提示lib1和lib2有更新內容,這個判斷的依據來源於submodule commit id的引用。

現在怎麼更新呢?難道還是像project1中那樣進入子模組的目錄然後git checkout master,接著git pull?

而且現在僅僅才兩個子模組、兩個專案,如果在真實的專案中使用的話可能幾個到幾十個不等,再加上N個submodule,自己算一下要怎麼更新多少個submodules?

例如筆者現在做的一個專案有5個web模組,每個web模組引用公共的css、js、images、jsp資源,這樣就有20個submodule需要更新!!!

工欲善其事,必先利其器,寫一個指令碼代替手動任務。

2.8.1 牛刀小試

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ grep path .gitmodules | awk '{ print $3 }' > /tmp/study-git-submodule-dirs
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ cat /tmp/study-git-submodule-dirs
 libs/lib1
 libs/lib2

我們通過分析.gitmodules檔案得出子模組的路徑,然後就可以根據這些路徑進行更新。

2.8.2 上路

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ mkdir bin
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ vi bin/update-submodules.sh

把下面的指令碼複製到bin/update-submodules.sh中:

#!/bin/bash
grep path .gitmodules | awk '{ print $3 }' > /tmp/study-git-submodule-dirs
 
# read
while read LINE
do
 echo $LINE
 (cd ./$LINE && git checkout master && git pull)
done < /tmp/study-git-submodule-dirs

稍微解釋一下上面的指令碼執行過程:

  • 首先把子模組的路徑寫入到檔案/tmp/study-git-submodule-dirs中;
  • 然後讀取檔案中的子模組路徑,依次切換到master分支(修改都是在master分支上進行的),最後更新最近改動。

2.8.3 2013-01-19更新

網友@紫煌給出了更好的辦法,一個命令就可以代替上面的bin/update-submodules.sh的功能:

git submodule foreach git pull

此命令也指令碼一樣,迴圈進入(enter)每個子模組的目錄,然後執行foreach後面的命令。

該後面的命令可以任意的,例如 git submodule foreach ls -l 可以列出每個子模組的檔案列表

2.8.3 體驗工具帶來的便利

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git submodule 
+36ad12d40d8a41a4a88a64add27bd57cf56c9de2 libs/lib1 (heads/master)
+7290dce0062bd77df1d83b27dd3fa3f25a836b54 libs/lib2 (heads/master)
 
# 新增執行許可權
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ chmod +x ./bin/update-submodules.sh
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ ./bin/update-submodules.sh 
libs/lib1
Already on 'master'
remote: Counting objects: 4,done.
From /home/henryyan/submd/repos/lib1
  36ad12d..8c666d8 master   -> origin/master
Updating 36ad12d..8c666d8
Fast-forward
 README |  1 +
 1 files changed,0 deletions(-)
 create mode 100644 README
libs/lib2
Switched to branch 'master'
remote: Counting objects: 5,done.
From /home/henryyan/submd/repos/lib2
  7290dce..e372b21 master   -> origin/master
Updating 7290dce..e372b21
Fast-forward
 lib2-features |  1 +
 1 files changed,0 deletions(-)
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git submodule 
 8c666d86531513dd1aebdf235f142adbac72c035 libs/lib1 (heads/master)
 e372b21dffa611802c282278ec916b5418acebc2 libs/lib2 (heads/master)
 
 ➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git status 
# On branch master
# Untracked files:
#  (use "git add <file>..." to include in what will be committed)
#
#  bin/
nothing added to commit but untracked files present (use "git add" to track)
</file>

更新之後的兩個變化:

  • git submodule的結果和project2的submodule commit id保持一致;
  • project1-b不再提示new commits了。

現在可以把工具新增到倉庫了,當然你可以很驕傲的分享給其他專案組的同事。

➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git add bin/update-submodules.sh 
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) ✗ git commit -m "新增自動更新submodule的快捷指令碼^_^"
[master 756e788] 新增自動更新submodule的快捷指令碼^_^
 1 files changed,9 insertions(+),0 deletions(-)
 create mode 100755 bin/update-submodules.sh
➜ henryyan@hy-hp ~/submd/ws/project1-b git:(master) git push
Counting objects: 5,625 bytes,done.
To /home/henryyan/submd/ws/../repos/project1.git
  8fcca50..756e788 master -> master

2.9 新進員工加入團隊,一次性Clone專案和Submodules

一般人使用的時候都是使用如下命令:

git clone /path/to/repos/foo.git
git submodule init
git submodule update

新員工不耐煩了,嘴上不說但是心裡想:怎麼那麼麻煩?

上面的命令簡直弱暴了,直接一行命令搞定:

git clone --recursive /path/to/repos/foo.git

–recursive引數的含義:可以在clone專案時同時clone關聯的submodules。

git help 對其解釋:

--recursive,--recurse-submodules
After the clone is created,initialize all submodules within,using their default settings. This is equivalent to running git
submodule update --init --recursive immediately after the clone is finished. This option is ignored if the cloned repository
does not have a worktree/checkout (i.e. if any of --no-checkout/-n,--bare,or --mirror is given)

2.9.1 使用一鍵方式克隆project2

➜ henryyan@hy-hp ~/submd/ws git clone --recursive ../repos/project2.git project2-auto-clone-submodules
Cloning into project2-auto-clone-submodules...
done.
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'
Cloning into libs/lib1...
done.
Submodule path 'libs/lib1': checked out '8c666d86531513dd1aebdf235f142adbac72c035'
Cloning into libs/lib2...
done.
Submodule path 'libs/lib2': checked out 'e372b21dffa611802c282278ec916b5418acebc2'
舒服……

3.移除Submodule

牢騷:搞不明白為什麼git不設計一個類似:git submodule remove的命令呢?

我們從project1.git克隆一個專案用來練習移除submodule:

➜ henryyan@hy-hp ~/submd/ws git clone --recursive ../repos/project1.git project1-remove-submodules
Cloning into project1-remove-submodules...
done.
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'
Cloning into libs/lib1...
done.
Submodule path 'libs/lib1': checked out '8c666d86531513dd1aebdf235f142adbac72c035'
Cloning into libs/lib2...
done.
Submodule path 'libs/lib2': checked out 'e372b21dffa611802c282278ec916b5418acebc2'
➜ henryyan@hy-hp ~/submd/ws cd !$
➜ henryyan@hy-hp ~/submd/ws cd project1-remove-submodules

3.1 Step by

1、刪除git cache和物理資料夾

➜ henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) git rm -r --cached libs/
rm 'libs/lib1'
rm 'libs/lib2'
➜ henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ✗ rm -rf libs

2、刪除.gitmodules的內容(或者整個檔案) 因為本例只有兩個子模組,直接刪除檔案:

➜ henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ✗ rm .gitmodules

如果僅僅刪除某一個submodule那麼開啟.gitmodules檔案編輯,刪除對應submodule配置即可。

3、刪除.git/config的submodule配置 原始檔:

[core]
  repositoryformatversion = 0 
  filemode = true
  bare = false
  logallrefupdates = true
[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = /home/henryyan/submd/ws/../repos/project1.git
[branch "master"]
  remote = origin
  merge = refs/heads/master
[submodule "libs/lib1"]
  url = /home/henryyan/submd/repos/lib1.git
[submodule "libs/lib2"]
  url = /home/henryyan/submd/repos/lib2.git

刪除後:

[core]
  repositoryformatversion = 0 
  filemode = true
  bare = false
  logallrefupdates = true
[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = /home/henryyan/submd/ws/../repos/project1.git
[branch "master"]
  remote = origin
  merge = refs/heads/master

4、提交更改

➜ henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ✗ git status
# On branch master
# Changes to be committed:
#  (use "git reset HEAD <file>..." to unstage)
#
#  deleted:  libs/lib1
#  deleted:  libs/lib2
#
# Changes not staged for commit:
#  (use "git add/rm <file>..." to update what will be committed)
#  (use "git checkout -- <file>..." to discard changes in working directory)
#
#  deleted:  .gitmodules
#
➜ henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ✗ git add .gitmodules
➜ henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ✗ git commit -m "刪除子模組lib1和lib2"
[master 5e2ee71] 刪除子模組lib1和lib2
 3 files changed,0 insertions(+),8 deletions(-)
 delete mode 100644 .gitmodules
 delete mode 160000 libs/lib1
 delete mode 160000 libs/lib2
➜ henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) git push
Counting objects: 3,done.
Writing objects: 100% (2/2),302 bytes,done.
Total 2 (delta 0),reused 0 (delta 0)
Unpacking objects: 100% (2/2),done.
To /home/henryyan/submd/ws/../repos/project1.git
  756e788..5e2ee71 master -> master
</file></file></file>

4.結束語

對於Git Submodule來說在剛剛接觸的時候多少會有點凌亂的趕緊,尤其是沒有接觸過svn:externals。

本文從開始建立專案到使用git submodule的每個步驟以及細節、原理逐一講解,希望到此讀者能駕馭它。

學會了Git submdoule你的專案中再也不會出現大量重複的資原始檔、公共類庫,更不會出現多個版本,甚至一個客戶的多個專案風格存在各種差異。

來源: <http://www.kafeitu.me/git/2012/03/27/git-submodule.html>

拉取所有子模組

git submodule foreach git pull
git submodule foreach --recursive git submodule init 
git submodule foreach --recursive git submodule update 

到此這篇關於Git Submodule使用完整教程(小結)的文章就介紹到這了,更多相關Git Submodule使用內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!