Skip to content

Commit message

TIP

修改前想清楚哪些东西需要改,最好是列个列表,一个一个做,一个一个提交。尽量避免一次修改多个地方,导致提交记录混乱。

master 分支的首次提交内容必须是 Initial commit,并且需要确保可用。

对于简单(绝大多数)的修改,使用 git commit -m "<类型>[(作用域)]: 描述" 即可。

作用域和描述如果是英文,首字母使用小写。

如果出现了破坏性的修改,需在 <类型>[(作用域)] 后加 !。并且(可选的)在使用多行 commit 的最后一行添加 BREAKING CHANGE: 破坏描述

类型值

类型描述
feat添加了新特征或者新功能
fixbug 修复
chore对非业务性代码的修改,例如 devcontainer.vscode 中的内容
docs修改文档,如 README 文件
style修改样式,如调整 CSS、空格、缩进、空行等
refactor重构代码,如修改代码结构、变量名。比较详细的例子可以参考 《重构:改善既有代码的设计》
test增加、修改、删除代码的测试用例

例子

bash
git commit -m `chore(devcontainer): 增加vscode扩展`
bash
git commit -m "feat: 新增定时发送邮件功能"
bash
git commit
chore(devcontainer)!: 放弃对 Python 3.8 以下版本的支持

BREAKING CHANGE: 使用了 Python 3.8 才出现的海象运算符

template

模版内容:

# head: <type>[<scope>]: <subject>
# - type:
#   - feat: 增加新的特征
#   - fix: 修复bug
#   - chore: 对非业务性代码的修改
#   - docs: 对文档进行了修改
#   - style: 不影响代码含义的修改,比如空格、格式化、缺失的分号等
#   - refactor: 既不是修复bug也不是添加特征的代码重构
#   - test: 增加确实的测试或者矫正已存在的测试
# - scope: can be empty (eg. if the change is a global or difficult to assign to a single component)
# - subject: start with verb (such as 'change'), 50-character line
#
# body: 72-character wrapped. This should answer:
# * Why was this change necessary?
# * How does it address the problem?
# * Are there any side effects?
#
# footer:
# - Include a link to the ticket, if any.
# - BREAKING CHANGE
#

全局使用

  1. 创建 ~/.gitmessage,并填入 template 中的模版内容。

  2. 修改 ~/.gitconfig,确保:

    [commit]
        template = ~/.gitmessage