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 subModule(子模組)Git
- Git 子模組Git
- Git應用詳解第十講:Git子庫:submodule與subtreeGit
- git 子模組使用方法Git
- git 子模組使用小結Git
- git submodule小記Git
- Git使用之submoduleGit
- Git submodule使用指南(一)Git
- Git submodule使用指南(二)Git
- 如何在大型專案中使用Git子模組開發Git
- 手把手教你把 Git 子模組更新到主專案Git
- JAVA單子模式(static 應用)Java模式
- 將git倉庫從submodule轉換為subtreeGit
- Git 分支策略與submodule對分支策略的影響Git
- Yii2 之 frontend 子模組實踐之一:新增前後臺子模組
- Git的基本應用Git
- SpringBoot + maven 父子模組專案搭建Spring BootMaven
- SpringBoot載入子模組配置檔案的方法Spring Boot
- Linux 安裝配置應用- GitLinuxGit
- git 和 gitlub 中應用整理Git
- Git應用詳解第九講:Git cherry-pick與Git rebaseGit
- 用 Git 作為聊天應用的後端Git後端
- 2019-01-17 submodule
- maven如何快速更新子模組專案的版本號Maven
- Git應用詳解第八講:Git標籤、別名與Git gcGitGC
- Python之外掛模式(使用import_module匯入子模組)Python模式Import
- [Python]OS模組應用Python
- MyBatis 應用的組成MyBatis
- 【git實際應用填坑解決】Git
- Git應用詳解第六講:Git協作與Git pull常見問題Git
- 使用Jenkins + git submodule 實現自動化編譯,解決程式碼安全性問題JenkinsGit編譯
- Java 組合模式及其應用Java模式
- PDM應用模組設計
- Git 學習以及建立第一個應用Git
- 盒子模型模型
- gitlab webhook 如何針對一個專案下的子模組配置GitlabWebHook
- 基於Barra多因子模型的組合權重優化模型優化
- 013:列表和元組的應用