Git Submodule使用完整教程

释放双眼,带上耳机,听听看~!

Git Submodule使用完整教程

自从看了蒋鑫的《Git权威指南》之后就开始使用Git Submodule功能,团队也都熟悉了怎么使用,多个子系统(模块)都能及时更新到最新的公共资源,把使用的过程以及经验和容易遇到的问题分享给大家。


1
2
3
1Git Submodule功能刚刚开始学习可能觉得有点怪异,所以本教程把每一步的操作的命令和结果都用代码的形式展现给大家,以便更好的理解。
2
3

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
4
1➜ henryyan@hy-hp~pwd
2/home/henryyan
3mkdir -p submd/repos
4

创建需要的本地仓库:


1
2
3
4
5
6
1cd ~ /submd/repos
2git --git-dir=lib1.git init --bare
3git --git-dir=lib2.git init --bare
4git --git-dir=project1.git init --bare
5git --git-dir=project2.git init --bare
6

初始化工作区:


1
2
3
1mkdir ~/submd/ws
2cd ~/submd/ws
3

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1➜ henryyan@hy-hp~ /submd/ws 
2git clone ../repos/project1.git  
3Cloning into project1...
4done.
5warning: You appear to have cloned an empty repository.
6➜ henryyan@hy-hp~/submd/ws  cd  project1
7➜ henryyan@hy-hp~/submd/ws/project1 git:(master)  
8echo "project1" > project-infos.txt
9➜ henryyan@hy-hp ~ /submd/ws/project1 git:(master) ✗  
10ls
11project-infos.txt
12        
13➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
14git add project-infos.txt  
15        
16➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
17git status
18        
19# On branch master
20#
21# Initial commit
22#
23# Changes to be committed:
24#   (use "git rm --cached <file>..." to unstage)
25#
26#   new file:   project-infos.txt
27#
28        
29➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
30git commit -m "init project1"
31[master (root-commit) 473a2e2] init project1
32
331 files changed, 1 insertions(+), 0 deletions(-)
34create mode 100644 project-infos.txt
35        
36➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)
37
38git push origin master
39        
40Counting objects: 3,  
41done.
42        
43Writing objects: 100% (3/3 ), 232 bytes,  
44done.
45Total 3 (delta 0), reused 0 (delta 0)
46Unpacking objects: 100% (3/3),  
47done.
48        
49To /home/henryyan/submd/ws/ ../repos/project1.git
50        
51* [new branch]      master -> master
52        
53</file>
54

初始化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
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
1➜ henryyan@hy-hp  ~ /submd/ws/project1
2cd ..
3        
4➜ henryyan@hy-hp  ~ /submd/ws 
5git clone .. /repos/project2.git  
6Cloning into project2...
7done.
8        
9warning: You appear to have cloned an empty repository.
10        
11➜ henryyan@hy-hp  ~ /submd/ws 
12cd project2
13        
14➜ henryyan@hy-hp ~ /submd/ws/project2 git:(master)  
15echo "project2" > project-infos.txt
16        
17➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗  
18ls project-infos.txt
19          
20➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
21git add project-infos.txt  
22        
23➜ henryyan@hy-hp  ~  /submd/ws/project2 git:(master) ✗
24git status
25        
26# On branch master
27#
28# Initial commit
29#
30# Changes to be committed:
31#   (use "git rm --cached <file>..." to unstage)
32#
33#   new file:   project-infos.txt
34#
35        
36➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
37git commit -m  "init project2"
38        
39[master (root-commit) 473a2e2] init project2
401 files changed, 1 insertions(+), 0 deletions(-)
41create mode 100644 project-infos.txt
42        
43➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master)
44
45git push origin master
46        
47Counting objects: 3,  
48done.
49        
50Writing objects: 100% (3/3 ), 232 bytes,  
51done.
52Total 3 (delta 0), reused 0 (delta 0)
53Unpacking objects: 100% (3/3),  
54done.
55        
56To /home/henryyan/submd/ws/
57../repos/project2.git
58
59* [new branch]      master -> master
60</file>
61
62

2.1.3 初始化公共类库

初始化公共类库lib1:


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
1➜ henryyan@hy-hp  ~ /submd/ws 
2git clone ../repos/lib1.git  
3        
4Cloning into lib1...    
5done.
6        
7warning: You appear to have cloned an empty repository.
8        
9➜ henryyan@hy-hp  ~/submd/ws 
10cd lib1  
11        
12➜ henryyan@hy-hp  ~ /submd/ws/lib1 git:(master)  
13echo "I'm lib1." > lib1-features
14        
15➜ henryyan@hy-hp  ~ /submd/ws/lib1  git:(master) ✗
16git add lib1-features  
17        
18 ➜ henryyan@hy-hp  ~ /submd/ws/lib1 git:(master) ✗
19git commit -m  "init lib1"
20        
21[master (root-commit) c22aff8] init lib1
22
231 files changed, 1 insertions(+), 0 deletions(-)
24
25create mode 100644 lib1-features
26        
27➜ henryyan@hy-hp  ~ /submd/ws/lib1 git:(master)
28git push origin master
29        
30Counting objects: 3,  
31done .
32        
33Writing objects: 100% (3/3 ), 227 bytes,  
34done.
35        
36Total 3 (delta 0), reused 0 (delta 0)
37Unpacking objects: 100% (3/3 ),  
38done .
39        
40To /home/henryyan/submd/ws/
41.. /repos/lib1.git
42        
43* [new branch]      master -> master
44

初始化公共类库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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
1➜ henryyan@hy-hp  ~ /submd/ws/lib1 git:(master)  
2cd ..
3
4➜ henryyan@hy-hp  ~ /submd/ws 
5 git clone .. /repos/lib2.git  
6        
7Cloning into lib2...
8done.
9        
10warning: You appear to have cloned an empty repository.
11        
12➜ henryyan@hy-hp  ~  /submd/ws 
13cd ib2  
14        
15➜ henryyan@hy-hp  ~ /submd/ws/lib2 git:(master)  
16echo "I'm lib2." > lib2-features
17        
18➜ henryyan@hy-hp  ~ /submd/ws/lib2 git:(master) ✗
19git add lib2-features  
20        
21➜ henryyan@hy-hp  ~ /submd/ws/lib2 git:(master) ✗
22git commit -m "init lib2"
23        
24[master (root-commit) c22aff8] init lib2
25        
261 files changed, 1 insertions(+), 0 deletions(-)
27create mode 100644 lib2-features
28        
29➜ henryyan@hy-hp  ~ /submd/ws/lib2 git:(master)
30git push origin master
31        
32Counting objects: 3,  
33done.
34        
35Writing objects: 100% (3/3), 227 bytes,  
36done.
37        
38Total 3 (delta 0), reused 0 (delta 0)
39Unpacking objects: 100% (3/3),  
40done.
41        
42To /home/henryyan/submd/ws/
43.. /repos/lib2.git
44        
45* [new branch]      master -> master
46

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1➜ henryyan@hy-hp  ~  /submd/ws/lib2 git:(master)  
2cd ../project1
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)  
5ls project-infos.txt
6        
7➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)
8git submodule add ~/submd/repos/lib1.git libs/lib1
9        
10Cloning into libs/lib1
11...          
12done.
13        
14➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
15git submodule add ~ /submd/repos/lib2.git libs/lib2
16        
17Cloning into libs/lib2
18...
19done.
20        
21➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗  
22ls libs  project-infos.txt
23        
24➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗  
25ls libs  
26        
27lib1  lib2
28        
29➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
30git status  
31        
32# On branch master
33# Changes to be committed:
34#   (use "git reset HEAD <file>..." to unstage)
35#
36#   new file:   .gitmodules
37#   new file:   libs/lib1
38#   new file:   libs/lib2
39#
40# 查看一下公共类库的内容
41        
42
43➜ henryyan@hy-hp  ~  /submd/ws/project1 git:(master)  
44cat libs/lib1/lib1-features
45        
46I'm lib1.
47        
48➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)  
49cat libs/lib2/lib2-features
50        
51I'm lib2.
52        
53</file>
54

好了,到目前为止我们已经使用git submodule add命令为project1成功添加了两个公共类库(lib1、lib2),查看了当前的状态发现添加了一个新文件(.gitmodules)和两个文件夹(libs/lib1、libs/lib2);那么新增的**.gitmodules**文件是做什么用的呢?我们查看一下文件内容便知晓了:


1
2
3
4
5
6
7
8
9
10
11
12
1n@hy-hp  ~ /submd/ws/project1 git:(master) ✗  
2cat.gitmodules  
3        
4[submodule "libs/lib1"]
5path = libs/lib1
6url = /home/henryyan/submd/repos/lib1.git
7        
8[submodule "libs/lib2"]
9path = libs/lib2
10url = /home/henryyan/submd/repos/lib2.git
11
12

原来如此,.gitmodules记录了每个submodule的引用信息,知道在当前项目的位置以及仓库的所在。

好的,我们现在把更改提交到仓库。


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
1➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
2git commit -a -m  "add submodules[lib1,lib2] to project1"
3        
4[master 7157977] add submodules[lib1,lib2] to project1
5        
63 files changed, 8 insertions(+), 0 deletions(-)
7        
8create mode 100644 .gitmodules
9create mode 160000 libs/lib1
10create mode 160000 libs/lib2
11        
12➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)
13git push
14        
15Counting objects: 5,  
16done.
17        
18Delta compression using up to 2 threads.
19Compressing objects: 100% (4/4),  
20done.
21        
22Writing objects: 100% (4/4), 491 bytes,  
23done.
24
25Total 4 (delta 0), reused 0 (delta 0)
26Unpacking objects: 100% (4/4),  
27done.
28        
29To /home/henryyan/submd/ws/
30.. /repos/project1.git
31        
3245cbbcb..7157977  master -> master
33

1
2
1假如你是第一次引入公共类库的开发人员,那么项目组的其他成员怎么Clone带有Submodule的项目呢,下面我们再clone一个项目讲解如何操作。
2

2.3 Clone带有Submodule的仓库

模拟开发人员B……


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)  
2cd ~/submd/ws
3        
4➜ henryyan@hy-hp  ~ /submd/ws 
5git clone ../repos/project1.git project1-b
6        
7Cloning into project1-b...
8done.
9        
10➜ henryyan@hy-hp  ~  /submd/ws 
11cd project1-b  
12        
13➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master)
14git submodule  
15        
16-c22aff85be91eca442734dcb07115ffe526b13a1 libs/lib1
17        
18-7290dce0062bd77df1d83b27dd3fa3f25a836b54 libs/lib2
19
20

看到submodules的状态是hash码和文件目录,但是注意前面有一个减号:,含义是该子模块还没有检出。

OK,检出project1-b的submodules……


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master)
2git submodule init
3        
4Submodule 'libs/lib1'(/home/henryyan/submd/repos/lib1.git) registered  
5for path 'libs/lib1'
6        
7Submodule 'libs/lib2'(/home/henryyan/submd/repos/lib2.git) registered  
8for path 'libs/lib2'
9        
10➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master)
11git submodule update
12        
13Cloning into libs /lib1
14...
15done.
16        
17Submodule path 'libs/lib1': checked out 'c22aff85be91eca442734dcb07115ffe526b13a1'
18        
19Cloning into libs/lib2
20...
21done.
22
23Submodule path 'libs/lib2': checked out '7290dce0062bd77df1d83b27dd3fa3f25a836b54'
24

1
2
1读者可以查看:.git/config文件的内容,最下面有submodule的注册信息!
2

验证一下类库的文件是否存在:


1
2
3
4
5
6
7
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master)  
2cat libs /lib1/lib1-features libs /lib2/lib2-features
3        
4I'm lib1.
5        
6'm lib2.
7

上面的两个命令(git submodule init & update)其实可以简化,后面会讲到!

2.3 修改Submodule

我们在开发人员B的项目上修改Submodule的内容。

先看一下当前Submodule的状态:


1
2
3
4
5
6
7
8
9
10
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master)  
2cd libs/lib1
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1-b/libs/lib1 
5git status
6        
7# Not currently on any branch.
8        
9nothing to commit (working directory clean)
10

为什么是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
5
6
7
8
9
10
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b/libs/lib1 
2cat .git/HEAD
3        
4c22aff85be91eca442734dcb07115ffe526b13a1
5        
6➜ henryyan@hy-hp  ~ /submd/ws/project1-b/libs/lib1 
7cat .git/refs/heads/master              
8        
9c22aff85be91eca442734dcb07115ffe526b13a1
10

现在我们要修改lib1的文件需要先切换到master分支:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b/libs/lib1 
2git checkout master
3        
4Switched to branch 'master'
5        
6➜ henryyan@hy-hp  ~ /submd/ws/project1-b/libs/lib1 git:(master)  
7echo "add by developer B" >> lib1-features
8        
9➜ henryyan@hy-hp  ~ /submd/ws/project1-b/libs/lib1 git:(master) ✗
10git commit -a -m "update lib1-features by developer B"
11        
12[master 36ad12d] update lib1-features by developer B
13        
141 files changed, 1 insertions(+), 0 deletions(-)
15

在主项目中修改Submodule提交到仓库稍微繁琐一点,在git push之前我们先看看project1-b状态:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
2git status
3        
4# On branch master
5# Changes not staged for commit:
6#   (use "git add <file>..." to update what will be committed)
7#   (use "git checkout -- <file>..." to discard changes in working directory)
8#
9#   modified:   libs/lib1 (new commits)
10#
11        
12no changes added to commit (use"git add" and/or "git commit -a" )
13        
14</file></file>
15

libs/lib1 (new commits)状态表示libs/lib1有新的提交,这个比较特殊,看看project1-b的状态:


1
2
3
4
5
6
7
8
9
10
11
12
13
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
2git diff
3        
4diff --git a /libs/lib1 b /libs/lib1
5index c22aff8..36ad12d 160000
6        
7--- a /libs/lib1        
8+++ b /libs/lib1
9@@ -1 +1 @@
10-Subproject commit c22aff85be91eca442734dcb07115ffe526b13a1
11+Subproject commit 36ad12d40d8a41a4a88a64add27bd57cf56c9de2
12
13

从状态中可以看出libs/lib1的commit id由原来的c22aff85be91eca442734dcb07115ffe526b13a1更改为36ad12d40d8a41a4a88a64add27bd57cf56c9de2


1
2
3
4
5
1注意:如果现在执行了git submodule update操作那么libs/lib1的commit id又会还原到c22aff85be91eca442734dcb07115ffe526b13a1,
2
3这样的话刚刚的修改是不是就丢死了呢?不会,因为修改已经提交到了master分支,只要再git checkout master就可以了。
4
5

现在可以把libs/lib1的修改提交到仓库了:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗  
2cd libs/lib1
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1-b/libs/lib1 git:(master)
5git push
6        
7Counting objects: 5,  
8done.
9        
10Writing objects: 100% (3/3), 300 bytes,  
11done.
12        
13Total 3 (delta 0), reused 0 (delta 0)
14Unpacking objects: 100% (3/3),  
15done.
16        
17To /home/henryyan/submd/repos/lib1.git
18        
19c22aff8..36ad12d  master -> master
20

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


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
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b/libs/lib1 git:(master)  
2cd ~/submd/ws/project1-b
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1-b  git:(master) ✗
5git add -u
6        
7➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
8git commit -m "update libs/lib1 to lastest commit id"
9        
10[master c96838a] update libs/lib1 to lastest commit id
11        
121 files changed, 1 insertions(+), 1 deletions(-)
13        
14➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master)
15git push
16        
17Counting objects: 5,  
18done.
19        
20Delta compression using up to 2 threads.
21        
22Compressing objects: 100% (3/3),  
23done.
24Writing objects: 100% (3/3), 395 bytes,  
25done.
26        
27Total 3 (delta 0), reused 0 (delta 0)
28Unpacking objects: 100% (3/3),  
29done.
30To  /home/henryyan/submd/ws/
31.. /repos/project1.git
32        
337157977..c96838a  master -> master
34

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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master)  
2cd ../project1
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)
5git pull
6        
7remote: Counting objects: 5,  
8done.
9remote: Compressing objects: 100% (3/3),  
10done.
11        
12remote: Total 3 (delta 0), reused 0 (delta 0)
13Unpacking objects: 100% (3/3),  
14done.
15        
16From  /home/henryyan/submd/ws/
17.. /repos/project1
18        
197157977..c96838a  master     -> origin/master
20Updating 7157977..c96838a    Fast-forward
21        
22libs/lib1 |    2 +-
23        
241 files changed, 1 insertions(+), 1 deletions(-)
25        
26➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
27git status
28        
29# On branch master
30# Changes not staged for commit:
31#   (use "git add <file>..." to update what will be committed)
32#   (use "git checkout -- <file>..." to discard changes in working directory)
33#
34#   modified:   libs/lib1 (new commits)
35#
36        
37 no changes added to commit (use "git add" and/or "git commit -a" )
38</file></file>
39

我们运行了git pull命令和git status获取了最新的仓库源码,然后看到了状态时modified,这是为什么呢?

我们用git diff比较一下不同:


1
2
3
4
5
6
7
8
9
10
1➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
2git diff
3diff --git a /libs/lib1 b /libs/lib1
4index 36ad12d..c22aff8 160000
5--- a /libs/lib1
6+++ b /libs/lib1
7@@ -1 +1 @@
8-Subproject commit 36ad12d40d8a41a4a88a64add27bd57cf56c9de2
9+Subproject commit c22aff85be91eca442734dcb07115ffe526b13a1
10

从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
12
13
14
15
16
17
1➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
2git submodule update
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
5git status
6        
7# On branch master
8# Changes not staged for commit:
9#   (use "git add <file>..." to update what will be committed)
10#   (use "git checkout -- <file>..." to discard changes in working directory)
11#
12#   modified:   libs/lib1 (new commits)
13#
14no changes added to commit (use "git add" and /or "git commit -a")
15        
16</file></file>
17

泥马,为什么没有更新?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
13
14
15
16
17
18
19
20
21
1➜ henryyan@hy-hp  ~ /submd2/ws/project1 git:(master) ✗  
2cat .git/config
3        
4[core]
5repositoryformatversion = 0
6filemode = true
7bare = false
8logallrefupdates = true
9        
10[remote "origin"]
11fetch = +refs
12        /heads/
13        *:refs
14        /remotes/origin/
15        *
16url = /home/henryyan/submd/ws/../repos/project1.git
17        
18[branch "master"]
19remote = origin
20merge = refs /heads/master
21

我们说过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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
2git submodule init
3        
4Submodule 'libs/lib1'(/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
5        
6Submodule 'libs/lib2'(/home/henryyan/submd/repos/lib2.git) registered for path   'libs/lib2'
7        
8➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
9git submodule update
10        
11remote: Counting objects: 5,  
12done.
13
14remote: Total 3 (delta 0), reused 0 (delta 0)
15Unpacking objects: 100% (3/3),  
16done.
17        
18From /home/henryyan/submd/repos/lib1
19c22aff8..36ad12d  master     -> origin/master
20        
21Submodule path 'libs/lib1' : checked out  '36ad12d40d8a41a4a88a64add27bd57cf56c9de2'
22        
23➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)  
24cat .git/config
25        
26[core]
27repositoryformatversion = 0
28filemode = true
29bare = false
30logallrefupdates = true
31        
32[remote "origin"]
33fetch = +refs /heads/ *:refs /remotes/origin/ *
34        
35url = /home/henryyan/submd/ws/../repos/project1.git
36        
37[branch "master"]
38remote = origin
39merge = refs /heads/master
40        
41[submodule "libs/lib1"]
42url = /home/henryyan/submd/repos/lib1.git
43        
44[submodule "libs/lib2"]
45url = /home/henryyan/submd/repos/lib2.git
46        
47➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)  
48cat libs/lib1/lib1-features
49        
50I'm lib1.
51        
52add by developer B
53
54

上面的结果足以证明刚刚的推断,所以记得当需要更新子模块的内容时请先确保已经运行过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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
1➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)  
2cd ~/submd/ws/project2
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master)
5git submodule add ~ /submd/repos/lib1.git libs/lib1
6        
7Cloning into libs/lib1
8...
9done.
10        
11➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
12git submodule add ~/submd/repos/lib2.git libs/lib2
13        
14zsh: correct'libs/lib2' to 'libs/lib1' [nyae]? n
15        
16Cloning into libs/lib2
17...
18done.
19        
20➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗  
21ls
22        
23libs  project-infos.txt
24        
25➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
26git submodule init
27        
28Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path  'libs/lib1'
29        
30Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path  'libs/lib2'
31        
32➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
33git status
34        
35# On branch master
36# Changes to be committed:
37#   (use "git reset HEAD <file>..." to unstage)
38#
39#   new file:   .gitmodules
40#   new file:   libs/lib1
41#   new file:   libs/lib2
42#
43        
44➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
45git commit -a -m  "add lib1 and lib2"
46[master 8dc697f] add lib1 and lib2
473 files changed, 8 insertions(+), 0 deletions(-)
48create mode 100644 .gitmodules
49create mode 160000 libs/lib1
50create mode 160000 libs/lib2
51
52➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master)
53git push
54        
55Counting objects: 5,  
56done.
57Delta compression using up to 2 threads.
58Compressing objects: 100% (4/4),  done.
59Writing objects: 100% (4/4), 471 bytes, done.
60Total 4 (delta 0), reused 0 (delta 0)
61Unpacking objects: 100% (4/4), done.
62        
63To /home/henryyan/submd/ws/../repos/project2.git
646e15c68..8dc697f  master -> master
65</file>
66

我们依次执行了添加submodule并commit和push到仓库,此阶段任务完成。

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

假如开发人员C同时负责project1project2,有可能在修改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
17
18
19
20
21
22
23
24
25
26
27
28
29
1➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master)  
2cd libs/lib1
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib1 git:(master)  
5echo "lib1 readme contents" > README
6        
7➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib1 git:(master) ✗
8git add README  
9        
10➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib1 git:(master) ✗
11git commit -m "add file README"
12[master 8c666d8] add  file  README
131 files changed, 1 insertions(+), 0 deletions(-)
14create mode 100644 README
15        
16➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib1 git:(master)
17git push
18        
19Counting objects: 4, done.
20        
21Delta compression using up to 2 threads.
22Compressing objects: 100% (2/2), done.
23Writing objects: 100% (3/3), 310 bytes, done.
24Total 3 (delta 0), reused 0 (delta 0)
25        
26Unpacking objects: 100% (3/3), done.
27To /home/henryyan/submd/repos/lib1.git
2836ad12d..8c666d8  master -> master
29

前面提到过现在仅仅只完成了一部分,我们需要在project2中再更新lib1的commit id:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
2
3git status  
4# On branch master
5# Changes not staged for commit:
6#   (use "git add <file>..." to update what will be committed)
7#   (use "git checkout -- <file>..." to discard changes in working directory)
8#
9#   modified:   libs/lib1 (new commits)
10#
11        
12no changes added to commit (use "git add" and/or "git commit -a" )
13        
14➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
15git add libs/lib1
16        
17➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
18git commit -m  "update lib1 to lastest commit id"
19        
20[master ce1f3ba] update lib1 to lastest commit id
211 files changed, 1 insertions(+), 1 deletions(-)
22
23</file></file>
24

1
2
1我们暂时不push到仓库,等待和lib2的修改一起push。
2

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
1➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master)  
2cd libs/lib2
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib2 git:(master)  
5echo "学习Git submodule的修改并同步功能" >> lib2-features  
6        
7➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib2 git:(master) ✗
8git add lib2-features  
9        
10➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib2 git:(master) ✗
11git commit -m  "添加文字:学习Git submodule的修改并同步功能"
12        
13[master e372b21] 添加文字:学习Git submodule的修改并同步功能
141 files changed, 1 insertions(+), 0 deletions(-)
15
16➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib2 git:(master)
17git push
18        
19Counting objects: 5, done.
20Delta compression using up to 2 threads.
21Compressing objects: 100% (2/2), done.
22        
23Writing objects: 100% (3/3), 376 bytes, done.
24Total 3 (delta 0), reused 0 (delta 0)
25Unpacking objects: 100% (3/3), done.
26        
27To /home/henryyan/submd/repos/lib2.git
287290dce..e372b21  master -> master
29        
30➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib2 git:(master)  
31echo "学习Git submodule的修改并同步功能" >> lib2-features  
32        
33➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib2 git:(master) ✗
34git add lib2-features  
35        
36➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib2 git:(master) ✗
37git commit -m  "添加文字:学习Git submodule的修改并同步功能"
38        
39[master e372b21] 添加文字:学习Git submodule的修改并同步功能
401 files changed, 1 insertions(+), 0 deletions(-)
41        
42➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib2 git:(master)
43git push
44        
45Counting objects: 5, done.
46Delta compression using up to 2 threads.
47Compressing objects: 100% (2/2),done.
48Writing objects: 100% (3/3), 376 bytes, done.
49Total 3 (delta 0), reused 0 (delta 0)
50Unpacking objects: 100% (3/3), done.
51        
52To /home/henryyan/submd/repos/lib2.git
537290dce..e372b21  master -> master
54        
55➜ henryyan@hy-hp  ~ /submd/ws/project2/libs/lib2 git:(master)  
56cd ~/submd/ws/project2
57        
58➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
59git status  
60        
61# On branch master
62# Your branch is ahead of 'origin/master' by 1 commit.
63#
64# Changes not staged for commit:
65#   (use "git add <file>..." to update what will be committed)
66#   (use "git checkout -- <file>..." to discard changes in working directory)
67#
68#   modified:   libs/lib2 (new commits)
69#
70        
71no changes added to commit (use "git add" and/or "git commit -a")
72        
73➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
74git add libs/lib2 
75        
76➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master) ✗
77git commit -m "update lib2 to lastest commit id"
78        
79[master df344c5] update lib2 to lastest commit id
801 files changed, 1 insertions(+), 1 deletions(-)
81
82➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master)
83git status  
84        
85# On branch master
86# Your branch is ahead of 'origin/master' by 2 commits.
87#
88        
89nothing to commit (working directory clean)
90        
91➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master)
92git push
93
94Counting objects: 8, done.
95Delta compression using up to 2 threads.
96Compressing objects: 100% (6/6), done.
97        
98Writing objects: 100% (6/6), 776 bytes, done.
99Total 6 (delta 0), reused 0 (delta 0)
100Unpacking objects: 100% (6/6), done.
101        
102To /home/henryyan/submd/ws/../repos/project2.git
1038dc697f..df344c5  master -> master
104        
105</file></file>
106

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

现在project2已经享受到了最新的代码带来的快乐,那么既然project1和project2属于同一个风格,或者调用同一个功能,要让这两个(可能几十个)项目保持一致。


1
2
3
4
5
6
7
8
1➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master)  
2cd ../project1
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)
5git pull
6        
7Already up-to-date.
8

看看上面的结果对吗?为什么lib1和lib2更新了但是没有显示new commits呢?说到这里我记得刚刚开始学习的时候真得要晕死了,Git跟我玩捉迷藏游戏,为什么我明明提交了但是从project1更新不到任何改动呢?

帮大家分析一下问题,不过在分析之前先看看当前(project1和project2)的submodule状态:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1# project2 的状态,也就是我们刚刚修改后的状态
2        
3➜ henryyan@hy-hp  ~ /submd/ws/project2 git:(master)
4git submodule  
5        
68c666d86531513dd1aebdf235f142adbac72c035 libs /lib1 (heads/master)
7        
8e372b21dffa611802c282278ec916b5418acebc2 libs /lib2 (heads/master)
9        
10# project1 的状态,等待更新submodules
11        
12➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)
13git submodule  
14        
1536ad12d40d8a41a4a88a64add27bd57cf56c9de2 libs /lib1 (remotes /origin/HEAD)
167290dce0062bd77df1d83b27dd3fa3f25a836b54 libs /lib2 (heads /master)
17

两个项目有两个区别:

  • 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
20
21
22
23
24
25
26
27
28
29
30
31
1➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)  
2cd libs/lib1
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1/libs/lib1 
5git checkout master
6        
7Previous HEAD position was 36ad12d... update lib1-features by developer B
8Switched to branch  'master'
9Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
10        
11➜ henryyan@hy-hp  ~ /submd/ws/project1/libs/lib1 git:(master)
12git pull
13        
14remote: Counting objects: 4, done.
15remote: Compressing objects: 100% (2/2), done.
16remote: Total 3 (delta 0), reused 0 (delta 0)
17Unpacking objects: 100% (3/3), done.
18        
19From /home/henryyan/submd/repos/lib1
20        
2136ad12d..8c666d8  master     -> origin/master
22Updating c22aff8..8c666d8
23Fast-forward
24README        |    1 +
25lib1-features |    1 +
26        
272 files changed, 2 insertions(+), 0 deletions(-)
28create mode 100644 README
29        
30➜ henryyan@hy-hp  ~ /submd/ws/project1/libs/lib1 git:(master)
31

果不其然,我们看到了刚刚在project2中修改的内容,同步到了project1中,当然现在更新了project1lib1,commit id也会随之变动:


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
1➜ henryyan@hy-hp  ~ /submd/ws/project1/libs/lib1 git:(master)  
2cd ../../
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
5git status  
6        
7# On branch master
8# Changes not staged for commit:
9#   (use "git add <file>..." to update what will be committed)
10#   (use "git checkout -- <file>..." to discard changes in working directory)
11#
12#   modified:   libs/lib1 (new commits)
13#
14        
15no changes added to commit (use "git add" and/or "git commit -a")
16        
17➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
18git diff
19diff --git a /libs/lib1 b /libs/lib1
20index 36ad12d..8c666d8 160000
21--- a /libs/lib1
22+++ b /libs/lib1
23@@ -1 +1 @@
24        
25-Subproject commit 36ad12d40d8a41a4a88a64add27bd57cf56c9de2
26+Subproject commit 8c666d86531513dd1aebdf235f142adbac72c035
27</file></file>
28

现在最新的commit id和project2目前的状态一致,说明真的同步了;好的,现在可以使用相同的办法更新lib2了:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗  
2cd libs/lib2
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1/libs/lib2 git:(master)
5git pull
6        
7remote: Counting objects: 5, done.
8remote: Compressing objects: 100% (2/2), done.
9remote: Total 3 (delta 0), reused 0 (delta 0)
10Unpacking objects: 100% (3/3), done.
11        
12From /home/henryyan/submd/repos/lib2
137290dce..e372b21  master     -> origin/master
14Updating 7290dce..e372b21
15Fast-forward
16lib2-features |    1 +
171 files changed, 1 insertions(+), 0 deletions(-)
18

2.7.2 更新project1的submodule引用

2.7.1中我们更新了project1lib1lib2的最新版本,现在要把最新的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
24
25
26
27
28
29
30
31
32
33
34
35
1➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
2git status
3        
4# On branch master
5# Changes not staged for commit:
6#   (use "git add <file>..." to update what will be committed)
7#   (use "git checkout -- <file>..." to discard changes in working directory)
8#
9#   modified:   libs/lib1 (new commits)
10#   modified:   libs/lib2 (new commits)
11#
12        
13no changes added to commit (use "git add" and/or "git commit -a")
14        
15➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master) ✗
16git commit -a -m "update lib1 and lib2 commit id to new version"
17        
18[master 8fcca50] update lib1 and lib2 commit id to new version
192 files changed, 2 insertions(+), 2 deletions(-)
20        
21➜ henryyan@hy-hp  ~ /submd/ws/project1 git:(master)
22git push
23        
24Counting objects: 5, done.
25Delta compression using up to 2 threads.
26Compressing objects: 100% (3/3), done.
27Writing objects: 100% (3/3), 397 bytes, done.
28Total 3 (delta 0), reused 0 (delta 0)
29Unpacking objects: 100% (3/3), done.
30        
31To /home/henryyan/submd/ws/.. /repos/project1.git
32        
33c96838a..8fcca50  master -> master
34</file></file>
35

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
24
25
26
27
28
29
30
31
32
33
34
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master)
2git pull
3        
4remote: Counting objects: 5, done.
5remote: Compressing objects: 100% (3/3), done.
6remote: Total 3 (delta 0), reused 0 (delta 0)
7Unpacking objects: 100% (3/3), done.
8        
9From /home/henryyan/submd/ws/ .. /repos/project1
10        
11c96838a..8fcca50  master     -> origin/master
12Updating c96838a..8fcca50
13Fast-forward
14        
15libs/lib1 |    2 +-
16libs/lib2 |    2 +-
17        
182 files changed, 2 insertions(+), 2 deletions(-)
19        
20➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
21git status
22        
23# On branch master
24# Changes not staged for commit:
25#   (use "git add <file>..." to update what will be committed)
26#   (use "git checkout -- <file>..." to discard changes in working directory)
27#
28#   modified:   libs/lib1 (new commits)
29#   modified:   libs/lib2 (new commits)
30#
31        
32no changes added to commit (use "git add" and/or "git commit -a")
33</file></file>
34

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
5
6
7
8
9
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗  
2grep path .gitmodules | awk '{ print $3 }' > /tmp/study-git-submodule-dirs
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗  
5cat /tmp/study-git-submodule-dirs
6        
7libs/lib1
8libs/lib2
9

我们通过分析**.gitmodules**文件得出子模块的路径,然后就可以根据这些路径进行更新。

2.8.2 上路


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

把下面的脚本复制到bin/update-submodules.sh中:


1
2
3
4
5
6
7
8
9
10
11
1#!/bin/bash
2
3grep path .gitmodules | awk '{ print $3 }' >  /tmp/study-git-submodule-dirs
4        
5# read
6while read LINE
7do
8  echo $LINE
9  (cd./$LINE && git checkout master && git pull)
10done < /tmp/study-git-submodule-dirs
11

稍微解释一下上面的脚本执行过程:

  • 首先把子模块的路径写入到文件**/tmp/study-git-submodule-dirs**中;

  • 然后读取文件中的子模块路径,依次切换到master分支(修改都是在master分支上进行的),最后更新最近改动。

2.8.3

一个命令就可以代替上面的bin/update-submodules.sh的功能:


1
2
1git submodule foreach git pull
2

此命令也脚本一样,循环进入(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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
2git submodule  
3        
4+36ad12d40d8a41a4a88a64add27bd57cf56c9de2 libs/lib1 (heads/master)
5+7290dce0062bd77df1d83b27dd3fa3f25a836b54 libs/lib2 (heads/master)
6        
7# 添加执行权限
8        
9➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗  
10chmod +x ./bin/update-submodules.sh
11        
12➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
13./bin/update-submodules.sh  
14        
15libs/lib1
16        
17Already on 'master'
18        
19remote: Counting objects: 4, done.
20remote: Compressing objects: 100% (2/2), done.
21remote: Total 3 (delta 0), reused 0 (delta 0)
22Unpacking objects: 100% (3/3), done.
23        
24From /home/henryyan/submd/repos/lib1
25        
2636ad12d..8c666d8  master     -> origin/master
27Updating 36ad12d..8c666d8
28Fast-forward
29README |    1 +
301 files changed, 1 insertions(+), 0 deletions(-)
31create mode 100644 README
32libs/lib2
33Switched to branch 'master'
34remote: Counting objects: 5, done.
35remote: Compressing objects: 100% (2/2), done.
36remote: Total 3 (delta 0), reused 0 (delta 0)
37Unpacking objects: 100% (3/3), done.
38        
39From /home/henryyan/submd/repos/lib2
407290dce..e372b21  master     -> origin/master
41Updating 7290dce..e372b21
42Fast-forward
43lib2-features |    1 +
441 files changed, 1 insertions(+), 0 deletions(-)
45        
46➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
47git submodule  
48        
498c666d86531513dd1aebdf235f142adbac72c035 libs/lib1 (heads/master)
50e372b21dffa611802c282278ec916b5418acebc2 libs/lib2 (heads/master)
51        
52➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
53git status  
54        
55# On branch master
56# Untracked files:
57#   (use "git add <file>..." to include in what will be committed)
58#
59#   bin/
60        
61nothing added to commit but untracked files present (use  "git add" to track)
62        
63</file>
64

更新之后的两个变化:

  • git submodule的结果和project2的submodule commit id保持一致;

  • project1-b不再提示new commits了。

现在可以把工具添加到仓库了,当然你可以很骄傲的分享给其他项目组的同事。


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
1➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
2git add bin/update-submodules.sh  
3        
4➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master) ✗
5git commit -m "添加自动更新submodule的快捷脚本^_^"
6        
7[master 756e788] 添加自动更新submodule的快捷脚本^_^
81 files changed, 9 insertions(+), 0 deletions(-)
9        
10create mode 100755 bin/update-submodules.sh
11        
12➜ henryyan@hy-hp  ~ /submd/ws/project1-b git:(master)
13git push
14
15Counting objects: 5, done.
16Delta compression using up to 2 threads.
17Compressing objects: 100% (3/3), done.
18Writing objects: 100% (4/4), 625 bytes, done.
19Total 4 (delta 0), reused 0 (delta 0)
20        
21Unpacking objects: 100% (4/4),  done.
22        
23To /home/henryyan/submd/ws/ .. /repos/project1.git
248fcca50..756e788  master -> master
25

2.9 新进员工加入团队,一次性Clone项目和Submodules

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


1
2
3
4
5
6
1git clone /path/to/repos/foo.git
2        
3git submodule init
4
5git submodule update
6

1
2
1新员工不耐烦了,嘴上不说但是心里想:怎么那么麻烦?
2

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


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

recursive参数的含义:可以在clone项目时同时clone关联的submodules。

git help 对其解释:


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

2.9.1 使用一键方式克隆project2


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1➜ henryyan@hy-hp  ~ /submd/ws 
2git clone --recursive ../repos/project2.git project2-auto-clone-submodules
3        
4Cloning into project2-auto-clone-submodules... done.
5        
6Submodule 'libs/lib1'(/home/henryyan/submd/repos/lib1.git) registered for path  'libs/lib1'
7        
8Submodule 'libs/lib2'(/home/henryyan/submd/repos/lib2.git) registered for path  'libs/lib2'
9        
10Cloning into libs/lib1 ... done.
11        
12Submodule path 'libs/lib1': checked out '8c666d86531513dd1aebdf235f142adbac72c035'
13        
14Cloning into libs/lib2 ... done.
15        
16Submodule path 'libs/lib2' : checked out  'e372b21dffa611802c282278ec916b5418acebc2'
17

舒服……

3.移除Submodule

牢骚:搞不明白为什么git不设计一个类似:git submodule remove的命令呢?

我们从project1.git克隆一个项目用来练习移除submodule:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1➜ henryyan@hy-hp  ~ /submd/ws 
2git clone --recursive ../repos/project1.git project1-remove-submodules
3
4Cloning into project1-remove-submodules... done.
5        
6Submodule 'libs/lib1'(/home/henryyan/submd/repos/lib1.git) registered for path  'libs/lib1'
7        
8Submodule 'libs/lib2'(/home/henryyan/submd/repos/lib2.git) registered for path  'libs/lib2'
9        
10Cloning into libs/lib1 ... done.
11        
12Submodule path 'libs/lib1': checked out '8c666d86531513dd1aebdf235f142adbac72c035'
13        
14Cloning into libs/lib2 ... done.
15        
16Submodule path 'libs/lib2' : checked out  'e372b21dffa611802c282278ec916b5418acebc2'
17        
18➜ henryyan@hy-hp  ~ /submd/ws 
19cd !$
20        
21➜ henryyan@hy-hp  ~ /submd/ws 
22cd project1-remove-submodules
23

3.1 Step by

1、删除git cache和物理文件夹


1
2
3
4
5
6
7
8
9
1➜ henryyan@hy-hp  ~ /submd/ws/project1-remove-submodules git:(master)
2git rm -r --cached libs/
3        
4rm 'libs/lib1'
5rm 'libs/lib2'
6        
7➜ henryyan@hy-hp  ~ /submd/ws/project1-remove-submodules git:(master) ✗  
8rm -rf libs
9

2、删除.gitmodules的内容(或者整个文件)因为本例只有两个子模块,直接删除文件:


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

1
2
1如果仅仅删除某一个submodule那么打开.gitmodules文件编辑,删除对应submodule配置即可。
2

3、删除.git/config的submodule配置源文件:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1[core]
2    repositoryformatversion = 0
3    filemode = true
4    bare = false
5    logallrefupdates = true
6[remote "origin"]
7    fetch = +refs/heads/*:refs/remotes/origin/*
8    url = /home/henryyan/submd/ws/../repos/project1.git
9[branch "master"]
10    remote = origin
11    merge = refs/heads/master
12[submodule "libs/lib1"]
13    url = /home/henryyan/submd/repos/lib1.git
14[submodule "libs/lib2"]
15    url = /home/henryyan/submd/repos/lib2.git
16
17

删除后:


1
2
3
4
5
6
7
8
9
10
11
12
13
1[core]
2    repositoryformatversion = 0
3    filemode = true
4    bare = false
5    logallrefupdates = true
6[remote "origin"]
7    fetch = +refs/heads/*:refs/remotes/origin/*
8    url = /home/henryyan/submd/ws/../repos/project1.git
9[branch "master"]
10    remote = origin
11    merge = refs/heads/master
12
13

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1➜ henryyan@hy-hp  ~ /submd/ws/project1-remove-submodules git:(master) ✗
2git status
3        
4# On branch master
5# Changes to be committed:
6#   (use "git reset HEAD <file>..." to unstage)
7#
8#   deleted:    libs/lib1
9#   deleted:    libs/lib2
10#
11# Changes not staged for commit:
12#   (use "git add/rm <file>..." to update what will be committed)
13#   (use "git checkout -- <file>..." to discard changes in working directory)
14#
15#   deleted:    .gitmodules
16#
17        
18
19➜ henryyan@hy-hp  ~ /submd/ws/project1-remove-submodules git:(master) ✗
20git add .gitmodules
21        
22➜ henryyan@hy-hp  ~ /submd/ws/project1-remove-submodules git:(master) ✗
23git commit -m  "删除子模块lib1和lib2"
24        
25[master 5e2ee71] 删除子模块lib1和lib2
263 files changed, 0 insertions(+), 8 deletions(-)
27delete mode 100644 .gitmodules
28delete mode 160000 libs/lib1
29delete mode 160000 libs/lib2
30        
31➜ henryyan@hy-hp  ~ /submd/ws/project1-remove-submodules git:(master)
32git push
33        
34Counting objects: 3,  done .
35Delta compression using up to 2 threads.
36Compressing objects: 100% (2/2),  done.
37Writing objects: 100% (2/2), 302 bytes, done.
38        
39Total 2 (delta 0), reused 0 (delta 0)
40Unpacking objects: 100% (2/2), done.
41        
42
43To /home/henryyan/submd/ws/ .. /repos/project1.git
44756e788..5e2ee71  master -> master
45        
46</file></file></file>
47

4.结束语

对于Git Submodule来说在刚刚接触的时候多少会有点凌乱的赶紧,尤其是没有接触过svn:externals

本文从开始创建项目到使用git submodule的每个步骤以及细节、原理逐一讲解,希望到此读者能驾驭它。

学会了Git submdoule你的项目中再也不会出现大量重复的资源文件、公共类库,更不会出现多个版本,甚至一个客户的多个项目风格存在各种差异。

给TA打赏
共{{data.count}}人
人已打赏
安全经验

职场中的那些话那些事

2021-9-24 20:41:29

安全经验

elk+redis 搭建nginx日志分析平台

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索