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 ...
随机推荐
- Jupyter交互式工具安装使用
Jupyter交互式工具安装使用 Jupyter Notebook(此前被称为IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言. 文档:https://jupyter ...
- CSS3 @font-face 规则
指定名为"myFirstFont"的字体,并指定在哪里可以找到它的URL: @font-face { font-family: myFirstFont; src: url('San ...
- 作业调度框架Quartz.NET-现学现用-01-快速入门
原文:作业调度框架Quartz.NET-现学现用-01-快速入门 前言 你需要应用执行一个任务吗?这个任务每天或每周星期二晚上11:30,或许仅仅每个月的最后一天执行.一个自动执行而无须干预的任务在执 ...
- jquery easyui datagrid 在翻页以后仍能记录被选中的行及刷新设置选中行数据
//easyUI的datagrid在复选框多选时,如何在翻页以后仍能记录被选中的行://注意datagrid中需要配置idField属性,一般为数据的主键 $.ajax({ type: 'GET', ...
- C# 生成随机的6位字母,包含大小写
今天自己做项目需要生成随机的6位字母,于是自己写了一个,下面代码是可以生成任意位数字母的. string _zimu = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg ...
- 将集群WEB节点静态数据迁移到共享存储器(LNMP环境)
系统版本:Centos 6.5 机器及IP规划如下: 192.168.0.117 MySQL 192.168.0.118 nginx+php 192.168.0.123 nfs ①在NFS机器上 ...
- HashMap,HashSet
HashMap,HashSet 摘自:https://www.cnblogs.com/skywang12345/p/3310887.html#a1 目录 一. HashMap(键值对形式存取,键 ...
- 22,Django常用命令
学习Django你需要了解常见命令的使用,比如创建项目,创建应用,创建超级用户,数据表创建及更新,启动服务器等.这些命令都包含在django-admin.py和manage.py里.除此以外manag ...
- 【转】DSP动态内存分配函数的使用
DSP里的动态内存分配,其分配的内存区域在在堆(heap)中.同时DSP里动态分配内存的函数还有calloc以及reclloc.这些动态分配的内存放置在.system段的全局池或堆(heap)中.因此 ...
- systemd_journal_no_entries问题解决
问题: #journalctl Journal file /var/log/journal/410/system@0.journal~ uses an unsupported feature, ign ...