网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/12/27 浏览:3 次 )

自从看了蒋鑫的《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 准备环境

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

初始化工作区:

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

2.1.2 初始化项目

初始化project1:

"project1" > project-infos.txt
"git rm --cached <file>..." to unstage)
#
# new file: project-infos.txt
#
"init project1"
[master (root-commit) 473a2e2] init project1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 project-infos.txt
"htmlcode">
"project2" > project-infos.txt
"git rm --cached <file>..." to unstage)
#
# new file: project-infos.txt
#
"init project2"
[master (root-commit) 473a2e2] init project2
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 project-infos.txt
"htmlcode">
"I'm lib1." > lib1-features
"init lib1"
[master (root-commit) c22aff8] init lib1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 lib1-features
"htmlcode">
"I'm lib2." > lib2-features
"init lib2"
[master (root-commit) c22aff8] init lib2
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 lib2-features
"htmlcode">
"git reset HEAD <file>..." to unstage)
#
# new file: .gitmodules
# new file: libs/lib1
# new file: libs/lib2
#
 
# 查看一下公共类库的内容
 
"htmlcode">
n@hy-hp ~/submd/ws/project1 git:(master) "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的引用信息,知道在当前项目的位置以及仓库的所在。

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

"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
 
"htmlcode">
"htmlcode">
"htmlcode">
"htmlcode">
"htmlcode">
"htmlcode">
"add by developer B"  lib1-features
"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状态:

"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的状态:

"htmlcode">
"htmlcode">
"update libs/lib1 to lastest commit id"
[master c96838a] update libs/lib1 to lastest commit id
 1 files changed, 1 insertions(+), 1 deletions(-)
"htmlcode">
"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比较一下不同:

"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中,但是没有在仓库注册,证据如下:

"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中注册子模块的信息,下面我们试试注册之后再更新子模块:

"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
 
"htmlcode">
"git reset HEAD <file>..." to unstage)
#
# new file: .gitmodules
# new file: libs/lib1
# new file: libs/lib2
#
"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
"htmlcode">
"lib1 readme contents" > README
"add file README"
[master 8c666d8] add file README
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 README
"htmlcode">
"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")
"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文件添加文字

"学习Git submodule的修改并同步功能"  lib2-features 
"添加文字:学习Git submodule的修改并同步功能"
[master e372b21] 添加文字:学习Git submodule的修改并同步功能
 1 files changed, 1 insertions(+), 0 deletions(-)
"学习Git submodule的修改并同步功能"  lib2-features 
"添加文字:学习Git submodule的修改并同步功能"
[master e372b21] 添加文字:学习Git submodule的修改并同步功能
 1 files changed, 1 insertions(+), 0 deletions(-)
"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")
"update lib2 to lastest commit id"
[master df344c5] update lib2 to lastest commit id
 1 files changed, 1 insertions(+), 1 deletions(-)
"htmlcode">
"htmlcode">
# project2 的状态,也就是我们刚刚修改后的状态
"htmlcode">
"htmlcode">
"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")
"htmlcode">
"htmlcode">
"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")
"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(-)
"htmlcode">
"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 牛刀小试

"htmlcode">
"htmlcode">
#!/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 体验工具带来的便利

"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了。

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

"添加自动更新submodule的快捷脚本^_^"
[master 756e788] 添加自动更新submodule的快捷脚本^_^
 1 files changed, 9 insertions(+), 0 deletions(-)
 create mode 100755 bin/update-submodules.sh
"htmlcode">
git clone /path/to/repos/foo.git
git submodule init
git submodule update

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

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

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

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

git help 对其解释:

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

2.9.1 使用一键方式克隆project2

"color: #ff0000">3.移除Submodule

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

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

"htmlcode">
"htmlcode">
"htmlcode">
[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、提交更改

"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
#
"删除子模块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
"color: #ff0000">4.结束语

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

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

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

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

拉取所有子模块

git submodule foreach git pull
git submodule foreach --recursive git submodule init 
git submodule foreach --recursive git submodule update 
上一篇:Git 2.27.0详细安装步骤详解
下一篇:提高github下载速度的方法可达到2MB/s(100%有效)