gitでリモートレポジトリをbareに変更

gitでローカルのディレクトリから別のディレクトリへcloneして作業した後, pushしようとしたところ, 以下のようなエラーが出てしまい, pushできなかった.

error: refusing to update checked out branch: refs/heads/master
error: By default, updating the current branch in a non-bare repository
error: is denied, because it will make the index and work tree inconsistent
error: with what you pushed, and will require 'git reset --hard' to match
error: the work tree to HEAD.
error:
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
error:
error: To squelch this message and still keep the default behaviour, set
error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.

http://wiki.hituzi.jp/wiki.cgi?page=Git%A5%E1%A5%E2

調べてみるとエラーに書かれている通り, push先のレポジトリ(元々のレポジトリ)が"bare"でないことが原因らしい.

この時まで僕はbareと言う事の意味を知らなかったのだが, リモートレポジトリはbareレポジトリと呼ばれる作業ファイルがない管理情報だけのgitリポジトリである必要があるらしい. bareレポジトリはinitするときか, cloneするときに--bareオプションをつければOK.

問題はすでにinitもcloneもしちゃっていることである. というわけで関係を修復するために以下のようにした.

$ git clone --bare /path/to/original/repo /path/to/new/repo.git
$ cd /path/to/cloned/repo
$ git config remote.origin.url
/path/to/original/repo
$ git config remote.origin.url /path/to/new/repo.git
$ git config remote.origin.url
/path/to/new/repo.git
$ git push

これでOK. 以降は/path/to/new/repo.git*1からcloneするようにすれば良い.

http://hakobe932.hatenablog.com/entry/20080521/1211373969
http://tobysoft.net/wiki/index.php?git%2F%A5%EA%A5%E2%A1%BC%A5%C8%A5%EA%A5%DD%A5%B8%A5%C8%A5%EAURL%A4%F2%CA%D1%B9%B9%A4%B9%A4%EB%CA%FD%CB%A1

追記:

ここではcloneしてbareレポジトリを作成し, pushしているが, initで空のbareレポジトリを作ってそれにpushした方が良さそうです.

*1:これも知らなかったが, bareレポジトリのディレクトリには~.gitのように名づけるのが慣例みたい.