[转]使用Git Submodule管理子模块
本文转自:https://blog.csdn.net/qq_37788558/article/details/78668345
实例代码: 父项目:https://github.com/jjz/pod-project 子项目:https://github.com/jjz/pod-library
使用场景
基于公司的多项目,我们提取了一个公共的类库提供给多个项目使用,但是这个library怎么和git在一起方便的管理呢? 需要解决以下的几个问题:
- 如何在git项目中导入library库?
- library库在其他的项目中被修改了如何push?
- 其他项目如何获取到library库最新的提交?
- 如何在clone的时候能够自动导入library库?
解决以上问题,我使用git 的Submodule来解决。
什么是Submodule?
git Submodule 是一个很好的项目协作工具,他允许类库项目做为repository,子项目做为一个单独的git项目存在父项目中,子项目可以有自己的独立的commit,push,pull。而父项目以Submodule的形式包含子项目,父项目可以指定子项目header,父项目中会提交 Submodule的信息,在clone父项目的时候可以把Submodule初始化。
在项目中添加Submodule
git submodule add git@github.com:jjz/pod-library.git pod-library
使用 git status命令可以看到
- git status
- On branch master
- Changes to be committed:
- new file: .gitmodules
- new file: pod-library
多了两个需要提交的文件gitmodules 内容
- [submodule "pod-library"]
- path = pod-library
- url = git@github.com:jjz/pod-library.git
这里记录了子项目的目录和子项目的git信息
- pod-libray
- Subproject commit 4ac42d2f8b9ba0c2f0f2f2ec87ddbd529275fea5
这里是子项目的commit的id,git并不会记录Submodule的文件变动,它是按照这个commit的git来对比Submodule的变动的
这两个文件都需要提交到父项目的git中
我们还可以这样添加Submodule
- git add .gitmodules pod-ibrary
- git commit -m "pod-library submodule"
- git submodule init
修改提交Submodule
首先要确认有对Submodule的commit权限
进入Submodule目录里面,对修改的文件进行提交
cd pod-library/
我们修改了其中的一个文件看下文件的变动
- git status
- modified: pod-library/UseAFHTTP.h
commit submodule
git commit -a -m'test submodule'
push 到远端
>git push
然后再回到父目录:
- cd ..
- git status
- on branch master
- modified: pod-library (new commits)
可以看到pod-library已经变更为最新的commit id
Subproject commit 330417cf3fc1d2c42092b20506b0d296d90d0b5f
我们需要把推送到父项目的远端
- git commit -m'update submodule'
- git push
更新Submodule
更新的方法有两种:
- 在父项目的目录下运行
git submodule foreach git pull
在Submodule的目录下面更新
cd pod-library
git pull
注意更新Submodule的时候如果有新的commit id产生,需要在父项目产生一个新的提交,pod-libray文件中的 Subproject commit会变为最新的commit id
在clone的时候初始化Submodule
- 采用递归参数
--recursive
git clone git@github.com:jjz/pod-project.git --recursive
- loning into 'pod-project'...
- remote: Counting objects: 57, done.
- remote: Compressing objects: 100% (45/45), done.
- remote: Total 57 (delta 13), reused 49 (delta 8), pack-reused 0
- Receiving objects: 100% (57/57), 18.79 KiB | 0 bytes/s, done.
- Resolving deltas: 100% (13/13), done.
- Checking connectivity... done.
- Submodule 'pod-library' (git@github.com:jjz/pod-library.git) registered for path 'pod-library'
- Cloning into 'pod-library'...
- remote: Counting objects: 34, done.
- remote: Compressing objects: 100% (25/25), done.
- remote: Total 34 (delta 8), reused 30 (delta 7), pack-reused 0
- Receiving objects: 100% (34/34), 12.95 KiB | 0 bytes/s, done.
- Resolving deltas: 100% (8/8), done.
- Checking connectivity... done.
- Submodule path 'pod-library': checked out '330417cf3fc1d2c
- 42092b20506b0d296d90d0b5f'
会自动init Submodule
或者使用第二种方法
先clone父项目
git clone git@github.com:jjz/pod-project.git
cd pod-project
git submodule init
- Submodule 'pod-library' (git@github.com:jjz/pod-library.git)
- registered for path 'pod-library'
git submodule update
- Cloning into 'pod-library'...
- remote: Counting objects: 34, done.
- remote: Compressing objects: 100% (25/25), done.
- remote: Total 34 (delta 8), reused 30 (delta 7), pack-reused 0
- Receiving objects: 100% (34/34), 12.95 KiB | 0 bytes/s, done.
- Resolving deltas: 100% (8/8), done.
- Checking connectivity... done.
- Submodule path 'pod-library': checked out '330417cf3fc1d2c42092b20506b0d296d90d0b5f'
删除Submodule
git 并不支持直接删除Submodule需要手动删除对应的文件
- cd pod-project
- git rm --cached pod-library
- rm -rf pod-library
- rm .gitmodules
- vim .git/config
- [submodule "pod-library"]
- url = git@github.com:jjz/pod-library.git
- 删除submodule相关的内容
- git commit -a -m 'remove pod-library submodule'
作者:姜家志
链接:http://www.jianshu.com/p/d433d3417a19
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
[转]使用Git Submodule管理子模块的更多相关文章
- 使用Git Submodule管理子模块
转自:https://segmentfault.com/a/1190000003076028 使用场景 基于公司的项目会越来越多,常常需要提取一个公共的类库提供给多个项目使用,但是这个library怎 ...
- 转:Git Submodule管理项目子模块
使用场景 当项目越来越庞大之后,不可避免的要拆分成多个子模块,我们希望各个子模块有独立的版本管理,并且由专门的人去维护,这时候我们就要用到git的submodule功能. 常用命令 git clone ...
- Git Submodule管理项目子模块
使用场景 当项目越来越庞大之后,不可避免的要拆分成多个子模块,我们希望各个子模块有独立的版本管理,并且由专门的人去维护,这时候我们就要用到git的submodule功能. 常用命令 git clone ...
- git submodule 管理子项目
使用场景 拆分项目,当项目越来越大之后,我们希望 子模块 可以单独管理,并由 专门 的人去维护,这个时候只可以使用 git submodule 去完成. 常用命令 git clone <repo ...
- 使用git submodule管理一个需要多个分立开发或者第三方repo的项目
在项目开发中,特别是web前端开发中,有非常多的开源第三方library,我们希望引用他们,同时也希望能够方便地保持这些第三方 开源repo的更新.另外一方面如果我们自己在开发一个网站的项目,这个项目 ...
- git submodule的使用
1.在项目中使用Submodule 为当前工程添加submodule,命令如下:git submodule add 仓库地址 路径仓库地址:是指子模块仓库地址URL.路径:指将子模块放置在当前工程下的 ...
- Git submodule 子模块的管理和使用
因为代码分了两个小组管理,一部分代码使用跨平台语言实现,一部分使用原生实现. 所以使用Git submodule 来进行管理. 1,查看/更新 子模块 $ git submodule add ssh: ...
- git submodule git 子模块管理相关操作
Git 子模块操作相关的一些命令备忘: # 当使用git clone下来的工程中带有submodule时,初始的时候 submodule的内容并不会自动下载下来的,需执行如下命令: git submo ...
- Git submodule - 子模块【转】
子模块 有种情况我们经常会遇到:某个工作中的项目需要包含并使用另一个项目. 也许是第三方库,或者你独立开发的,用于多个父项目的库. 现在问题来了:你想要把它们当做两个独立的项目,同时又想在一个项目中使 ...
随机推荐
- Django积木块11 —— 缓存
缓存 Django的缓存可以缓存视图中的函数,模版中的内容,和一些不长变化的数据. # setting CACHES = { 'default':{ 'BACKEND':'django.core.ca ...
- MyBatis在非Spring环境下第三方DataSource设置-Druid篇
首先在ITEye上面看到一个同标题文章,在此说明,此文并非转载自 http://iintothewind.iteye.com/blog/2069522 ,因为这篇文章根本就是错误的,照着上面做,工程可 ...
- junit 方法:assertEquals 和 assertTrue
assertEquals 和 assertTrue 区别相同之处:都能判断两个值是否相等 assertTrue 如果为true,则运行success,反之Failure assertEquals 如果 ...
- HC-05蓝牙模块配置与使用
蓝牙模块BT-HC05模块是一款高性能的蓝牙串口模块. 1.可用于各种带蓝牙功能的电脑.蓝牙主机.手机.PDA.PSP等智能终端配对. 2.宽波特率范围4800~1382400,并且模块兼容单片机系统 ...
- 常用类:Object
2017-08-08 Object :作为所有类的根类,(超类,父类) 常用的方法: public int hasCode(){//返回该对象的哈希码值(地址)}:判断对象是否在同一内存地址上 pub ...
- Flask框架(一)
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return '<h1>hello w ...
- Python小练习之寻找101到200之间的素数
方法1:from math import * def primeNumber(start,end): num = 0 for i in range(start,end): flag = 0 for j ...
- [转]深入理解 GRE tunnel
我以前写过一篇介绍 tunnel 的文章,只是做了大体的介绍.里面多数 tunnel 是很容易理解的,因为它们多是一对一的,换句话说,是直接从一端到另一端.比如 IPv6 over IPv4 的 tu ...
- pip 安装模块时出现error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools":
在使用pip安装mysqlclient模块时,出现如下错误: 在网上查找资料后显示可能是由于不兼容导致的,最好去下载.whl文件安装成功. 资源地址:http://www.lfd.uci.edu/~g ...
- centos7使用wordpress布署网站(2)
1.接下来需要配置数据库,为使用wordpress做准备 修改认证方式: vim .../phpMyAdmin/config.inc.php [...] $cfg['Servers'][$i]['au ...