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.
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.
If we check our globals for git we should see something like
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.
Now you should see a hooks directory and we will need to set the proper permissions to make the scripts executable.
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.
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.
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.
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
var_dump that should probably be excluded from your production code.
Go forth and work smarter not harder!