设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 服务器 > 安全 > 正文

Git Submodule使用完整教程(小结)(13)

发布时间:2020-08-21 01:42 所属栏目:53 来源:网络整理
导读:henryyan@hy-hp ~/submd/ws/project1 git:(master) cd libs/lib2 henryyan@hy-hp ~/submd/ws/project1/libs/lib2 git:(master) git pullremote: Counting objects: 5, done.remote: Compressing objects: 100% (2/2

➜ 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中以保持最新的引用。

➜ 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项目的子模块(使用脚本)

➜ 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 牛刀小试

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

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

2.8.2 上路

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

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

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

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

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

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

2.8.3 2013-01-19更新

网友@紫煌给出了更好的办法,一个命令就可以代替上面的bin/update-submodules.sh的功能:

git submodule foreach git pull

此命令也脚本一样,循环进入(enter)每个子模块的目录,然后执行foreach后面的命令。

该后面的命令可以任意的,例如 git submodule foreach ls -l 可以列出每个子模块的文件列表

2.8.3 体验工具带来的便利

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读