git submodule 子模組應用

weixin_34148340發表於2018-07-12

經常有這樣的事情,當你在一個專案上工作時,你需要在其中使用另外一個專案。也許它是一個第三方開發的庫或者是你獨立開發和並在多個父專案中使用的。這個場景下一個常見的問題產生了:你想將兩個專案單獨處理但是又需要在其中一箇中使用另外一個。

在Git 中你可以用子模組submodule來管理這些專案,submodule允許你將一個Git 倉庫當作另外一個Git 倉庫的子目錄。這允許你克隆另外一個倉庫到你的專案中並且保持你的提交相對獨立

在專案中遇到的問題

專案中通過composer安裝來自segmentFault的markdown語言解析器hyper-down外掛,版本為"joyqi/hyper-down": "@dev"

$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing joyqi/hyper-down (dev-master 2fad8f9): Cloning 2fad8f9c61 from cache
Writing lock file
Generating autoload files

用git add composer安裝自動生成相應的vendor目錄時產生以下報錯

$git add vendor/
warning: adding embedded git repository: vendor/joyqi/hyper-down
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint:   git submodule add <url> vendor/joyqi/hyper-down
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint:   git rm --cached vendor/joyqi/hyper-down
hint:
hint: See "git help submodule" for more information.

原因是composer安裝外掛的時候會clone segmentFault在github程式碼庫中的程式碼,此處再新增到版本控制就會報版本庫已經存在

報錯資訊提出瞭解決辦法,就是通過新增子模組來進行版本控制

解決步驟

  • 先刪除 vendor/joyqi/hyper-down下的git 快取
 $ git rm -rf --cached vendor/joyqi/hyper-down                                   
 rm 'vendor/joyqi/hyper-down'
  • 建立子模組關聯
$ git submodule add https://github.com/SegmentFault/HyperDown.git vendor/joyqi/hyper-down
Adding existing repo at 'vendor/joyqi/hyper-down' to the index

然後可以看到根目錄下多了一個.gitmodules的檔案:

[submodule "vendor/joyqi/hyper-down"]
   path = vendor/joyqi/hyper-down
   url = https://github.com/SegmentFault/HyperDown.git
  • 檢視當前git的改動資訊
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   .gitmodules
        new file:   vendor/autoload.php
        new file:   vendor/composer/ClassLoader.php
        new file:   vendor/composer/LICENSE
        new file:   vendor/composer/autoload_classmap.php
        new file:   vendor/composer/autoload_namespaces.php
        new file:   vendor/composer/autoload_psr4.php
        new file:   vendor/composer/autoload_real.php
        new file:   vendor/composer/autoload_static.php
        new file:   vendor/composer/installed.json
        new file:   vendor/joyqi/hyper-down

執行 git add && git commit && git push

程式碼庫

  • GitHub程式碼庫的結構如下
vendor
  - composer
    - ...
      ...
      ...
  - joyqi
    - [hyper-down @ 2fad8f9](https://github.com/SegmentFault/HyperDown/tree/2fad8f9c61a984c9ba61616d8f09aa37054c7563 )
  autoload.php
.gitmodules
...
...

客戶端使用

法一:克隆父專案,再更新子模組
  • 客戶端 git clone 得到 vendor\joyqi\hyper-down 是空資料夾,需要匯出子模組的程式碼方可正常使用

  • 檢視子模組資訊

$ git submodule
-2fad8f9c61a984c9ba61616d8f09aa37054c7563 vendor/joyqi/hyper-down 

子模組前面有一個-,說明子模組檔案還未檢入(空資料夾)

  • 初始化子模組
$ git submodule init
Submodule 'vendor/joyqi/hyper-down' (https://github.com/SegmentFault/HyperDown.git) registered for path 'vendor/joyqi/hyper-down'

初始化模組只需在克隆父專案後執行一次

  • 更新子模組
$ git submodule update
Cloning into 'C:/Project/test/new/vendor/joyqi/hyper-down'...
Submodule path 'vendor/joyqi/hyper-down': checked out '2fad8f9c61a984c9ba61616d8f09aa37054c7563'
法二 :遞迴克隆整個專案
  • 在git clone的時候新增遞迴引數 --recursive
$ git clone http://github.com/HongXunPan/submodule.git --recursive new
Cloning into 'new'...
warning: redirecting to https://github.com/HongXunPan/submodule.git/
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 20 (delta 2), reused 20 (delta 2), pack-reused 0
Unpacking objects: 100% (20/20), done.
Submodule 'vendor/joyqi/hyper-down' (https://github.com/SegmentFault/HyperDown.git) registered for path 'vendor/joyqi/hyper-down'
Cloning into 'C:/Project/test/new/vendor/joyqi/hyper-down'...
remote: Counting objects: 400, done.
remote: Total 400 (delta 0), reused 0 (delta 0), pack-reused 400
Receiving objects: 100% (400/400), 225.11 KiB | 161.00 KiB/s, done.
Resolving deltas: 100% (244/244), done.
Submodule path 'vendor/joyqi/hyper-down': checked out '2fad8f9c61a984c9ba61616d8f09aa37054c7563'
執行完可以看到vendor/joyqi/hyper-down資料夾下已經checkout所有檔案了

相關文章