Streamlining Git Workflows with Husky: Enforcing Commit Quality with Git Hooks

Streamlining Git Workflows with Husky: Enforcing Commit Quality with Git Hooks

Introduction

Maintaining an efficient Git workflow is incredibly important. One of the key aspects of this workflow is ensuring high-quality commits. In this article, we will delve into how you can streamline your Git processes and enforce commit quality using a tool called Husky in conjunction, with Git hooks.

Why High-Quality Commits Matter

Before diving into the details let's take a moment to understand why high-quality commits are significant;

  1. Readability: Written commit messages make it easier for you and your team to understand the purpose and context of each change.

  2. Debugging: Clear commits simplify the process of identifying and resolving issues by providing a history of changes.

  3. Collaboration: When working in teams meaningful commit messages help maintain organization and alignment.

  4. Revert and Cherry-pick: Quality commits are crucial when you need to revert changes or selectively apply commits to branches.

With these advantages in mind let's explore how you can achieve high-quality commits in your Git workflow with the assistance of Husky and Git hooks.

What is Husky and Git Hooks

Husky is a popular git hooks manager that automates tasks before committing or pushing changes. Husky allows you to maintain coding standards execute tests and make sure that your commits follow guidelines.

Git Hooks are scripts that allow us to automate or enforce actions, in our Git workflow. For instance, we can utilize a commit hook that runs before making a commit to ensure coding standards and identify any linting errors.

Setting Up Husky and Git Hooks

To get started, follow these steps to set up Husky and Git hooks in your project:

  1. Initialize Your Git Repository: If you haven't already, create or navigate to your Git repository.

  2. Install the following packages as a dev dependency

     npm install husky @commitlint/cli @commitlint/config-conventional -D
    
    • husky: git hooks manager

    • @commitlint/config-conventional: A configuration for Commitlint that enforces conventional commit message format.

    • @commitlint/cli: The Commitlint command-line tool.

  3. Initialize Husky in Your Repository:

     npx husky install
    
  4. Configure Commitlint: Create a commitlint.config.js file in your project's root directory to define the commit message format rules. You can use the conventional format or customize it to your needs. Here's an example using the commit message format rules:

     module.exports = {
         extends: ['@commitlint/config-conventional'],
         rules: {
             'body-leading-blank': [1, 'always'],
             'body-max-line-length': [2, 'always', 100],
             'footer-leading-blank': [1, 'always'],
             'footer-max-line-length': [2, 'always', 100],
             'header-max-length': [2, 'always', 100],
             'scope-case': [2, 'always', 'lower-case'],
             'subject-case': [
                 2,
                 'never',
                 ['sentence-case', 'start-case', 'pascal-case', 'upper-case'],
             ],
             'subject-empty': [2, 'never'],
             'subject-full-stop': [2, 'never', '.'],
             'type-case': [2, 'always', 'lower-case'],
             'type-empty': [2, 'never'],
             'type-enum': [
                 2,
                 'always',
                 [
                     'chore',
                     'ci',
                     'docs',
                     'feat',
                     'fix',
                     'perf',
                     'refactor',
                     'revert',
                     'style',
                     'test',
                     'translation',
                     'security',
                     'changeset',
                 ],
             ],
         },
     };
    

    The rules above collectively define the commit message conventions that Commitlint will check against when validating your commits. It helps maintain a consistent and well-structured commit history in your Git repository, making it easier to understand the purpose of each commit.

  5. Configure Husky: Husky allows you to run tasks before specific Git hooks, such as pre-commit or commit message. To set up Husky, create a .husky directory in your project's root by using the command npx husky in your root directory a .husky directory will be created and create a pre-commit and commit-msg hook script and inside it:

    pre-commit:

     #!/bin/sh
     . "$(dirname "$0")/_/husky.sh"
    
     npm run lint-format
    

    The bash script above initializes the husky's functionality by sourcing its husky.sh script and then runs an npm script named lint-format, which performs code linting and formatting checks.

    commit-msg:

     #!/bin/sh
     . "$(dirname "$0")/_/husky.sh"
     npx --no-install commitlint --edit "$1"
    

    commit-msg hook script sources the husky.sh script and attempts to run commitment on the commit message file passed as an argument.

  6. Add a script to your package.json

     "scripts": {
             "lint": "eslint . --fix",
             "format": "prettier --write .",
             "lint-format": "npm run lint && npm run format"
     }
    

    This code defines npm scripts where lint uses ESLint to fix linting issues in the project, the format uses Prettier to automatically format code, and lint-format runs both lint and format scripts sequentially.

Commit your changes:

git add .
git commit -m "feat: add new feature"

If your commit message does not follow the conventional format or if any staged files fail linting, Husky will prevent the commit and provide feedback.

Benefits of Using Husky and Git Hooks

The following are some of the advantages you can expect if you enforce commit message standards using Husky and Git hooks:

  1. Husky maintains a clean and organized commit history by enforcing a consistent commit message format.

  2. Husky and Commitlint automate the process of writing and checking commit messages.

  3. Your team will be able to understand your commits more easily, resulting in more efficient collaboration.

  4. Clear commit messages make it easier to pinpoint the source of the bug and make debugging easy.

Conclusion

It is essential that software engineers streamline their Git workflow and write high-quality commits. Utilizing Husky hooks and Git hooks makes it easier to enforce commit message standards, resulting in a more clean and organized Git repository.

Did you find this article valuable?

Support Eddy's Space by becoming a sponsor. Any amount is appreciated!