Git Hooks

What is a git hook?

Git hooks are surprisingly something developers know nothing about or have even heard of. Which is weird because it’s one of those things that you should no doubt be using in your git workflow. If utilized they will take out some of those redundant tasks that you just hate doing or completely forget about. They can also improve code quality across projects with specific needs. I can’t stress enough how happy you will be by learning how to us git hooks and integrating them into your project.

A git hook is a bash script that is ran at certain periods in the execution process when you are working with git. For instance if you are about to push there is the hook pre-push which can be used to execute a script before a push occurs. The script can do anything you want really. You can check your codebase for certain flaws, send out a tweet, or anything to your liking really.

Configure your first hook

By default git has it’s own hooks and init dir which is dependent on your system. These hooks are included in every repo that you create on your system by default and you can check them out by having a look at your .git dir in one of your projects. There are also sample hooks in the .git/hooks dir by default so you can have a look at those for more insight.

To start we’ll need to create a custom directory where we want to store our git templates. I keep any custom code on my machine in my home directory under ~/.code/ so I have mine configured like so.

1
git config --global init.templatedir '~/.code/.git_template'

If we check our globals for git we should see something like

1
git config -l
1
init.templatedir=~/.code/.git_template

The directory can be named anything you want really and doesn’t have to be named the same as mine. After you set your global template directory you will need to add a hooks directory under that.

1
cd ~/.code/.git_template && mkdir hooks

Now you should see a hooks directory and we will need to set the proper permissions to make the scripts executable.

1
chmod ug+x ~/.code/.git_template/hooks/*

Next we will want to create a hook inside that hooks dir and we are going to keep is simple. What we are going to do is write a simple bash script that will prepend the current branch name to our commit message. This is useful if your working with multiple branches and want to have a hard reference to that branch name after it has been merged into other branches. I use if for feature branch on occasion and it has proven useful.

1
touch ~/.code/.git_template/hooks/prepare-commit-msg

If you like here is a sample of that code. What it does is simply get the branch name and checks if we are on master or develop. If we aren’t on either of those branches then it will prepend the current branch wrapped in square brackets to our commit message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# This way you can customize which branches should be skipped when prepending the commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
# Define the branches to skip if your on an of these branches
BRANCHES_TO_SKIP=(master develop)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
fi

Adding your hooks to your project

After you’ve created your git hook navigate to a project that you don’t mind testing out this nifty new script on and type.

1
git init

This should pull in your new hooks configuration into that repo for you to use. If you want to verify that they are there you can navigate to the .git/hooks directory in your project and check if the files are there and current. Sometimes it doesn’t initialize properly so just be weary of that. Create a new branch and name it anything you like as long as it’s not in that list of BRANCHES_TO_SKIP. Once you complete your changes write up your commit message as usual and you should see the branch name appended to the commit message the you just typed up.

Really that’s it! You can get as creative as you like with these scripts. For instance you can check for debugging chunks of code or keywords such as console.log or var_dump that should probably be excluded from your production code.

Go forth and work smarter not harder!

More information on git hooks