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代码库中的代码,此处再添加到版本控制就会报版本库已经存在

报错信息提出了解决办法,就是通过添加子模块来进行版本控制

解决步骤

然后可以看到根目录下多了一个.gitmodules的文件:

[submodule "vendor/joyqi/hyper-down"]
    path = vendor/joyqi/hyper-down
    url = https://github.com/SegmentFault/HyperDown.git

执行 git add && git commit && git push

代码库

客户端使用

##### 法一:克隆父项目,再更新子模块

子模块前面有一个-,说明子模块文件还未检入(空文件夹)

初始化模块只需在克隆父项目后运行一次

##### 法二 :递归克隆整个项目

##### 执行完可以看到vendor/joyqi/hyper-down文件夹下已经checkout所有文件了

Responses
  1. Hi to every single one, it's really a fastidious for me to pay a quick visit this site, it contains precious Information.

    Reply
  2. Hi! Do you use Twitter? I'd like to follow you if that would be okay. I'm definitely enjoying your blog and look forward to new posts.

    Reply
  3. Thanks designed for sharing such a nice thought, article is nice, thats why i have read it fully

    Reply
  4. Онлайн смотреть фильм Странники терпенья онлайн в хорошем качестве посмотреть предлагаем смотреть фильмы и сериалы в хорошем качестве

    Reply
  5. Смотреть фильм онлайн Наследники посмотреть в хорошем качестве онлайн предлагаем смотреть фильмы и сериалы в хорошем качестве

    Reply
  6. generic cialis canada cialis canada cialis pill

    Reply