Using Git to deploy and update a website Pt. 2

By

Previously I wrote Tutorial: How to use Git to deploy and update a website Pt. 1, I was able to simultaneous push my changes seamlessly from my local machine to my git server, and web server.

It worked great, and reduced my deployment time significantly. Now I am taking it a step further. I want to push two different branches to different locations on my remote server. There are two branches on my local machine Master, and Beta.

With this twist, I will be able to push the Master branch to http://example.com, and the Beta branch to http://beta.example.com. Let’s start by creating the Beta Branch from your existing directory on the local machine.

git branch checkout -b beta

After that, SSH to the GIT Server, and navigate to the Git repository folder, example: /home/project/website.git/.

Then create a post-receive hook that checks out the Master and Beta branch into the respectable web server’s DocumentRoot.

vi hooks/post-receivecat >
#!/bin/bashwhile read oldrev newrev refdo  branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ];
then    git --work-tree=/path/under/root/dir/live-site/ checkout -f $branch    
echo ''Changes pushed live.''  
fi  
if [ "dev" == "$branch" ];
then    
git --work-tree=/path/under/root/dir/dev-site/ checkout -f $branch    
echo ''Changes pushed to dev.''  
fi done

And that’s it! Easy as pie. 0.o