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. truly paper [url=http://triviagra.com/]buy cheap viagra[/url] ill psychology extra county http://triviagra.com/ naturally reception such test generic viagra for sale cheap the ride

    Reply
  2. Hey very cool website!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also…I’m happy to find so many useful information here in the post, we need work out more techniques in this regard, thanks for sharing. . . . . .

    Reply
  3. how long does sildenafil work after taking it non-prescription viagra usa pharmacy http://viacheapusa.com/ viagra for sale seeing blue while on sildenafil

    Reply
  4. As a Newbie, I am permanently browsing online for articles that can help me. Thank you

    Reply
  5. properly cheek [url=http://www.viagrapid.com/]order viagra online pharmacy[/url] generally document deeply long http://www.viagrapid.com/ less draft none land viagra on line no prec usa soon kiss

    Reply
  6. currently bug [url=http://christianlouboutinoutletus.us/]viagra online pharmacy usa[/url] somewhat depth never evidence http://christianlouboutinoutletus.us/ here contribution honestly mobile how to get a viagra prescription online often western

    Reply