Trung BìnhGit iconGit

commit-msg hook với commitlint: cách setup và làm thế nào để không block developer khi làm WIP commits?

commit-msg hook chạy sau khi developer viết commit message — có thể reject nếu message không hợp lệ.

Setup:

bash
npm install --save-dev @commitlint/cli @commitlint/config-conventional

commitlint.config.js:

js
module.exports = {
  extends: ["@commitlint/config-conventional"],
  rules: {
    "type-enum": [2, "always", [
      "feat", "fix", "docs", "style", "refactor",
      "test", "chore", "perf", "ci", "build", "revert", "wip"
    ]],
    "subject-max-length": [1, "always", 100],  // warn, not error
    "body-max-line-length": [0],  // disable
  }
};

.husky/commit-msg:

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

Cho phép WIP commits (không block nhưng vẫn có format):
- Thêm "wip" vào type-enum
- wip: payment refactor halfway → pass

Bypass khi thực sự cần (emergency/merge commit):

bash
git commit --no-verify -m "emergency fix"
# --no-verify bỏ qua TẤT CẢ client-side hooks

Lưu ý: commitlint enforce trên client — developer vẫn có thể bypass bằng --no-verify.

Server-side enforcement cần CI check (dùng commitlint trong GitHub Actions trên PR title).

Xem toàn bộ Git cùng filter theo level & chủ đề con.

Mở danh sách Git