Garrett Murphey

Ruby & JavaScript Development

Running Grunt Tasks with Git Pre-Commit Hooks

December 22, 2012

I’m always forgetting to run release tasks when committing to git, and then I’m forced to either rewrite history with rebase or roll back and re-commit. It’s a big time sink. Thankfully, I’ve been playing with Grunt tasks and git pre-commit hooks and found a way to not only save time and my sanity, but take release tasks out of my manual workflow entirely. This hook will stash any work that’s not staged, run the release task, stage any files created or updated by the grunt task and restore any stashed files. If you aren’t using a release task, this will work equally well with grunt jshint or any testing tasks you may have defined.

In the pre-commit hook (.git/hooks/pre-commit):

# stash unstaged changes, run release task, stage release updates and restore stashed files

NAME=$(git branch | grep '*' | sed 's/* //')

# don't run on rebase
if [ $NAME != '(no branch)' ]
  git stash -q --keep-index
  grunt release


  if [ $RETVAL -ne 0 ]
    exit 1

  git add .
  git stash pop -q

And in your Grunt config:

grunt.registerTask('release', ['lint', 'qunit', 'concat', 'min']);

Never commit linting errors or failing tests again, and let git save your sanity with automated grunt tasks.

Other Recent Posts