Last Updated: February 25, 2016
·
2.469K
· zigotica

Git post-receive hook to save branches into different folders

What if we want to work on a project, show demo to client from a specific folder and at the same time, be able to push other branches to the same repository and serve them to hidden folders?

#!/bin/sh
#
# modified by zigotica from 
# http://www.ekynoxe.com/git-post-receive-for-multiple-remote-branches-and-work-trees/
# way more flexible now!

while read oldrev newrev ref
    do
    branch=`echo $ref | cut -d/ -f3`

    if [ "master" = "$branch" ]; then
        folder="current"
    else
        folder="${branch}"
    fi

    ROUTE="/usr/share/nginx/somewhere"
    REPO="reponame"
    FINAL="${ROUTE}/${REPO}/${folder}"
    git --work-tree=${FINAL} checkout -f $branch
done

You will need to give your git user permissions to write to those folders, for instance:

chown git.git branchpath

Enjoy!