まじめにゆいがどくそん

仕事関連の真面目な記事が満載です、多分。

リモートリポジトリへの git push に失敗した場合 (remote rejected)

現象

ローカルのgitリポジトリへ commit したあとに リモートリポジトリへの git push で下記のエラーが発生

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

対策

リモートリポジトリの設定を確認

$ git config --get-all core.bare
false

「bare」ではないので、可能ならば「bare」にする。

$ git config --bool core.bare true

リモートリポジトリの設定を再確認

$ git config --get-all core.bare
true

注意

「git push できない」で検索すると、「git config --add receive.denyCurrentBranch ignore」を勧めるサイトがいくつかヒットするが、git config --add receive.denyCurrentBranch ignoreはどう危険なのか - 西尾泰和のはてなダイアリーでその危険性が述べられている。

上記の記事には「bare」なリポジトリに変更できない場合の対応方法も記載されている。

最後に

git初心者なのでツッコミ大歓迎〜