我是靠谱客的博主 温柔信封,最近开发中收集的这篇文章主要介绍Git Submodule完整教程(2/3),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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同时负责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
➜ 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中,当然现在更新了project1lib1,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中我们更新了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
➜ 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需要更新!!!

工欲善其事,必先利其器,写一个脚本代替手动任务。

最后

以上就是温柔信封为你收集整理的Git Submodule完整教程(2/3)的全部内容,希望文章能够帮你解决Git Submodule完整教程(2/3)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(224)

评论列表共有 0 条评论

立即
投稿
返回
顶部