git submodule 子模組應用
經常有這樣的事情,當你在一個專案上工作時,你需要在其中使用另外一個專案。也許它是一個第三方開發的庫或者是你獨立開發和並在多個父專案中使用的。這個場景下一個常見的問題產生了:你想將兩個專案單獨處理但是又需要在其中一箇中使用另外一個。
在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所有檔案了
相關文章
- Git應用詳解第十講:Git子庫:submodule與subtreeGit
- 聊聊Git subModule(子模組)Git
- Git使用之submoduleGit
- Git submodule的使用Git
- git submodule小記Git
- Git submodule 的筆記Git筆記
- Git submodule使用指南(二)Git
- Git submodule使用指南(一)Git
- GIT SUBMODULE在Android中的使用GitAndroid
- 將git倉庫從submodule轉換為subtreeGit
- Git 分支策略與submodule對分支策略的影響Git
- 【Git】Git reset/revert的應用Git
- Git的基本應用Git
- Git 子模組Git
- git filter-branch應用GitFilter
- PDM應用模組設計
- js path模組的應用JS
- [Python]OS模組應用Python
- git 和 gitlub 中應用整理Git
- Git 子庫使用Git
- Git應用詳解第九講:Git cherry-pick與Git rebaseGit
- 用 Git 作為聊天應用的後端Git後端
- Linux 安裝配置應用- GitLinuxGit
- Git應用詳解第八講:Git標籤、別名與Git gcGitGC
- pickle模組 collections模組在物件導向中的應用物件
- 原生應用新增 Flutter 模組依賴Flutter
- 如何將模組化應用於 SQLSQL
- git 子模組使用小結Git
- git 子模組使用方法Git
- 【git實際應用填坑解決】Git
- 第十三章:應用函子
- Git應用詳解第六講:Git協作與Git pull常見問題Git
- Django建立app應用和admin模組DjangoAPP
- Flask 中模組化應用的實現Flask
- Nginx的HTTP模組與Stream模組:區別與應用場景NginxHTTP
- 如何用JGit管理Git子模組Git
- 什麼是git subcommand,如何建立git子命令?Git
- Git 學習以及建立第一個應用Git