git submodule

http://d.hatena.ne.jp/woremacx/20080308/1204986198のように、gitで外部レポジトリを扱えるようにする方法。

外部レポジトリの追加

git submodule addすると、外部レポジトリをサブモジュールとして取り込めるようになります。

# cloneする
$ git clone git://example.com/repos/private/
$ cd private

# git://example.com/repos/external/を追加する
$ git submodule add git://example.com/repos/external/

# commitしておく
$ git commit -m "Add submodule"
$ git push

外部レポジトリ内での作業

外部レポジトリで作業したときは、そこでコミットするほかに、上位の自分のレポジトリでもコミットしておく必要がある。自分のレポジトリと外部レポジトリのリビジョンの対応関係を覚えておくためらしい。

# 外部レポジトリでいろいろ作業する
$ cd external
$ echo foo >> foo.txt
$ git add foo.txt
$ git commit -m 'some text...'
$ git push

# 自分のレポジトリでもコミットする
$ cd ..
$ git add external
$ git commit -m 'update submodule'
$ git push

git cloneしたときの作業

自分のレポジトリをコピーしただけでは、サブモジュールはコピーされません。git submodule initとgit submodule updateをする必要がある。

# 別のマシンで
$ git clone git://example.com/repos/private/
$ cd private

# externalの中はcloneされない
$ ls -a
. ..

# git submodule init/git submoudle updateするとcloneされる
$ git submodule init
$ git submodule update

また、これだけではbranchがmasterになっていないので、masterに切り替える必要がある。

# externalはbranchが(no branch)になっている
$ cd external
$ git branch
* (no branch)
  master

# branchをmasterにする
$ git checkout master
...
Switched to branch "master"

参考