git submodule 使用小结
git submodule 使用小结
原文链接 http://blog.gezhiqiang.com/2017/03/08/git-submodule/######
Git Submodule 允许一个git仓库,作为另一个git仓库的子目录,并且保持父项目和子项目相互独立。
添加子仓库
$ git submodule add <仓库地址> <本地路径>
新建一个父仓库main,一个子仓库sub。将父仓库克隆到本地。
$ git clone ssh://git@10.2.237.56:23/dennis/main.git
进入父仓库,并添加子仓库。
$ git submodule add ssh://git@10.2.237.56:23/dennis/sub.git lib
添加成功后,在父仓库根目录增加了.gitmodule文件。
[submodule "sub"]
path = lib
url = ssh://git@10.2.237.56:23/dennis/sub.git
并且在父仓库的git 配置文件中加入了submodule段。
$ cat .git/config
// 加了submodule段
[submodule "sub"]
url = ssh://git@10.2.237.56:23/dennis/sub.git
注意:添加子仓库之后,主仓库的对应目录下(这里为lib),并不是sub仓库的文件,而是对应的commit id。如图所示:

检出(checkout)
克隆一个包含子仓库的仓库目录,并不会clone下子仓库的文件,只是会克隆下.gitmodule描述文件,需要进一步克隆子仓库文件。
// 初始化本地配置文件
$ git submodule init
// 检出父仓库列出的commit
$ git submodule update
或者使用组合指令。
$ git submodule update --init --recursive
此时子目录在一个未命名分支,此时子仓库有改动并没有检测到。
$ git branch
* (HEAD detached at 46a27af)
master
在子仓库,切换到master分支,并git pull最新代码之后,回到主仓库目录,会显示子仓库修改,需要在主仓库提交修改,即修改指定的commit id。
$ 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: lib (new commits)
no changes added to commit (use "git add" and/or "git commit -a")
更新
如果在本地修改子仓库,在主仓库 git status会显示子仓库有修改。
$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)
(commit or discard the untracked or modified content in submodules)
modified: lib (modified content)
no changes added to commit (use "git add" and/or "git commit -a")
需要现在子仓库提交,然后再到主仓库提交代码。
删除子仓库
- 删除.gitsubmodule里相关部分
- 删除.git/config 文件里相关字段
- 删除子仓库目录。
$ git rm --cached <本地路径>
如果未按照上述步骤删除,可能残留在.git/modudles文件夹内。
参考
[1] Git submodule实战
22人点赞
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 ...
随机推荐
- Vue框架初识01
摘要 vue简介 vue使用 一.Vue简介: 简介: Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 A ...
- 爬虫请求库之selenium
一.介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作, ...
- .Net Core-类库中创建CodeFirst
本文仅用来学习记录. 搭建项目架构的时候,需要在类库中进行CodeFirst数据迁移 1.在项目的解决方案中,添加类库ERPFrame.Model 2.在类库项目中 添加实体模型和数据上下文 其中 ...
- mysql开启二进制日志
打开xhell进入系统 进入mysql配置文件目录 执行 cd /etc/mysql 首先找到my.cnf这个配置文件,然后使用vim进入文件编辑 放开我标记的地方. 注意我标记的地方,其实这个就是在 ...
- 5_PHP数组_3_数组处理函数及其应用_9_数组集合运算函数
以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组集合运算函数 1. array_merge() 函数 程序: <?php $array1 = array ...
- MQTT协议中的topic
1.MQTT协议中的topic 定阅与发布必须要有主题,只有当定阅了某个主题后,才能收到相应主题的payload,才能进行通信. 2. 主题层级分隔符--"/" 主题层级分隔符使得 ...
- Matlab单例模式
classdef SingletonClass < handle methods(Access = private) function obj = SingletonClass() disp(' ...
- 【开发笔记】- Java读取properties文件的五种方式
原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...
- ConcurrentLinkedQueue (一)
ConcurrentLinkedQueue 主要讲一下在JDK8中,ConcurrentLikedQueue是如何入队,出队的. 首先我们要明白,ConcurrentLikedQueue是一种安全的没 ...
- p1.BTC-密码学的原理
所谓加密货币是不加密的,区块链上所有的交易内容(包括:账户的地址,转账的地址)都是公开的. Bitcoin中主要用到密码学的中的两个功能:Hash和签名. 一 Hash Cryptographic h ...