Using Git Hooks to delegate routine tasks

My team currently uses GitHub as the center of our development process, taking full advantage of Pull Requests and all that. That means we use git and our process is to always develop using feature-branches, making PRs for each feature and then merging them in after code review.

This means we switch branches a lot, either to see someone’s code and review it or to kickoff a new branch for a new feature. Over time this scenario seemed to happen a few times:

  • Switch to master and pull in updates
  • Start working in a new feature in a new branch
  • Write something and run tests or view the site
  • OMG!! everything is broken!
  • turn to team: “I’m getting an error on the stuff you added, did you get it?”
  • team says: “No”
  • me: “ah dammit, did not pull in required dependencies” or “ah dammit, I did not regenerate assets” (this may or not be preceded by hours of pointless debugging)

Basically I keep forgetting that every time I switch branches I should get up-to-date dependencies and compile a new CSS to get all the latest shiny. So in order to make this automatic I decided to setup a git hook that did that for me.

Creating a Git Hook

Git has lots of hook points , so just pick one that fits your flow. In my case I picked the post-checkout hook, since I want this to happen every time I switch to another branch.

To create a new hook, you need to create a file in the .git/hooks folder with the name of the hook you want to attach to, in my case post-checkout, make sure this file is executable and then code your hook in shell script format.

chmod +x .git/hooks/post-checkout

Writing a on-demand hook

Sometimes you just flip between branches to check up on things and get some updated code, so you may not always need to run composer and install assets, so I also baked in a confirmation step to allow me to abort in case I’m doing something else.

For now all I needed was to run Composer and generate my assets (less to css and such), so this is what I got:


# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty

while true; do
    read -p "Run Composer install and Assetic Dump? [Y/n]: " yn
    case $yn in
        [Yy]* ) composer install; php app/console assetic:dump; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;

Wrap up

Git Hooks are pretty cool, if your workflow is build around git like lots of team do nowadays the hooks are a cool place to tie in your day to day stuff that tends to be forgotten and generate unneeded debugging and such. Take a look at available hooks and think about your routine, I’m pretty sure you will find something you can delegate out to git and stop worrying about it everyday.

comments powered by Disqus

Related Posts

php|tek09: Day #1

php|tek09: Day #1

  • May 20, 2009

Tutorial day at php|tek! The day begun with a PHP Breakfast, where I met guys like Matthew Weier O’Phinney and saw the guys from yesterday as well.

Read More
BlogBlogs Userinfo Plugin: correção de bug

BlogBlogs Userinfo Plugin: correção de bug

  • April 28, 2008

Hoje pela manhã identifiquei um antigo bug do plugin que causava instabilidade no cache e algumas vezes erros de “carregando pra sempre”.

Read More
Novo Artigo: AJAX - O que é? Como funciona?

Novo Artigo: AJAX - O que é? Como funciona?

  • October 9, 2006

Publiquei hoje meu primeiro artigo de uma série de artigos que irá explicar AJAX desde on início, em teoria e na prática procurando assim dar capacidade para qualquer um entender como usar e onde usar essa nova tecnologia que é tendência na web atual.

Read More