参考http://www.diguage.com/archives/146.html

一、添加子模块

从新建一个项目,或者从远处服务器上克隆一个项目,作为“顶级项目”。这里,从 Github 上新建一个项目,然后再克隆下来:

➜   git clone git@github.com:diguage/parent.git

Cloning into 'parent'...
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
Receiving objects: % (/), 4.59 KiB | bytes/s, done.
Checking connectivity... done.

进入刚克隆下来的项目,我们来添加子模块。这里的子模块就必须从远处服务器上克隆了。另外,子模块可以放在项目的任意目录下,并且可以根据自己的需要重命名。为了方便演示,所以就直接放在子目录了。进入顶级项目,我们先看一下版本库的状态:

➜  cd parent
➜ git status #查看版本库的状态
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

这里提示,项目中没有任何需要提交的东西。下面,我们开始添加子模块:

命令如下:git submodule add 仓库地址 路径

git submodule add git@github.com:diguage/child.git #添加子模块child

Cloning into 'child'...
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
Receiving objects: % (/), 4.63 KiB | bytes/s, done.
Checking connectivity... done.

这时,再看一下版本库的状态:

➜  git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) new file: .gitmodules
new file: child

这时,我们看到,除了刚刚添加的 child 模块外,还多了一个 .gitmodules 文件。这个文件就是用于记录子模块的路径已经远程版本库地址的地方。这两个文件处于待提交的状态。下面把这个修改提交一下。


➜   git commit -am "增加子模块"

[master a05bfe0] 增加子模块
files changed, insertions(+)
create mode .gitmodules
create mode child ➜ git push origin master Counting objects: , done.
Delta compression using up to threads.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To git@github.com:diguage/parent.git
e56cd5a..a05bfe0 master -> master

这时,大家可以登录到 Github 或者其他的版本库服务器上看一下项目的状态。至此,添加子模块的工作已经完成。


二、更新子模块

当我们添加完子模块后,过段时间后,子模块有更新,这时候,我们就需要更新了。

方式一


➜  cd child
➜ git pull origin master remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
Unpacking objects: % (/), done.
From github.com:diguage/child
* branch master -> FETCH_HEAD
d4ec39a..1b855a4 master -> origin/master
Updating d4ec39a..1b855a4
Fast-forward
README.md | +++
file changed, insertions(+)

再进入到顶级项目中,查看一下项目状态:

➜  cd ..
➜ git status On branch master
Your branch is up-to-date with 'origin/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: child (new commits) no changes added to commit (use "git add" and/or "git commit -a")

这里可以看到明显的提示,在项目有一个新的提交。为什么这样呢?其实,Git 在顶级项目中记录了一个子模块的提交日志的指针,用于保存子模块的提交日志所处的位置,以保证无论子模块是否有新的提交,在任何一个地方克隆下顶级项目时,各个子模块的记录是一致的。避免因为所引用的子模块不一致导致的潜在问题。如果我们更新了子模块,我们需要把这个最近的记录提交到版本库中,以方便和其他人协同。这也是刚刚添加完子模块后还要在顶级项目中提交一次的原因。

➜  git commit -am "更新子模块"

[master 5ac4dbb] 更新子模块
file changed, insertion(+), deletion(-) ➜ git push origin master Counting objects: , done.
Delta compression using up to threads.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To git@github.com:diguage/parent.git
a05bfe0..5ac4dbb master -> master

备注:进入子模块目录后,你就当做这是一个独立的项目,可以正常进行一个普通 Git 项目所执行的任何操作。更新完正常的修改、添加任务后,把所做的修改提交,然后推送到远处版本库上。然后,如果需要记得在顶级项目中,也做一次提交,修改一下顶级项目所引用的子模块的提交日志的“指针”。

三、克隆项目[这个我实践过,别的尚未实践]

当多人合作搞一个项目,需要从远处版本库上克隆项目代码。当使用git clone下来的工程中带有submodule时,初始的时候,submodule的内容并不会自动下载下来的,此时,只需执行如下命令:git submodule update --init --recursive

即可将子模块内容下载下来后工程才不会缺少相应的文件。

方式一(比较麻烦,但是是基础的方式)


$ git clone https://github.com/ethz-asl/rovio.git   # 第一步
Cloning into 'rovio'...
remote: Counting objects: 2759, done.
remote: Total 2759 (delta 0), reused 0 (delta 0), pack-reused 2759
Receiving objects: 100% (2759/2759), 964.96 KiB | 7.00 KiB/s, done.
Resolving deltas: 100% (1808/1808), done.
Checking connectivity... done.
$ cd rovio
$ git submodule update --init --recursive # 第二步
Submodule 'lightweight_filtering' (https://bitbucket.org/bloesch/lightweight_filtering.git) registered for path 'lightweight_filtering'
Cloning into 'lightweight_filtering'...
remote: Counting objects: 1541, done.
remote: Compressing objects: 100% (921/921), done.
remote: Total 1541 (delta 691), reused 0 (delta 0)
Receiving objects: 100% (1541/1541), 319.81 KiB | 99.00 KiB/s, done.
Resolving deltas: 100% (1050/1050), done.
Checking connectivity... done.
Submodule path 'lightweight_filtering': checked out 'b1d235dca7646c013c96c9335c3d8a982b4244b5'
$ cd lightweight_filtering
$ git status # 第三步
HEAD detached at b1d235d
nothing to commit, working directory clean
$ git checkout master # 第四步
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

方式二(比较简单的方式)


$ git clone --recursive https://github.com/ethz-asl/rovio.git # 第一步
$ cd rovio/lightweight_filtering # 第二步
$ git checkout master # 第三步

四、删除子模块

首先,要在“.gitmodules”文件中删除相应配置信息。

然后,执行“git rm 子模块 ”命令将子模块所在的文件从git中删除。



➜   git rm child  #删除子模块

rm 'child'

➜   git status  #查看版本库的状态

On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) modified: .gitmodules
deleted: child ➜ git commit -am "删除子模块" #删除完子模块后还要在顶级项目中提交一次 [master b0b088b] 删除子模块
2 files changed, 4 deletions(-)
delete mode 160000 child ➜ git push origin master Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 274 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:diguage/parent.git
5ac4dbb..b0b088b master -> master



Git Submodule 使用简介的更多相关文章

  1. git submodule初用

    git submodule主要是用于针对git项目中还存在git子模块的情况.在一般情况下,我们通过git clone 获取项目的时候会把项目中的所有信息都拿到.但是,如果相关中存在git子模块那么, ...

  2. git submodule 使用

    这个是备忘录,原网页: https://medium.com/@porteneuve/mastering-git-submodules-34c65e940407 http://cncc.bingj.c ...

  3. git submodule(转载)

    From:http://www.worldhello.net/2010/01/26/425.html 删除 git submodule (git 库子模组) 有两种情况会创建 git submodul ...

  4. Git submodule 特性

    当你习惯了代码的 VCS 后,基本上是离不开的. 作为一个依赖多个子项目组成的项目,要实现直观的代码逻辑结构,可以考虑使用 Git submodule 特性. 当然,如果只是单独的依赖的话,用依赖管理 ...

  5. Git subtree和Git submodule

    git submodule允许其他的仓库指定以一个commit嵌入仓库的子目录. git subtree替代git submodule命令,合并子仓库到项目中的子目录.不用像submodule那样每次 ...

  6. 使用git submodule管理一个需要多个分立开发或者第三方repo的项目

    在项目开发中,特别是web前端开发中,有非常多的开源第三方library,我们希望引用他们,同时也希望能够方便地保持这些第三方 开源repo的更新.另外一方面如果我们自己在开发一个网站的项目,这个项目 ...

  7. Git submodule实战

    http://blog.jqian.net/post/git-submodule.html 使用git管理的项目开发中,如果碰到公共库和基础工具,可以用submodule来管理. 常用操作 例如, 公 ...

  8. git submodule的操作

    对于有submodule的库,检出的方法是: git clone https://github.com/BelledonneCommunications/linphone-android.git -- ...

  9. git submodule相关操作

    $ cd 项目目录 // 初始化 $ git init $ git submodule add https://github.com/XXXX // 普通更新 $ git submodule upda ...

随机推荐

  1. 根据inode编号来删除文件或目录

    在Linux系统上,有时候会出现文件名为特殊字符的文件或目录,当我们使用rm来删除这样的文件或目录时,就会出错导致删不掉.但是我们可以依据inode号来删除这样的文件,方法如下: (1)执行ls -i ...

  2. Hash的一点测试

    哈希表的学习与测试 以前写的hash都是碰运气的hash,就是乘上质数取模的那种,这样不能保证不碰撞,所以今天先写上几个双hush和链表的hash,并比较一下他们的速度,测试的话用洛谷上的“字符串哈希 ...

  3. python函数入门

    知识内容: 1.函数的作用 2.函数的定义与调用 3.函数的返回值 4.函数的参数 5.局部变量与全局变量 6.作用域 一.函数的作用 1.复用代码 将可能重复执行的代码封装成函数,并在需要执行的地方 ...

  4. Tornado之模板

    知识点 静态文件配置 static_path StaticFileHandler 模板使用 变量与表达式 控制语句 函数 块 4.1 静态文件 现在有一个预先写好的静态页面文件 (下载静态文件资源), ...

  5. UVA-568-数论

    题意 输入一个n,求n!最后一个不是0的数 2x5肯定是等于10的,先把所有不是2和5的数乘起来,保留最后一位 计算过程中计算出2和5的个数 因为2*5=10,而且2的个数比5的个数多,所以最后只要把 ...

  6. OpenACC 计算构建内的自定义函数

    ▶ 使用 routine 构件创建的自定义函数,在并行调用上的差别 ● 代码,自定义一个 sqab 函数,使用内建函数 fabsf 和 sqrtf 计算一个矩阵所有元素绝对值的平方根 #include ...

  7. 1.HTML编码解码URL替换--代码整理

    public class HtmlCode { public static String encode(String str){ String s = ""; if (str.le ...

  8. Activity服务类-1 DynamicBpmnService服务类

    这个服务是5.19版本后新增的一个服务,和RepositoryService的作用相似,都是与流程定义有关,但是却完全不同.从名字上来看是动态的BPMN服务,看里面的方法都是改变流程的相关属性.这个方 ...

  9. Windows 域用户

    Windows 2000 组及说明 分类: Windows 2000 的组分为Security 和 Distribution 两种. Security 类型是Windows 2000 唯一用于赋予权限 ...

  10. eclipse 断点找到同名的其它类

    转载自Eclipse断点进入另一个项目的同名Java文件中(http://tunps.com/p/11789.html) eclipse 断点找到同名的其它类 A和B是两个相同的项目,A一直本地,B是 ...