Use Git Hooks to Automate Your Development Workflow
There’s something that happens in my workflow more often than I’d like to admit. I’m focused on a coding task, fixing a bug or finishing up a feature. Certain that I’m done, I git push
and move on to something else. Meanwhile, a CI pipeline is running, analyzing my latest changes and finding them lacking.
And there goes my focus and momentum, all due to an extra empty line, and forgetting to run rubocop before pushing my code!
Enter Git Hooks!
Hooks are extension points built in to git that allow us to add our own functionality in the workflow. Peering into the .git/hooks
directory of a git repo will show us some example scripts for the available hooks:
To prevent myself from pushing bad code up to my git/CI server, I’m going to create a pre-push script that will run my code analysis tool (rubocop) and abort the push on failure. This script will go in .git/hooks/pre-push
:
Now set the script as executable:
Git will run the script before pushing changes. A non-zero exit code indicates failure and will abort the push.
Enjoy!