[转]使用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命令可以看到

  1.  
    git status
  2.  
     
  3.  
     
  4.  
    On branch master
  5.  
    Changes to be committed:
  6.  
     
  7.  
    new file: .gitmodules
  8.  
    new file: pod-library

多了两个需要提交的文件
gitmodules 内容

  1.  
    [submodule "pod-library"]
  2.  
    path = pod-library
  3.  
    url = git@github.com:jjz/pod-library.git

这里记录了子项目的目录和子项目的git信息

  1.  
    pod-libray
  2.  
    Subproject commit 4ac42d2f8b9ba0c2f0f2f2ec87ddbd529275fea5

这里是子项目的commit的id,git并不会记录Submodule的文件变动,它是按照这个commit的git来对比Submodule的变动的

这两个文件都需要提交到父项目的git中

我们还可以这样添加Submodule

  1.  
    git add .gitmodules pod-ibrary
  2.  
    git commit -m "pod-library submodule"
  3.  
    git submodule init

修改提交Submodule

首先要确认有对Submodule的commit权限
进入Submodule目录里面,对修改的文件进行提交

cd pod-library/

我们修改了其中的一个文件看下文件的变动

  1.  
    git status
  2.  
     
  3.  
    modified: pod-library/UseAFHTTP.h

commit submodule

git commit -a -m'test submodule'

push 到远端
>git push

然后再回到父目录:

  1.  
    cd ..
  2.  
    git status
  3.  
    on branch master
  4.  
     
  5.  
     
  6.  
    modified: pod-library (new commits)

可以看到pod-library已经变更为最新的commit id

Subproject commit 330417cf3fc1d2c42092b20506b0d296d90d0b5f
我们需要把推送到父项目的远端

  1.  
    git commit -m'update submodule'
  2.  
    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

  1.  
    loning into 'pod-project'...
  2.  
    remote: Counting objects: 57, done.
  3.  
    remote: Compressing objects: 100% (45/45), done.
  4.  
    remote: Total 57 (delta 13), reused 49 (delta 8), pack-reused 0
  5.  
    Receiving objects: 100% (57/57), 18.79 KiB | 0 bytes/s, done.
  6.  
    Resolving deltas: 100% (13/13), done.
  7.  
    Checking connectivity... done.
  8.  
    Submodule 'pod-library' (git@github.com:jjz/pod-library.git) registered for path 'pod-library'
  9.  
    Cloning into 'pod-library'...
  10.  
    remote: Counting objects: 34, done.
  11.  
    remote: Compressing objects: 100% (25/25), done.
  12.  
    remote: Total 34 (delta 8), reused 30 (delta 7), pack-reused 0
  13.  
    Receiving objects: 100% (34/34), 12.95 KiB | 0 bytes/s, done.
  14.  
    Resolving deltas: 100% (8/8), done.
  15.  
    Checking connectivity... done.
  16.  
    Submodule path 'pod-library': checked out '330417cf3fc1d2c
  17.  
     
  18.  
    42092b20506b0d296d90d0b5f'

会自动init Submodule

或者使用第二种方法
先clone父项目

git clone git@github.com:jjz/pod-project.git
cd pod-project
git submodule init

  1.  
    Submodule 'pod-library' (git@github.com:jjz/pod-library.git)
  2.  
    registered for path 'pod-library'

git submodule update

  1.  
    Cloning into 'pod-library'...
  2.  
    remote: Counting objects: 34, done.
  3.  
    remote: Compressing objects: 100% (25/25), done.
  4.  
    remote: Total 34 (delta 8), reused 30 (delta 7), pack-reused 0
  5.  
    Receiving objects: 100% (34/34), 12.95 KiB | 0 bytes/s, done.
  6.  
    Resolving deltas: 100% (8/8), done.
  7.  
    Checking connectivity... done.
  8.  
    Submodule path 'pod-library': checked out '330417cf3fc1d2c42092b20506b0d296d90d0b5f'

删除Submodule

git 并不支持直接删除Submodule需要手动删除对应的文件

  1.  
    cd pod-project
  2.  
    git rm --cached pod-library
  3.  
    rm -rf pod-library
  4.  
    rm .gitmodules
  5.  
     
  6.  
    vim .git/config
  7.  
    [submodule "pod-library"]
  8.  
    url = git@github.com:jjz/pod-library.git
  9.  
    删除submodule相关的内容
  10.  
    git commit -a -m 'remove pod-library submodule'



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

 

posted on 2018-12-22 17:15  freeliver54  阅读(493)  评论(2编辑  收藏  举报

导航