A beginner’s guide to git hooks.

Gokul Chandrasekaran Blocked Unblock Follow Following May 15, 2016

Git hooks if used right can improve productivity by a good amount.

The motivation of this blog is to provide a comprehensive overview for people like me who tried scouring the Internet to learn about git hooks.

Git hooks come inbuilt with Git, they are essentially scripts that run before or after certain specific events. Git hooks are quite powerful. They can be used for a number of tasks like validating commits, automating builds to name a couple. Git hooks are of two types :

Client-side — These run on the developer’s system

Server-side — These run on the server hosting the Git repository

We will primarily be covering client-side hooks in this blog.

A poll I came across on Javascript Weekly Issue #278 showed that :

74% of developers across the world use console statements for debugging.

Being among that group of the population, I have the habit of leaving ‘console’ and ‘debugger’ statements in my javascript code during commits, thus riddling the repository with them and annoying the other developers collaborating on the project. This is where git hooks came handy for me.

Let me walk through the steps I followed for writing my first git hook.

The hook scripts can be found in the .git/hooks folder of your git project. They can be identified by the event during which they have to be triggered. For example: post-commit hook runs after the commit task is performed.

The entire list of hooks are available here :

The default examples in the hooks directory are written in bash and come with a ‘.sample’ extension. The advantage of git-hooks is that they can be written in any scripting language. My choice of scripting was python, as I have a little background in python. But one can use any weapon of choice for writing their hooks. The scripting language used must just specified at the top of the script like this.

#!/bin/sh

Here is the python script that I wrote for the pre-commit hook. It reads through every file in the repo reading and removing debugger and console statements before committing to the repository.

#!/usr/bin/python

import os

import glob

import fileinput

from subprocess import call

import re

path = ‘src/’

textToSearch = ‘debugger;’

textToReplace = ‘’

for folder in [x[0] for x in os.walk(path)]:

for filename in glob.glob(os.path.join(folder, ‘*.js’)):

file = fileinput.FileInput(filename, inplace=True)

for line in file:

print(re.sub(“(console.(log|debug|info|count)\((.*)\)?)|(debugger(;)*)”, “”, line, flags=re.IGNORECASE)),

file.close()

call([“git”, “diff”])

call([“git”, “add”, “.”])

Now all we have to do is simple save the file as “pre-commit” to run it during the pre-commit event.

To be able to run our script we will have to make it executable. People on Linux or OSX need to run the following command.

chmod +x <path to repository>/.git/hooks/pre-commit

That is it, now every time you perform a commit, the script runs and removes all debugger, console statements from your repo files and then commits them.