LoginSignup
4

More than 5 years have passed since last update.

git pushでVPSにデプロイするには`git config receive.denyCurrentBranch updateInstead`で

Last updated at Posted at 2016-03-29

git pushでVPS上のHTMLを更新する方法です。通常、gitではリモートはbareリポジトリにし、そこに対してローカルからgit pushします。bareリポジトリはgitの変更履歴等のみを保持するリポジトリで、チェックインしたファイルがそのまま配置されるわけでないです。scp的な感覚でgit pushしてリモートのコードを変更したい場合は、non-bareなリポジトリをリモートに作ります。

前提

前提として、複数人でgit pushしない運用になっていることです。git config --add receive.denyCurrentBranch ignoreはどう危険なのか - 西尾泰和のはてなダイアリーを参照。個人的なサイトであれば問題ないと思います。

リモートにnon-bareなリポジトリを作る

cd /var/www/sites-available/
git init example-site.com
git config --local receive.denyCurrentBranch updateInstead

receive.denyCurrentBranch updateInsteadをセットしておかないと次のようなエラーになりgit pushできません。

Counting objects: 368, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (327/327), done.
Writing objects: 100% (368/368), 76.40 MiB | 1.97 MiB/s, done.
Total 368 (delta 35), reused 0 (delta 0)
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'.

ローカルからpushする

git remote add origin example.com:/var/www/sites-available/example-site.com
git push -u origin master

ここのexample.com:/var/www/sites-available/example-site.comの部分は、ホスト名:.gitがあるディレクトリのパスにします。

ホスト名の部分は、ローカルマシン(OSXなど)の$HOME/.ssh/configに設定したホスト名です。

~/.ssh/configの例
Host example.com
  User bob
  Port 2222

まとめ

  • 個人的なサイトなのでscp感覚でgit pushでサーバにソースをデプロイしたい
  • リモートはgit config --local receive.denyCurrentBranch updateInsteadでnon-bareなリポジトリを作る
  • ローカルはgit remote add origin example.com:/var/www/sites-available/example-site.comのようにしてremoteを追加しておく

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4