Search code examples
gitdeploymentgithooksgit-push

Git post-receive hook for push-to-deploy .. I want to checkout last pushed branch


I am able to implement push-to-deploy with Git by creating a bare repo on the server, and pushing to that from my dev. Below is my post-receive which handles updating the live site:

#!/bin/sh

echo
echo "*** Checking out branch [post-receive hook]"
echo

git --work-tree=/var/www/myproject checkout -f master

Notice that it checkouts "master" branch this works great in most cases. "master" is the branch I push (e.g. git push staging master when pushing to live to the staging server). However, I sometimes like to push a branch only without merging my changes to master (e.g. git push staging new-register-form). Can I change my post-receive hook script to checkout "the last pushed branch"? Is this possible?


Solution

  • Yes, you can, like this:

    #!/bin/bash
    while read oldrev newrev refname
    do
        branch=$(git rev-parse --symbolic --abbrev-ref $refname)
        git --work-tree=/var/www/myproject checkout -f $branch
    done
    

    taken from: here