🦁

commitlintによるコミットメッセージの標準化

2025/02/18に公開

課題

  • 非統一なコミットメッセージが引き起こす
  • 変更履歴の追跡困難(例: fixだけのメッセージでは何が修正されたか分かりにくい)
  • コードレビュー効率の低下

https://www.conventionalcommits.org/ja/v1.0.0/

git commit -m "<type>[optional scope]: <description>"

commitlintについて

commitlint を使用すると、コミットメッセージが作成された直後に自動でリンティングを実行できるため、開発効率の向上とチーム協働を実現するためのベストプラクティスとなります

https://commitlint.js.org/

インストール

yarn add -D husky @commitlint/{cli,config-conventional}

動作原理図解

GitHook連携

echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg
project
├── .husky/
│    └──commit-msg
└── ...

commitlintの設定ファイル作成

echo "export default { extends: ['@commitlint/config-conventional'] };" > commitlint.config.mjs
project
├── commitlint.config.mjs
└── ...

commitlintの基本設定

commitlint.config.mjs
export default {
  extends: ["@commitlint/config-conventional"],
  // 設定内容
};

https://commitlint.js.org/reference/configuration.html#configuration-object-example

テスト

git add .
git commit -m "xxxxxxx"
yarn run v1.22.19
$ commitlint --edit .git/COMMIT_EDITMSG
⧗   input: xxxxxxx
✖   subject may not be empty [subject-empty]type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky - commit-msg hook exited with code 1 (error)

Discussion