前端配置husky,commit-lint

1.husky配置

什么是husky?

husky是 git 的hook工具,就是用于git commit之前进行自定义命令。 我们可以使用husky在代码提交之前进行相应的检查,防止不规范的代码被提交到仓库。

安装husky

text 复制代码
pnpm add --save-dev husky

初始化husky

text 复制代码
npx husky init

执行后:

  • 项目根目录下会生成一个 .husky 文件夹。
  • 里面会包含一个默认的 pre-commit 示例 Hook 文件。

在.husky/pre-commit中编写命令

在pre-commit文件我们可以写在git提交之前进行的命令。

注意,这些pnpm命令提前在package.json文件中定义好:

pre-commit文件中的命令示例:

shell 复制代码
pnpm lint
pnpm format
git add -A

2.commit-lint

commit-lint可以规范我们的提交信息。

怎么规范的?

commit-lint规范git提交的描述词,比如我们对代码的样式部分进行了更改,提交信息应该这样写:

text 复制代码
git commit -m "style: 样式进行修改"

如果我们修改的是一些锁翠的代码,可以这样写:

text 复制代码
git commit -m "chore: 配置commit-lint"

如果不写stylechore等描述词,commit-lint拒绝提交。

安装commit-lint

text 复制代码
pnpm add -D @commitlint/cli @commitlint/config-conventional

创建配置文件

在项目根目录下创建 commitlint.config.js(或 .commitlintrc),添加以下内容:

js 复制代码
import '@commitlint/config-conventional'

export default { extends: ['@commitlint/config-conventional'] }

配置 husky 的 commit-msg Hook

Husky 需要在 commit-msg 阶段运行 commitlint,以验证提交信息。

创建 .husky/commit-msg 文件,并添加以下命令:

text 复制代码
pnpm commitlint --edit $1

随便提交测试一下

commit-lint描述词

在下面图中的文件中我们可以找到commit-lint定义的描述词:

  • feat:新功能(Feature)
  • fix:修复 bug(Bug fix)
  • docs:文档更改(Documentation)
  • style:代码样式调整(Formatting, whitespace changes)
  • refactor:重构(代码优化,不新增功能也不修 bug)
  • perf:性能优化(Performance improvements)
  • test:添加或修改测试(Tests)
  • build:构建系统或外部依赖更改(Build system)
  • ci:CI 配置或脚本更改(Continuous Integration)
  • chore:杂项(不修改 src 或 test 的更改)
  • revert:回滚之前的提交(Revert)

提交格式:

text 复制代码
git commit -m "chore: 配置commit-lint"
相关推荐
KaMeidebaby4 小时前
卡梅德生物技术快报|PD1 单克隆抗体定制配套 N 糖全谱质控开发
前端·人工智能·算法·数据挖掘·数据分析
wangruofeng5 小时前
iOS、Android、Flutter 2026 流行框架对比
前端框架·app
nuIl5 小时前
实现一个 Coding Agent(3):工具调用
前端·agent·cursor
nuIl5 小时前
实现一个 Coding Agent(4):ReAct 循环
前端·agent·cursor
nuIl5 小时前
实现一个 Coding Agent(1):一次 LLM 调用
前端·agent·cursor
nuIl5 小时前
实现一个 Coding Agent(2):让 LLM 流式响应
前端·agent·cursor
copyer_xyf5 小时前
Python 异常处理
前端·后端·python
sugar__salt5 小时前
从栈队列数据结构到JS原型面向对象全解
前端·javascript·数据结构
Python私教6 小时前
Git 撤销与回退避坑指南:reset / revert / restore 到底用哪个(2026 实战)
git
流浪0016 小时前
Linux篇(九):一文搞懂 Git:版本控制的原理与实操指南
git