Codementor Events

How to automate your git workflow with git hooks

Published May 28, 2018Last updated Nov 24, 2018
How to automate your git workflow with git hooks

What are they? How can you use them to improve your git workflow?

What are Git Hooks?
Git hooks are scripts that run prior to certain events like commit, push, rebase, etc. Since they are event-based, when you run certain git commands, git will check the hooks within the git repository to see if there is an associated script to run. This is found in the .git/hooks directory.

cd .git/hooks

If you list the files in that directory you will see the following sample hooks:

  1. applypatch-msg.sample
  2. commit-msg.sample
  3. post-update.sample
  4. pre-applypatch.sample
  5. pre-commit.sample
  6. pre-push.sample
  7. pre-rebase.sample
  8. pre-recieve.sample
  9. prepare-commit-msg.sample
  10. update.sample

How Can You Use Them to Improve your Git Workflow?
If you have ever worked on an open-source project with lots of contributors, or perhaps you're the technical team lead on a project and are looking for ways to improve your team's git workflow in such a way that certain workflow errors that developers are prone to are avoided before they are committed, or perhaps you're a developer who likes to always have her A game at 100 all the time, then now is a good time to explore git hooks.

In this post I am going to show you how to use the pre-push hook to run tests before pushing code to your remote branch. You can use any scripting language you like. PHP, JavaScript, and Python are very common scripting languages. In this post I will be using PHP.

If you are using a Mac, then follow along. If you're using a Windows machine, the shell commands will be different. Windows commands are not provided in this post.

Clone this repository. It contains a project built with Laravel and contains some unit tests written with PHPUnit. Once you have that cloned and set up, change directory to the git hooks directory and create a pre-commit file. Open this up with your favorite text editor (I use Atom).

cd .git/hooks
touch pre-commit
atom pre-commit      // Open this file with any text editor you like

Ideally, I want to be able to run the tests before pushing code to my remote branch. If the hook script exits with 0 then all is well and git continues to push the code to my remote branch. If it exits with a non-zero code, git halts the operation.

Screen Shot 2018-05-22 at 7.21.15 PM.png

Find the gist here.

Line 7
The exec function triggers the command to run the tests which are vendor/bin/PHPUnit in this case. 
If you are using any other test framework like Codeception then you should replace vendor/bin/PHPUnit with the specific command to run the tests in your chosen framework. The contents of the output variable will be an array populated with every line of output from the command.

Line 9:
If the exit code is not equal to 0, then the test did not run successfully. You can examine the contents of the output variable and determine which item of the array you want to display on your terminal. I have displayed the item at index 14 which shows a summary of the tests, passes and failures. This part is really up to you. You can output whatever you like on the terminal; the bottom line is that git halts and the push event does not happen anyway.

Line 18:
A zero exit code means we are all green. Git continues with the push event to your remote branch.

What next?
Make the pre-commit file executable to make it function as a hook.

chmod a+x pre-push

Now proceed to make a small change to any of the files in the project and stage the changed file(s). You can tweak your code to trigger a failure just so we can see the hook in action immediately.

ga . && gc -m "some small change"
git push origin the-branch-you-are-on

If there is a failure you will see something like what's below on your terminal:
Screen Shot 2018-05-22 at 6.20.27 PM.png

What's the benefit of this?

  1. Say you forgot to run the test command before pushing, this saves you the embarrassment you might face when the build fails on github/bitbucket/gitlab.
  2. Running the test manually all the time before pushing is a repetitive task, and what do we do with repetitive tasks? I thought so too! Plus it increases your productivity.
  3. You can do really cool stuff like computing the time it took for the test to run. That way, you can immediately know if the test took too long to execute. It's really all up to you.
    Feel free to explore other use cases for the different git hooks. I find this tool very interesting and cool. And you? Let me know in the comments.

Did you find this post useful? Kindly show some love and reshare!

Discover and read more posts from Alienyi David
get started
post commentsBe the first to share your opinion
Show more replies