Git post receive hooks

If like me you are a developer and you use got locally. You might also be like me in that you use git to “push to production”.

When doing this, you need to have a git post receive hook. Inside that hook might be something as simple as

GIT_WORK_TREE=/some/where/in your/server git commit -f

However this only works for your website source code, but what if you use a framework, or like me have one you also want to deploy and keep fresh and up to date? Then you have to do something else.

In the same git post receive hook, you can do the following after the above statement.

cd /some/where/in your/server/framework
env -i git pull origin master

And this will do the website update and checkout, followed by your framework code to!!

The env -i is important because it clears your environment variables and this is important because fit will try to use GIT_DIR whenever possible and of course your post receive hook sets this automatically which would cause your git pull to fail…

Hope it helps!

Leave a Reply

Your email address will not be published. Required fields are marked *