Git Submodule 使用简介
参考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 使用简介的更多相关文章
- git submodule初用
git submodule主要是用于针对git项目中还存在git子模块的情况.在一般情况下,我们通过git clone 获取项目的时候会把项目中的所有信息都拿到.但是,如果相关中存在git子模块那么, ...
- git submodule 使用
这个是备忘录,原网页: https://medium.com/@porteneuve/mastering-git-submodules-34c65e940407 http://cncc.bingj.c ...
- git submodule(转载)
From:http://www.worldhello.net/2010/01/26/425.html 删除 git submodule (git 库子模组) 有两种情况会创建 git submodul ...
- Git submodule 特性
当你习惯了代码的 VCS 后,基本上是离不开的. 作为一个依赖多个子项目组成的项目,要实现直观的代码逻辑结构,可以考虑使用 Git submodule 特性. 当然,如果只是单独的依赖的话,用依赖管理 ...
- Git subtree和Git submodule
git submodule允许其他的仓库指定以一个commit嵌入仓库的子目录. git subtree替代git submodule命令,合并子仓库到项目中的子目录.不用像submodule那样每次 ...
- 使用git submodule管理一个需要多个分立开发或者第三方repo的项目
在项目开发中,特别是web前端开发中,有非常多的开源第三方library,我们希望引用他们,同时也希望能够方便地保持这些第三方 开源repo的更新.另外一方面如果我们自己在开发一个网站的项目,这个项目 ...
- Git submodule实战
http://blog.jqian.net/post/git-submodule.html 使用git管理的项目开发中,如果碰到公共库和基础工具,可以用submodule来管理. 常用操作 例如, 公 ...
- git submodule的操作
对于有submodule的库,检出的方法是: git clone https://github.com/BelledonneCommunications/linphone-android.git -- ...
- git submodule相关操作
$ cd 项目目录 // 初始化 $ git init $ git submodule add https://github.com/XXXX // 普通更新 $ git submodule upda ...
随机推荐
- Ubuntu-14.04.1 desktop安装时遇到的小问题
su root认证失败:sudo passwd root,然后设置新密码. 重装linux导致g++显示已安装,但无法使用:将"系统设置"/"软件源"中所有更新 ...
- 信息学奥赛(NOIP)复赛学习方法推荐
一.确定你的语言 NOIP包括三种语言c/c++/pascal,在最初必须确定自己使用的语言.没有c/c++基础的,个人建议使用pascal,因为它更容易上手,如果有充裕的时间,则建议c/c++,因为 ...
- Mysql主从同步在线实施步骤【适合大数据库从库配置】
Mysql主从同步在线实施步骤[适合大数据库从库配置] MySQL的主从搭建大家有很多种方式,传统的mysqldump方式是很多人的选择之一,但比较适合在新实例中实施,对于较大的数据库则存在停机等不可 ...
- python-pycharm中使用anaconda部署python环境
pycharm中使用anaconda部署python环境 今天来说一下python中一个管理包很好用的工具anaconda,可以轻松实现python中各种包的管理.相信大家都会有这种体验,在pycha ...
- video元素和audio元素
内容: 1.video元素 2.audio元素 注:这两个元素均是HTML5新增的元素 1.video元素 (1)用途 <video> 标签定义视频,比如电影片段或其他视频流 (2)标签属 ...
- HTML5进阶
内容: 1.geolocation元素 2.video元素和audio元素 3.localStorage 4.WebWorker 5.WebSQL.IndexedDB 6.文件操作.文件拖拽新概念 7 ...
- (12/24) css进阶:sass文件的打包和分离
1.安装sass打包的loader 这里需要 在项目目录下用npm安装两个包.node-sass和sass-loader,(也可以使用cnpm安装) 因为sass-loader依赖于node-sass ...
- as3 有趣现象 关于声明与变量
当使用了一个变量,并且前后期都没有在有效域内对此声明,不管有没有赋值,都会报错. 但先使用了一个变量,后期在有效域内对此声明,那么此变量不报错,但在声明之前没有赋值,那么赋值默认值:如果使用变量时,赋 ...
- c++变量声明、定义,const变量
变量声明和定义的主要区别: 声明不分配存储空间,定义分配存储空间. 变量可以声明多次,但只能定义一次(一个变量只能在一个源文件中定义) 声明通常放在头文件(.h)中,定义放在源文件(.cpp)中 变量 ...
- 数据报表之Excel操作模块
Excel是当今最流行的电子表格处理软件,支持丰富的计算函数及图表,在系统运营方面广泛用于运营数据报表,比如业务质量.资源利用.安全扫描等报表,同时也是应用系统常见的文件导出格式,以便数据使用人员做进 ...