打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用Git Submodule管理子模块


基于公司的多项目,我们提取了一个公共的类库提供给多个项目使用,但是这个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-librarygit 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 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 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'
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Git Submoudle使用完整教程
Git 本身不支持对某一特定文件夹或者子目录的权限控制,但是可以通过子模块的形式间接实现
Git使用子模块引用外部项目
Google Code项目代码托管网站上Git版本控制系统使用简明教程
从 Git 编译源码
Cocoapods完整使用教程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服