自從看了蔣鑫的《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 准備環境
1
2
3
|
➜ henryyan@hy-hp ~
pwd
/home/henryyan
mkdir
-p submd
/repos
|
創建需要的本地倉庫:
1
2
3
4
5
|
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
|
初始化工作區:
1
2
|
mkdir
~
/submd/ws
cd
~
/submd/ws
|
2.1.2 初始化項目
初始化project1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
➜ 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
➜ 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, 1 insertions(+), 0 deletions(-)
create mode 100644 project-infos.txt
➜ henryyan@hy-hp ~
/submd/ws/project2
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/project2
.git
* [new branch] master -> master
<
/file
>
|
2.1.3 初始化公共類庫
初始化公共類庫lib1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
➜ 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, 1 insertions(+), 0 deletions(-)
create mode 100644 lib1-features
➜ henryyan@hy-hp ~
/submd/ws/lib1
git:(master) git push origin master
Counting objects: 3,
done
.
Writing objects: 100% (3
/3
), 227 bytes,
done
.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
done
.
To
/home/henryyan/submd/ws/
..
/repos/lib1
.git
* [new branch] master -> master
|
初始化公共類庫lib2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
➜ 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, 1 insertions(+), 0 deletions(-)
create mode 100644 lib2-features
➜ henryyan@hy-hp ~
/submd/ws/lib2
git:(master) git push origin master
Counting objects: 3,
done
.
Writing objects: 100% (3
/3
), 227 bytes,
done
.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
done
.
To
/home/henryyan/submd/ws/
..
/repos/lib2
.git
* [new branch] master -> master
|
2.2 為主項目添加Submodules
2.2.1 為project1添加lib1和lib2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
➜ 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文件是做什么用的呢?我們查看一下文件內容便知曉了:
1
2
3
4
5
6
7
|
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的引用信息,知道在當前項目的位置以及倉庫的所在。
好的,我們現在把更改提交到倉庫。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
➜ 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……
1
2
3
4
5
6
7
8
|
➜ 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……
1
2
3
4
5
6
7
8
9
10
|
➜ 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的注冊信息!
驗證一下類庫的文件是否存在:
1
2
3
|
➜ 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的狀態:
1
2
3
4
|
➜ 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的引用信息:
1
2
3
4
|
➜ 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分支:
1
2
3
4
5
6
|
➜ 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, 1 insertions(+), 0 deletions(-)
|
在主項目中修改Submodule提交到倉庫稍微繁瑣一點,在git push之前我們先看看project1-b狀態:
1
2
3
4
5
6
7
8
9
10
|
➜ 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的狀態:
1
2
3
4
5
6
7
8
|
➜ 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的修改提交到倉庫了:
1
2
3
4
5
6
7
8
|
➜ 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,
done
.
Writing objects: 100% (3
/3
), 300 bytes,
done
.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
done
.
To
/home/henryyan/submd/repos/lib1
.git
c22aff8..36ad12d master -> master
|
現在僅僅只完成了一步,下一步要提交project1-b引用submodule的commit id:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
➜ 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 insertions(+), 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
),
done
.
Writing objects: 100% (3
/3
), 395 bytes,
done
.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
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目錄同步倉庫:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
➜ 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), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
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 insertions(+), 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比較一下不同:
1
2
3
4
5
6
7
8
|
➜ 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……
1
2
3
4
5
6
7
8
9
10
11
|
➜ 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中,但是沒有在倉庫注冊,證據如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
➜ 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中注冊子模塊的信息,下面我們試試注冊之后再更新子模塊:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
➜ 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
.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
➜ 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, 8 insertions(+), 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,
done
.
Delta compression using up to 2 threads.
Compressing objects: 100% (4
/4
),
done
.
Writing objects: 100% (4
/4
), 471 bytes,
done
.
Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4
/4
),
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
➜ 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, 1 insertions(+), 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
),
done
.
Writing objects: 100% (3
/3
), 310 bytes,
done
.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
done
.
To
/home/henryyan/submd/repos/lib1
.git
36ad12d..8c666d8 master -> master
|
前面提到過現在僅僅只完成了一部分,我們需要在project2中再更新lib1的commit id:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
➜ 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 insertions(+), 1 deletions(-)
<
/file
><
/file
>
|
我們暫時不push到倉庫,等待和lib2的修改一起push。
2.6.2 在lib2中的lib2-features文件添加文字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
➜ 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, 1 insertions(+), 0 deletions(-)
➜ henryyan@hy-hp ~
/submd/ws/project2/libs/lib2
git:(master) git push
Counting objects: 5,
done
.
Delta compression using up to 2 threads.
Compressing objects: 100% (2
/2
),
done
.
Writing objects: 100% (3
/3
), 376 bytes,
done
.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
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, 1 insertions(+), 0 deletions(-)
➜ henryyan@hy-hp ~
/submd/ws/project2/libs/lib2
git:(master) git push
Counting objects: 5,
done
.
Delta compression using up to 2 threads.
Compressing objects: 100% (2
/2
),
done
.
Writing objects: 100% (3
/3
), 376 bytes,
done
.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
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 insertions(+), 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屬於同一個風格,或者調用同一個功能,要讓這兩個(可能幾十個)項目保持一致。
1
2
3
|
➜ 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狀態:
1
2
3
4
5
6
7
8
9
|
# 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分支:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
➜ 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
.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
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也會隨之變動:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
➜ 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了:
1
2
3
4
5
6
7
8
9
10
11
12
|
➜ 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
.
remote: Compressing objects: 100% (2
/2
),
done
.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
done
.
From
/home/henryyan/submd/repos/lib2
7290dce..e372b21 master -> origin
/master
Updating 7290dce..e372b21
Fast-forward
lib2-features | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
|
2.7.2 更新project1的submodule引用
在2.7.1中我們更新了project1的lib1和lib2的最新版本,現在要把最新的commit id保存到project1中以保持最新的引用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
➜ 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 insertions(+), 2 deletions(-)
➜ henryyan@hy-hp ~
/submd/ws/project1
git:(master) git push
Counting objects: 5,
done
.
Delta compression using up to 2 threads.
Compressing objects: 100% (3
/3
),
done
.
Writing objects: 100% (3
/3
), 397 bytes,
done
.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
done
.
To
/home/henryyan/submd/ws/
..
/repos/project1
.git
c96838a..8fcca50 master -> master
<
/file
><
/file
>
|
2.8 更新project1-b項目的子模塊(使用腳本)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
➜ henryyan@hy-hp ~
/submd/ws/project1-b
git:(master) git pull
remote: Counting objects: 5,
done
.
remote: Compressing objects: 100% (3
/3
),
done
.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
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 insertions(+), 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 牛刀小試
1
2
3
4
|
➜ 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 上路
1
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中:
1
2
3
4
5
6
7
8
9
|
#!/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的功能:
1
|
git submodule foreach git pull
|
此命令也腳本一樣,循環進入(enter)每個子模塊的目錄,然后執行foreach后面的命令。
該后面的命令可以任意的,例如 git submodule foreach ls -l 可以列出每個子模塊的文件列表
2.8.3 體驗工具帶來的便利
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
➜ 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
.
remote: Compressing objects: 100% (2
/2
),
done
.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
done
.
From
/home/henryyan/submd/repos/lib1
36ad12d..8c666d8 master -> origin
/master
Updating 36ad12d..8c666d8
Fast-forward
README | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README
libs
/lib2
Switched to branch
'master'
remote: Counting objects: 5,
done
.
remote: Compressing objects: 100% (2
/2
),
done
.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3
/3
),
done
.
From
/home/henryyan/submd/repos/lib2
7290dce..e372b21 master -> origin
/master
Updating 7290dce..e372b21
Fast-forward
lib2-features | 1 +
1 files changed, 1 insertions(+), 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了。
現在可以把工具添加到倉庫了,當然你可以很驕傲的分享給其他項目組的同事。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
➜ 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,
done
.
Delta compression using up to 2 threads.
Compressing objects: 100% (3
/3
),
done
.
Writing objects: 100% (4
/4
), 625 bytes,
done
.
Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4
/4
),
done
.
To
/home/henryyan/submd/ws/
..
/repos/project1
.git
8fcca50..756e788 master -> master
|
2.9 新進員工加入團隊,一次性Clone項目和Submodules
一般人使用的時候都是使用如下命令:
1
2
3
|
git clone
/path/to/repos/foo
.git
git submodule init
git submodule update
|
新員工不耐煩了,嘴上不說但是心里想:怎么那么麻煩?
上面的命令簡直弱暴了,直接一行命令搞定:
1
|
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
1
2
3
4
5
6
7
8
9
10
11
|
➜ 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
➜ 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和物理文件夾
1
2
3
4
|
➜ 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的內容(或者整個文件) 因為本例只有兩個子模塊,直接刪除文件:
1
|
➜ 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、提交更改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
➜ 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
.
Delta compression using up to 2 threads.
Compressing objects: 100% (2
/2
),
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你的項目中再也不會出現大量重復的資源文件、公共類庫,更不會出現多個版本,甚至一個客戶的多個項目風格存在各種差異。
本文的寫作來源於工作中的實踐,如果您對於某個做法有更好的辦法還請賜教,希望能留下您寶貴的意見。
原創文章,轉載請注明出處! Git Submoudle使用完整教程--咖啡兔