cd ../blog
8 min read DevOps

Bulletproof Git: Husky, Commitlint, and Lint-staged for an Immaculate Repo

Prevent dirty code or poorly written commits from entering your repository. Automate testing, formatting, and message validation with Husky.

Husky Testing Git
Bulletproof Git: Husky, Commitlint, and Lint-staged for an Immaculate Repo

## No More 'asdf' Commits

Working in a team requires the Git history to be a source of truth, not a graveyard of meaningless messages. To achieve this, we're going to bulletproof the workflow.

## Conventional Commits: The Standard

Beyond `feat` and `fix`, a senior dev uses:

  • -**refactor**: Code changes that neither fix a bug nor add a feature.
  • -**style**: Changes that do not affect the meaning of the code (whitespace, formatting, semicolons).
  • -**perf**: Changes to improve performance.
  • -**test**: To add or correct tests.
  • -**chore**: Updates to build tasks, packages, etc.
  • ## Automation with Commitlint

    How do we force everyone (including ourselves) to follow this? With **Commitlint**. If the message doesn't follow the format, Husky cancels the commit.

    ## Husky + Lint-staged: The Quality Filter

    You don't want code with linter errors or failing tests to be uploaded. We configure Husky to run **lint-staged** before each commit:

  • 1.**Prettier**: Automatically formats the code.
  • 2.**ESLint**: Checks for syntax errors or bad practices.
  • 3.**Vitest / Jest**: Runs unit tests for the changed files.
  • json
    "lint-staged": {
      "*.{js,ts,tsx}": [
        "eslint --fix",
        "prettier --write",
        "vitest related --run"
      ]
    }

    ## Conclusion

    With this setup, your repository becomes "self-cleaning." You ensure that every line reaching the cloud has professional quality, is tested, and well-documented.

    // Thanks for reading!