Last Updated: February 25, 2016
·
366
· MidnightLightning

Git Post-Receive push to live

If you have a remote GIT repository on the server hosting your live site, and want any push to the master branch to be made live, here's a post-receive hook script to do so:

Place this script as post-receive in the .git/hooks folder of the repository on the remote machine:

#!/bin/sh

while read oldrev newrev ref
do
    echo "# Analyzing $ref"
    if [ "$ref" = "refs/heads/master" ]
    then
        echo '#'
        echo '# Copying to live'
        echo '#'

        GIT_WORK_TREE='/path/to/doc/root' git checkout -f
    fi
done

This will only copy updates pushed to the master branch to the given /path/to/doc/root folder, letting you keep several feature branches in the repository too, without interfering.