git commit 提交规范

本文介绍了Conventional Commits规范,一种结构化的Git提交消息格式,旨在提高代码库的清晰度和维护性。内容包括规范的组成部分(类型、范围、描述、主体和页脚),常见提交类型,以及如何通过Commitizen、Commitlint和Husky自动化工具来确保提交合规。

在软件开发中,良好的Git提交记录规范有助于保持代码库的清晰和易维护。常见的提交记录规范之一是由Angular团队提出的Conventional Commits,它提供了一种结构化的提交消息格式,便于自动化工具处理和生成变更日志。

Conventional Commits规范

Conventional Commits的提交消息由以下部分组成:

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]
1. 类型(Type)

type用于说明提交的类型。常见的类型包括:

  • feat:新功能(feature)
  • fix:修补bug
  • docs:文档变更
  • style:代码格式(不影响代码运行的变动)
  • refactor:重构(既不是新增功能,也不是修复bug的代码变动)
  • perf:优化相关,比如提升性能、体验
  • test:增加测试
  • build:构建系统或外部依赖项的变更
  • ci:CI配置文件及脚本的变更
  • chore:其他不修改src或test文件的变动
  • revert:恢复先前的提交
2. 可选范围(Scope)

scope是可选的,用于说明提交影响的范围,比如数据层、控制层、视图层等。

3. 描述(Description)

description是对变更的简要说明,应该简明扼要。

4. 可选主体(Body)

body是可选的,详细描述提交的内容,可以包括变更的动机和设计细节。

5. 可选页脚(Footers)

footer是可选的,用于放置与提交相关的元数据,例如关闭的issue或破坏性变更的说明。

示例

1. 添加新功能
feat(user): add user authentication

Implemented user authentication using JWT. Added login and signup endpoints.
2. 修复bug
fix(login): resolve login issue with special characters

Fixed a bug where users could not log in if their password contained special characters.
3. 文档变更
docs(readme): update installation instructions

Updated the README file to include new installation steps for Windows users.
4. 代码格式变更
style: format code with Prettier

Applied Prettier formatting to the entire codebase to ensure consistent code style.
5. 重构代码
refactor(auth): simplify authentication middleware

Refactored the authentication middleware to reduce complexity and improve readability.
6. 性能优化
perf(database): optimize query performance

Improved database query performance by adding appropriate indexes.
7. 测试
test(auth): add unit tests for authentication

Added unit tests for the authentication service to cover all edge cases.
8. 构建系统变更
build(deps): update dependency lodash to v4.17.21

Updated the lodash dependency to address a security vulnerability.
9. CI配置变更
ci(travis): add Node.js version 14 to build matrix

Configured Travis CI to test against Node.js version 14.
10. 其他杂项
chore: update contributors list

Updated the CONTRIBUTORS.md file to include new contributors.
11. 恢复先前的提交
revert: revert "feat: add user authentication"

This reverts commit abc123.

使用工具自动化

为了确保提交记录符合规范,可以使用工具进行自动化验证和格式化。例如:

  • Commitizen:交互式CLI工具,帮助生成符合Conventional Commits规范的提交消息。
  • Commitlint:验证提交消息是否符合规范的工具。
  • Husky:Git钩子管理工具,可以与Commitlint结合使用,在提交前进行验证。
Commitizen示例

安装Commitizen和Conventional Commits适配器:

npm install -g commitizen
commitizen init cz-conventional-changelog --save-dev --save-exact

使用Commitizen进行提交:

git cz
Commitlint和Husky示例

安装Commitlint和Husky:

npm install @commitlint/{config-conventional,cli} husky --save-dev

配置Commitlint:

echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js

设置Husky Git钩子:

npx husky install
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

通过这些工具,可以确保团队成员在提交代码时遵循统一的提交记录规范,保持代码库的整洁和可维护性。

### Git Commit Message 规范示例 #### 单行提交信息格式 每次提交应当只解决一个问题,这有助于简化代码审查过程并提高可追溯性。单行提交信息应该简洁明了,通常不超过72个字符[^1]。 ```plaintext type(scope): subject ``` - `type` 表示更改的性质(如 feat, fix) - `scope` 是可选字段,用于指定受影响的部分或模块名称 - `subject` 描述所做的改动摘要 #### 完整多行提交信息结构 对于更复杂的变更,则推荐采用如下完整的多行格式: ```plaintext type(scope): subject Body explaining the change and its motivation. Footer with any relevant issue references. ``` 其中: - **Header**: 包含三部分——类型(type),作用域(scope)以及主题(subject) - **Body**: 对修改原因及实现细节做进一步解释说明;如果适用的话还可以提及如何测试这些变化。 - **Footer**: 如果有的话,在这里注明关联的问题编号或者其他元数据信息,例如关闭某个GitHub Issues。 #### 实际案例展示 ##### 添加新功能的例子 当向项目中引入新的特性时,可以这样书写commit message: ```plaintext feat(user-profile): add avatar upload feature Allow users to upload avatars from their local machines or via URL links. This enhancement improves user experience by personalizing profiles. Closes #1234 ``` ##### 修复Bug的例子 针对错误修正类别的提交消息则像下面这样表达: ```plaintext fix(authentication): resolve login redirect loop on Safari browser The previous implementation caused an infinite redirection when logging in using Safari due to incorrect handling of session cookies. The solution was implemented according to best practices outlined at MDN Web Docs regarding secure cookie settings. Fixes #9876 ``` #### 工具支持 为了确保团队成员都能遵循一致的消息格式化则,建议使用工具辅助验证。例如可以通过配置 pre-commit hook 来运行 validate-commit-msg 或者集成 CI/CD 流程中的检查脚本以自动拒绝不符合定的提交请求[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值