经常有这样的事情,当你在一个项目上工作时,你需要在其中使用另外一个项目。也许它是一个第三方开发的库或者是你独立开发和并在多个父项目中使用的。这个场景下一个常见的问题产生了:你想将两个项目单独处理但是又需要在其中一个中使用另外一个。
在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'