使用Git Submodule管理子模块
来源:互联网 发布:iphone透明屏幕软件 编辑:程序博客网 时间:2024/06/04 19:24
实例代码:父项目: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 statusOn branch masterChanges 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-libraySubproject 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 statuson 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 0Receiving 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 0Receiving 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'
会自动
init Submodule
或者使用第二种方法
先clone父项目git clone git@github.com:jjz/pod-project.git
cd pod-project
git submodule initSubmodule '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 0Receiving 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-projectgit rm --cached pod-libraryrm -rf pod-libraryrm .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管理子模块
- git学习 - 使用submodule(子模块)管理外链库
- Git Submodule管理项目子模块
- git submodule子模块
- git-子模块submodule
- git丶Git Submodule管理项目子模块
- git管理代码报错(使用Sourcetree工具) 有子模块Submodule(未解决)
- git子模块使用之git submodule与 git subtree比较
- Git Submodule管理项目子模块 使用场景 当项目越来越庞大之后,不可避免的要拆分成多个子模块,我们希望各个子模块有独立的版本管理,并且由专门的人去维护,这时候我们就要用到git的
- Git使用子模块引用外部项目
- git子模块
- Git下载子模块
- git子模块
- git-子模块subtree
- GIT-子模块
- git 子模块以及 在 Jenkins中如何使用
- 6.6 Git 工具 - 子模块
- 指针与地址
- 用c语言实现字符串倒序的几种情况
- 各种xlsx转换程Csv
- 静音去除的问题
- 机器学习算法练习之(一):Python实现logistic回归
- 使用Git Submodule管理子模块
- 怎么aop中实现记录请求访问时间:用ThreadLocal
- 3. JNI Functions
- Java树形菜单_轻量级js树形插件_jsTree树形插件
- Java---发送HTTPS请求
- 如何将python3.6软件的py文件打包成exe程序
- 初次运行 Git 前的配置
- win8.1 or win server 2012 安装python3.5以上导致Error 0x80240017
- C++_运算符重载的注意事项