git push production

I have several sites that I manage in one form or another, and a couple years ago I made a huge move from sending files over FTP to maintaining a remote repository on the server. Originally Mercurial - now Git. Still, same concept:

  1. Push to a central repository like Bitbucket or GitHub.
  2. Create a local clone on your production server in your /var/www directory or whatever
  3. When you make a new change, commit it locally…
  4. … push to GitHub …
  5. … ssh into your production server …
  6. Do a git pull from GitHub

This is better than FTP, but still isn’t ideal. First, it puts either an .hg or .git folder in the root of your production code. But also, it comes with the added step of having to SSH into your server.

With all the wonderful things Heroku can do with their git push heroku master, I figured I’d go looking for a better solution. I found it here:

Here’s the core of the post:

  1. Make sure you can SSH into your server with your private key. Plenty of articles on how to do that. So in a terminal you should be able to type: ssh and it’ll log you in without asking for a password.
  2. mkdir website.git && cd website.git
  3. git init --bare
  4. Create a new post-receive hook in git by creating a file in hooks/post-receive with this:
    GIT_WORK_TREE=/var/www/ git checkout -f
  1. Make sure it’s executable: chmod +x hooks/post-receive
  2. Then on your local repository: git remote add web ssh://
  3. And the key part: git push production +master:refs/heads/master

Now, from your local git repository, you can do this:

git push production

And your changes automatically move to the live server!