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

 

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. 【IDEA使用技巧】(5) —— IntelliJ IDEA集成Tomcat部署Maven Web项目

    1.IntelliJ IDEA集成Tomcat部署Maven Web项目 1.1.IDEA构建Maven Web项目 使用IDEA来创建一个简单的Hello World的Maven Web项目,并使用 ...

  2. go 学习笔记(2)go test

    Test 的写法: 每一个test文件必须import 一个"testing" test文件下的每一个test case均必须以Test开头并且符合TestXxx形式,否则go t ...

  3. 在 centos 上安装 virutalbox

    目录 简介 centos 6.x 安装 virtual box Step 1 – Add Required Yum Repositories Step 2 – Install Required Pac ...

  4. 2.8_Database Interface ADO由来

    OLE-DB,它无法广为流行,因为如下两点: 1.由于OLE-DB太底层化,使用上非常复杂,需要程序员拥有高潮的技巧. 2.OLEDB标准的API是C++API,只能供C++语言调用. 为了使得流行的 ...

  5. Hadoop之HDFS读写流程

    hadoophdfs 1. HDFS写流程 2. HDFS写流程 1. HDFS写流程 HDFS写流程 副本存放策略: 上传的数据块后,触发一个新的线程,进行存放. 第一个副本:与client最近的机 ...

  6. apache-httpd代理请求,selinux造成503问题的解决方法

    NameVirtualHost *:80 <VirtualHost *:80> ServerName test.baidu.com ProxyPreserveHost on ProxyPa ...

  7. Math对象的一些方法

    ceil(n) 返回n向上取整的最近的整数floor(n) 返回n向下取整到最近的整数max(a,b,c...) 返回最大值min(a,b,c...) 返回最小值round(n) 返回n四舍五入的最近 ...

  8. Java 之 JDBCTemplate

    Spring JDBC Spring 框架是对 JDBC 的简单封装,提供了一个 JDBCTemplate 对象简化 JDBC 的开发. 步骤: 1.导入 jar 包 2.创建 JDBCTemplat ...

  9. 【雅思】【绿宝书错词本】List37~48

    List 37 ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ List 38 ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ List 39 ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ...

  10. springboot学习入门简易版七---springboot2.0使用@Async异步执行方法(17)

    1启动类开启异步调用注解 @SpringBootApplication @EnableAsync //开启异步调用 public class StartApplication { 不开启则异步调用无效 ...