使用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 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'


作者:姜家志
链接:http://www.jianshu.com/p/d433d3417a19
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。