Tuesday, October 14, 2008

Cloning a local git repository into a remote host

リモートリポジトリをローカルにクローンする方法は知っているけど、ローカルリポジトリのクローンをリモートに作る方法がいまいちわからなかったので調べてみました(より標準的な方法を知っているひとはコメントください)。

まずは、ローカルにベアリポジトリのクローンを作成してアーカイブ。
 $ git clone --bare -l foo/.git foo.git
$ tar czf foo.git.tar.gz foo.git

これをリモートにコピーして展開すればOK

あとは、ローカルの clone 元の .git/config に以下の行を追加
[remote "origin"]
url = ssh://name.of.host/path/to/repos.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

git push はデフォルトで "origin" リポジトリのすべてのブランチを push するので、.git/config に remote "origin" のセクションがあれば、その url と fetch として記述されている refspec のマッピングを利用します。

また、 git pull はデフォルトで "origin" リポジトリから、 .git/config の branch セクションの remote が pull するリポジトリと一致するブランチを pull する (たぶん)。

よって、上記の設定により "git push", "git pull" だけで svn commit/update 相当の操作ができる。

ちなみに、これらの設定は
 git clone ssh://name.of.host/path/to/repos.git 
で得られるクローンリポジトリの設定と同じです。

No comments: