Automating Git Hooks with Husky.Net

Foreword

During a recent encounter with a React project, I noticed how Git hooks were being used to automatically format code and block non-buildable changes from being committed. That sparked an idea—why not apply the same practice in .NET projects? My search for a solution led me to Husky.Net, a powerful tool designed to automate Git tasks seamlessly within .NET environments.

Credits: https://imgflip.com/i/5gzf4i

What is Husky.Net

In a nutshell, Husky.Net is a powerful tool that simplifies Git hooks in .NET projects. It serves as a task runner, wrapping Git hooks with simple, easy-to-use commands.

Why Husky.Net

Husky.Net is an open-source tool written in C# for .NET developers. It integrates seamlessly across any environment, using simple dotnet CLI commands. It supports a wide range of Git hooks, including — but not limited to — pre-commit, post-commit, pre-push, and post-merge.

Installation

Let’s get started. In your .NET repository, create a tool manifest file (.config/dotnet-tools.json) using the command below. If the file already exists, you can skip this step:

dotnet new tool-manifest

Next, install Husky.Net as a local tool and register it in the manifest file:

dotnet tool install Husky

Finally, set up Husky.Net in your repository by running:

dotnet husky install

Automating Code Format

Let’s start with a simple task: adding a Git hook that automatically formats your code before each commit and stages the changes.

dotnet husky add pre-commit -c "dotnet format && git add ."

ELI5 (Explain Like I’m 5)

We’re using the dotnet husky add command to create a new Git hook.

The general format is:

husky add <hook-name> [options]

You can replace <hook-name> with any supported Git hook. For example:

  • pre-commit — Right before a commit is finalized — good for formatting, linting, tests.
  • pre-push — Before pushing commits — e.g. ensure tests pass or code builds.
  • post-commit — After the commit is created — logging or notifications.
  • post-merge — Triggered after merge.

In our example, the command:

  1. Uses dotnet format to format the code.
  2. Then runs git add . to stage the formatted files so they’re included in the same commit.

Quick Notes

  • Husky creates Git hooks inside the .husky folder.
  • Using the add command will append a new command to the specified hook.
  • You can also manually edit .husky/<hook-name> files to add or modify commands.

Additional Brilliant Examples

Preventing Failed Builds

You can prevent committing code that doesn’t build by adding a dotnet build command as a pre-commit hook:

dotnet husky add pre-commit -c "dotnet build"

Running Tests on Push

To ensure code is properly tested before pushing, add the following pre-push hook:

dotnet husky add pre-push -c "dotnet test --no-build --verbosity minimal"

Closing Thoughts

By integrating Husky.Net into your .NET workflow, you can automate essential Git tasks, enforce code quality, and prevent broken builds from ever reaching your repository. It’s a lightweight addition with a big impact—helping your team focus less on manual checks and more on building great software.

Leave a comment