Streamline Your Development Workflow with Git Hooks

As developers, we know how crucial it is to have a solid feedback loop in place. A well-configured project should have a CI/CD pipeline that ensures the code doesn’t break anything in the application’s logic or codebase. However, this process can be time-consuming and inefficient.

The Problem with Traditional Pipelines

The issue lies in the fact that errors are only detected once the code is in the repository, often after opening a pull request. This means developers must fix the code locally and push it to the repository again, wasting valuable time.

The Solution: Git Hooks

Many of the checks performed on the pipeline can be run locally on developers’ computers. But, no one expects developers to execute a set of commands each time they’re about to commit something. That’s where Git hooks come in. Git hooks are preconfigured custom scripts that get executed before an action is performed in Git.

Introducing Husky and Lint-Staged

To automate this process, we can use Husky, a library that makes sure each time the project’s dependencies are installed, the hooks are properly configured according to the package.json config. We’ll also be using lint-staged, which lets us execute commands on the staged files.

Configuring Husky and Lint-Staged

To install Husky, run the following command:


npm install husky --save-dev

Then, add the following config to package.json:


"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}

Lint-Staged Configuration

Lint-staged configuration supports glob patterns as keys. For example, we can provide the glob pattern to match all the TypeScript files by using the “*.ts” glob pattern.


"lint-staged": {
"*.ts": "eslint --fix"
}

How Lint-Staged Works

Lint-staged makes an assumption that the provided command expects a list of space-separated absolute file paths at the end. So it takes all the absolute paths of the staged files and executes the command by appending the paths at the end.

Integrating Popular Tools with Lint-Staged

Let’s integrate the three most popular tools with lint-staged:

Running a Linter

A linter is the most useful tool when it comes to signaling anything from code style guide inconsistencies to security issues.

Running a Code Formatter

The importance of consistency in code formatting can’t be overstated.

Running Tests

Unit tests are perfect to be run before each commit.

Bonus: Validating Commit Messages

Commit messages are the description of changes the commit consists of. It’s always a good idea to have them written in a unified fashion.

Testing the Setup

After setting everything up, we can commit our changes to see if everything’s working as it is supposed to.

Skipping Checks

If, for whatever reason, you need to skip the checks, there’s an option –no-verify that does exactly that.

By setting up Git hooks, we can ensure that the code pushed to the repository meets the expected standard. This significantly reduces the time needed to fix the discovered issues.

Leave a Reply

Your email address will not be published. Required fields are marked *