Using Git and a post-receive hook script for auto deployment

Set up a remote Git repositiory to automatically checkout to a working tree after a git push command. Useful for the auto deployment of web site files on a web server.

Create a bare GIT repository:

mkdir project.git && cd project.git
git init --bare

Create a detached work tree:

mkdir /home/user/myproject
git config core.worktree /home/user/myproject
git config core.bare false
git config receive.denycurrentbranch ignore

Example hooks/post-receive script:

#!/bin/sh
git checkout -f

Now when you push to your remote repository, the post-receive script will run a checkout of the latest files to you working tree.

5 thoughts on “Using Git and a post-receive hook script for auto deployment

  1. Pingback: Deploying node.js apps - Clock Blogs

  2. Thanks for this, works a treat and its been a real lifesaver.

    A quick note: I just spent 20 minutes cussing at my terminal window trying to figure out why, when I ran these commands yesterday as an experiment, they worked fine. Today I ran them and after I set core.bare to false I received:
    fatal: core.bare and core.worktree do not make sense

    As it turns out, today I’d tried to run the commands in the order above, whereas yesterday, I set core.bare first before I set core.worktree. Running the commands in that order gave the desired result. This was in Git 1.7.5

  3. When I try to push, I’m getting this error:

    fatal: You are on a branch yet to be born

    I’m on git 1.7.6 and I’m using the steps above to try to deploy after a push. Any thoughts?

  4. Pingback: Git Website Deployment Useful Sites | Ycsoftware.net

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>