post-receive hook and branches

Installing a post-receive hook in a git repository can be used to automate a lot
of things, i.e. initiate deployment, precompile a static page, or similar. Using
a hook is quite easy, simply create an executable file named like the hook inside
the hooks folder. So here we go

Init a bare repository

$ mkdir ~/test.git && cd ~/test.git
$ git init --bare

Init a repository

$ mkdir ~/test && cd ~/test
$ git init
$ git remote add origin ~/test.git

Create a hook

$ touch ~/test.git/hooks/post-receive
$ echo 'echo "I am a post receive hook"' >> ~/test.git/hooks/post-receive
$ chmod +x ~/test.git/hook/post-recive

Pushing to the repository means the hook is executed

$ cd ~/test
$ touch foo.txt
$ git add foo.txt
$ git commit -m 'foo'
$ git push origin master
...
remote: I am a post receive hook
...

To do more fun stuff it is sometimes important to know which branch got pushed,
to do this a couple of things are passed to the hook via stdin, so

https://gist.github.com/sideshowcoder/5751530.js

to detect the branch simply check against the $ref.

Happy hooking ;)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s