发布于 2026-01-06 7 阅读
0

使用 `@commitlint` 改进 Git 提交

使用 `@commitlint` 改进 Git 提交

在团队协作开发项目时,Git是一个至关重要的工具,它能帮助团队顺畅地协作。其关键特性之一是快照commits功能,该功能类似于项目进度的快照。

git提交

但是,你知道我的哪一次提交添加了新功能吗?我敢肯定你不知道,包括我自己也不知道……

问题是我随意命名提交时没有查看上下文,这让我很难确定何时需要撤销更改。

commitlint

为了解决这个问题,可以使用一个名为 `linter` 的实用代码commitlint检查工具。它可以防止我们随意命名提交。

// git commit
git commit -m "oops"

// it will trigger this error 
// ❌ type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]
Enter fullscreen mode Exit fullscreen mode

并强制我们正确命名提交。

git commit -m "fix: resolve the issue related to comma"
git commit -m "feat: add a feature named calculator"
Enter fullscreen mode Exit fullscreen mode

上下文非常清晰易懂,对吧?

以下是所有建议和提供的类型,commitlint包括示例提交:

  • 构建--> 影响构建系统或外部依赖项的更改
git commit -m "build: update npm dependency"
Enter fullscreen mode Exit fullscreen mode
  • ci --> 对我们的 CI 配置文件和脚本的更改
git commit -m "ci: add circleci configuration file"
Enter fullscreen mode Exit fullscreen mode
  • 文档--> 仅文档更改
git commit -m "docs: update readme with installation instructions"
Enter fullscreen mode Exit fullscreen mode
  • 功能--> 一项新功能
git commit -m "feat: add user authentication feature"
Enter fullscreen mode Exit fullscreen mode
  • 修复--> 错误修复
git commit -m "fix: resolve issue with incorrect data rendering"
Enter fullscreen mode Exit fullscreen mode
  • 性能--> 提高性能的代码更改
git commit -m "perf: optimize database query for faster response times"
Enter fullscreen mode Exit fullscreen mode
  • 重构--> 既不修复错误也不添加新功能的代码更改
git commit -m "refactor: reorganize code structure for better readability"
Enter fullscreen mode Exit fullscreen mode
  • 样式--> 不影响代码含义的更改(空格、格式、缺失的分号等)
git commit -m "style: format code according to Prettier standards"
Enter fullscreen mode Exit fullscreen mode
  • 测试--> 添加缺失的测试或更正现有测试
git commit -m "test: add unit tests for user authentication"
Enter fullscreen mode Exit fullscreen mode

有很多功能commitlint我无法一一列举,安装指南也是如此。要了解详情,您可以直接访问https://github.com/conventional-changelog/commitlint

文章来源:https://dev.to/maafaishal/better-git-commits-with-commitlint-g18