2012/07/14

git remote リポジトリを複数登録する

[git]

git remote は複数登録することができる。案外、知られていないようなのでこんな風に使ってるよ、というメモ。

remote add

例えば、以下のようにローカルのリポジトリを origin に設定している環境があるとして、

git remote -v
origin file:///home/bayashi/git-repos/Foo.git (fetch)
origin file:///home/bayashi/git-repos/Foo.git (push)

以下のように、bb というラベルで Bitbucket を remote に登録する

git remote add bb git@bitbucket.org:bayashi/foo.git

リモートが2つになりました。

git remote -v
bb  git@bitbucket.org:bayashi/foo.git (fetch)
bb  git@bitbucket.org:bayashi/foo.git (push)
origin file:///home/bayashi/git-repos/Foo.git (fetch)
origin file:///home/bayashi/git-repos/Foo.git (push)

以下のように remote のラベルを指定して push することで、Bitbucket に push することができます。

git push bb branch

手元で開発をはじめて、途中から Bitbucket に上げはじめた時にこういうことをしてます。Bitbucket は無料でプライベートリポジトリがもてるので、バックアップ代わりに上げといたりする。

さらに remote add してみる

remote はさらに登録することもできます。

例えば、公開で github にあげてもいいんじゃね? ってなったときに、 github もリモートに追加します。

git remote add gh git@github.com:bayashi/Foo.git

git remote -v
bb  git@bitbucket.org:bayashi/foo.git (fetch)
bb  git@bitbucket.org:bayashi/foo.git (push)
gh  git@github.com:bayashi/Foo.git (fetch)
gh  git@github.com:bayashi/Foo.git (push)
origin file:///home/bayashi/git-repos/Foo.git (fetch)
origin file:///home/bayashi/git-repos/Foo.git (push)

ローカルに push する場合

git push origin branch

bitbucket に push

git push bb branch

github に push

git push gh branch

remote の設定を削除する

ラベルを指定して、remote の設定を削除します。

git remote rm gh

しめ

remote を複数登録するというのは、実はあまり知られてないっぽいですが、つかってみると楽できる場面は結構あると思うので是非お試しを。

サイト内検索