United Kingdom: +44 (0)208 088 8978

Preventing bad git commits with Husky

Isaac shows us Husky, a tool that allows us to bring CI-style checks even earlier in the build chain

We're hiring Software Developers

Click here to find out more

We've all been in the situation where we've made a commit into source control, pushed, and then instantly realised that we've broken the build with a typo or similar. Or not realised, and then broken everyone else's code on that branch (if you're using branches!). Husky is a tool designed to fix that.

Git Hooks

Many of us are conversant with the idea of a continuous integration (CI) service such as TeamCity, Azure DevOps, GitHub Actions. They're designed to ensure that any commits to code are immediately checked with common tasks such as "does the codebase build?" or "do all the unit tests pass?". Well, it turns out that the git source control system itself has a similar capability, with a large numbers of "hooks" or "events" that you can "plug into". For example, git has a "hook" for when code is about to be committed ("pre-commit"), or when the commit message is being set ("commit-msg").

Husky

Husky is a dotnet tool that allows you to run arbitrary actions on specific git hooks. These could be to enforce the same sort of things that you would do centrally, but here they would run before committing code into git. For example, you could ensure your code built and passed all unit tests. Or you could run Fantomas to ensure that your code was correctly formatted. Or perhaps you have a rule that every commit needs to have an issue number in it! Any of these are possible because Husky simply runs any command line tool or arguments as needed; it also has the ability to pass in certain arguments from git, such as the list of files that are in the commit list.

A worked example

After following the getting started guide here, I was quickly able to create a pre-commit hook to run Fantomas to check that code is correctly formatted:

dotnet husky add pre-commit -c "dotnet fantomas --check -r"

This creates a file that stores that command (and is therefore source controlled and will be accessible to all users of the repository). Then, the next time you try to commit code, Fantomas will run to ensure that your code is correctly formatted. As this happens at the git level, it doesn't matter which Git application you use (or the command line); they will all work. As an example, here's a screenshot from Git Extensions (my current git tool of choice):

As you can see, the output of Fantomas has been surfaced into the error message and the commit has been rejected.

Conclusion

Husky is a useful tool to augment (but not replace!) automated CI systems. Not only can it prevent unnecessary broken commits which can slow other developers down, but it also can save time and money (especially if you are paying for build minutes for your CI service!). It's a dotnet tool, is easy to install and start using, and flexible and powerful.