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. Nice post. I was checking constantly this blog and I am impressed! Extremely helpful information specifically the last part :) I care for such information a lot. I was seeking this particular info for a very long time. Thank you and good luck.

    Reply
  2. great issues altogether, you just received a new reader. What would you recommend in regards to your publish that you just made a few days ago? Any positive?

    Reply
  3. Your mode of explaining all in this piece of writing is truly nice, every one be capable of without difficulty know it, Thanks a lot.

    Reply
  4. Hmm is anyone else experiencing problems with the images on this blog loading? I'm trying to find out if its a problem on my end or if it's the blog. Any responses would be greatly appreciated.

    Reply
  5. Fantastic post but I was wondering if you could write a litte more on this topic? I'd be very thankful if you could elaborate a little bit more. Bless you!

    Reply
  6. Simply wish to say your article is as surprising. The clearness to your put up is just nice and i could think you are a professional on this subject. Fine along with your permission allow me to take hold of your feed to keep up to date with impending post. Thanks 1, 000,000 and please carry on the rewarding work.

    Reply