Automate code verification before a commit
NicolasBrondinBernard
Have you set up tests in your project but don't always think to run them before committing? Here's a solution!

Article published on 31/03/2021, last updated on 10/08/2026
When you version your code using Git, you have the ability to run specific scripts during lifecycle events of your versioning, this is what we call "hooks".
Git offers many hooks, notably two that will interest us here:
- before a local commit (pre-commit)
- before a push to a remote server (pre-push)
I invite you to read the official documentation if you want to learn more about all the available hooks
The principle is simple, each hook is a script that will be called by Git. If the script in question returns a code 0 (everything went well) then Git will continue its current action, if the script returns a code 1 (error), then the event will be cancelled.
A pre-commit hook can for example be used to:
- Make sure that all tests pass correctly
- Run a linter
- Check the code coverage
- ...
Depending on your use case, you can use a pre-commit or pre-push hook, I personally recommend favoring the former but it's up to you!
Creating a pre-commit hook by hand
When you initialize your Git repository, it adds sample scripts to show you how to proceed in the /.git/hooks/*.sample folder
To create your first pre-commit hook, you can simply rename the pre-commit.sample file to pre-commit.
Careful for Linux and MacOS users, you'll need to remember to add execution rights to the file.
The sample file contains a script that checks whether the file names don't contain non-ASCII characters, and cancels the commit if that's the case, but you're free to create your own.
Here's the minimal template for a pre-commit file:
#!/bin/sh
if [ ... ]
then
echo "Pre-commit hook failed because..."
exit 1
fi
echo "Pre-commit hook executed successfully"
exit 0
Once your script is functional, all that's left is to try creating a commit and watch your hook run!
Creating a pre-commit hook with NPM
I personally work in a NodeJS environment, and I'm not an expert in bash scripts, so I found a solution to manage my pre-commit workflow directly with NPM.
This also allows me to share my hook with the other developers on the project, because otherwise the /.git/ folder is not versioned.
To do this, you just need to install the NPM package called "pre-commit":
$ npm install pre-commit --save-dev
And all that's left is to specify the names of the scripts that must run successfully before the commit, directly in the package.json file:
{
...
"scripts": {
"test": "mocha -R spec tests/index.js --exit",
"coverage": "nyc --reporter=text",
"format": "prettier --write ./src"
},
"pre-commit": [
"format",
"test",
"coverage"
],
...
}
Careful, if your hook doesn't run, make sure you installed the module after initializing your Git repository, otherwise it won't find the hooks folder and will fail silently.
You can find the official documentation for the module directly on GitHub: https://github.com/observing/pre-commit
Complete courses, exercises and certificates to really learn programming!
4.8 average rating
No comments yet