Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample post-receive hook that will "update" another folder (such as a web root) on your server after a push to the master branch
while read oldrev newrev ref
if [[ $ref =~ .*/master$ ]];
echo "Master ref received. Deploying master branch to production..."
git --work-tree=/home3/themes/whatever/web/root --git-dir=/home3/whatever.git checkout -f
echo "Ref $ref successfully received. Thanks and have a great day!"
// In GitHub, when setting up the webhook, use: "Content type: application/x-www-form-urlencoded"
if (!empty($_GET['p']) && $_GET['p'] == 'hippo') { // this makes the script *slightly* more obscure
if ($_POST['payload']) {
try {
$payload = json_decode($_POST['payload']);
} catch(Exception $e) {
file_put_contents('git.log', PHP_EOL.'*****An error occured getting the post payload from Github on: '.date('F j, Y, g:i a').PHP_EOL.PHP_EOL, FILE_APPEND);
file_put_contents('git.log', '--- Post payload push recieved: '.$payload->commits[0]->timestamp.' ---'.PHP_EOL, FILE_APPEND);
if ($payload->ref === 'refs/heads/master') {
`git fetch --all`;
`git reset --hard origin/master`;
file_put_contents('git.log', 'Master branch deployed to production. Log message: '.$payload->commits[0]->message.PHP_EOL, FILE_APPEND);
} else {
file_put_contents('git.log', 'The push from GitHub was to a branch other than master.'.PHP_EOL, FILE_APPEND);
} else {
file_put_contents('git.log', PHP_EOL.'*****This scipt was touched, but no post payload was sent. On this date: '.date('F j, Y, g:i a').PHP_EOL.PHP_EOL, FILE_APPEND);
} // no POST "payload" variable sent. check that repo is set to send 'application/x-www-form-urlencoded' data.
} // check if we have GET var 'hippo'

Push to Deploy

Do you want a PHP post-receive hook connected to GitHub with a GitHub webhook, or, a post-receive bash script hosted on your own server? This repo contains both.

  1. cd into your project's directory on your server or into your .git folder on your server.
  2. Get the PHP post-receive file from this Gist. PHP: wget Git post-recieve hook: wget
  3. Make sure the file can be executed ("chmod +x filename").
  4. If using the PHP version, make sure that you exclude the file (post-receive.php) in your .gitignore file.
  5. If using the git post-recieve hook, make sure to edit the paths in the file, relating them to your own project.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment