Git Commit Msg

简介: Git Commit Msg

The reasons for these conventions:

  • automatic generating of the changelog
  • simple navigation through git history (e.g. ignoring style changes)

Format of the commit message

Commit message 包括三部分: Header, Body 和 Footer .

<type>(<scope>): <subject>
// Empty one line 
<body>
// Empty one line
<footer>

Header 必须, Body 和 Footer 可以省略。

第一行不得超过 70 个字符,第二行始终为空。其他行换行行为 80 个字符, typescope 始终小写。

Header

Header 部分只有一行,三个字段: type(必需)、scope(可选)和subject (必需)。

(1) type

type 用于说明 commit 的类别,允许使用下面标识。

  • feat: 新功能 (feature)
  • fix: 修补 bug
  • docs: 文档(document)
  • style: 格式/样式(不影响代码运行的变动)
  • refactor: 重构(既不是新增功能,也不是修改bug的代码改动)
  • test: 增加测试
  • chore: 构建过程或辅助工具的变动
  • perf: 提高性能的代码更改
  • build: 影响构建系统或外部依赖项的更改(示例范围: gulp, npm)
  • ci: 对CI配置文件和脚本的更改(示例范围:Travis,Circle,BrowserStack)
  • revert: 恢复到以前的提交
(2) scope

scope 用于说明 commit 影响的范围

  • init
  • runner
  • watcher
  • config
  • web-server
  • proxy
  • etc.
(3) subject

subject 是 commit 目的的简短描述,不超过 50 个字符

  • 以动词开头,使用第一人称现在时,比如 change, 而不是 changedchanges
  • 第一个字母小写
  • 结尾不加句号(.)

Body

Body 是对本次 commit 的详细描述,可多行。下面是一个范例。

More detailed explanatory text, if necessary.  Wrap it to 
about 70 characters or so. 
Further paragraphs come after blank lines.
- Bullet points are okay, too
- Use a hanging indent

两个注意点

  1. 使用第一人称现在时, 使用 change 而不是 changedchanges
  2. 说明代码变动的动机,以及与以前行为的对比。

Footer

Footer 部分只用于两种情况。

(1) 不兼容变动

如果当前代码与上一个版本不兼容,则 Footer 部分以 BREAKING CHANGE 开头,后面是对变动的描述、以及变动理由和迁移方法。

BREAKING CHANGE:
`port-runner` command line option has changed to `runner-port`, so that it is
consistent with the configuration file syntax.
To migrate your project, change all the commands, where you use `--port-runner`
to `--runner-port`.
(2) 关闭 Issue

如果当前 commit 针对某个 issue ,那么可以在 Footer 部分关闭这个 issue。

前缀为Closes 关键字,如下所示:

Closes #123

也可以关闭多个 issue 。

Closes #456 #789 #911

Revert

还有一种特殊情况,如果当前 commit 用于撤销以前的 commit,则必须以revert: 开头,后面跟着被撤销 Commit 的 Header。

revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.

Body 部分的格式是固定的,必须写成This reverts commit &lt;hash>. ,其中的 hash 是被撤销 commit 的 SHA 标志符。

如果当前 commit 与被撤销的 commit , 在同一个发布(release)里面,那么它们都不会出现在 Change log 里面。如果两者在不同的发布,那么当前 commit,会出现在 Change log 的 Reverts 小标题下面。

Commitizen

Commitizen 是一个撰写合格 Commit message 的工具。

安装

npm install -g commitizen

然后,在项目目录里,运行下面命令,使其支持 Angular 的 Commit message 格式。

commitizen init cz-conventional-changelog --save --save-exact

以后,凡是用到 git commit 命令,一律改为使用 git cz 。这时会出现选项,用来生成符合格式的 Commit message.

➜  vue-examples git:(master) ✗ git add . 
➜  vue-examples git:(master) ✗ git cz
cz-cli@3.0.7, cz-conventional-changelog@2.1.0
Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.
? Select the type of change that you're committing: 
  revert:   Reverts a previous commit 
  feat:     A new feature 
  fix:      A bug fix 
❯ docs:     Documentation only changes 
  style:    Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) 
  refactor: A code change that neither fixes a bug nor adds a feature 
  perf:     A code change that improves performance 

生成 Change log

使用 conventional-changelog 工具生成 Change log

安装

npm install -g conventional-changelog-cli

进入项目目录

cd my-project

生成 Change log

conventional-changelog -p angular -i CHANGELOG.md -s

Angular 规范:docs.google.com/document/d/…

相关文章
|
2月前
|
前端开发 算法 开发工具
如何将git的多个commit合成一个
如何将git的多个commit合成一个
67 0
|
3月前
|
前端开发 测试技术 持续交付
【开发规范】Git Commit 规范
【1月更文挑战第26天】【开发规范】Git Commit 规范
|
3月前
|
JavaScript 测试技术 持续交付
2020你应该知道的git commit规范
2020你应该知道的git commit规范
89 0
|
3月前
|
开发工具 git
git撤销推送到远端仓库的提交commit信息
git撤销推送到远端仓库的提交commit信息
|
9月前
|
开发工具 git
git --- git撤销commit(未push)
git --- git撤销commit(未push)
123 0
|
4月前
|
开发工具 git
在偶有几次git commit的时候出现大量额外文件选择提交?
在偶有几次git commit的时候出现大量额外文件选择提交?
44 1
|
4月前
|
开发工具 git
git commit template
git commit template
23 0
|
4月前
|
开发工具 git
git 文件太大,清空历史commit
git 文件太大,清空历史commit
30 0
|
5月前
|
开发工具 git
DEA git提交规范插件(git commit template插件)
DEA git提交规范插件(git commit template插件)
235 0
|
5月前
|
JavaScript 开发工具 git
git commit 提交代码时报错Stashing changes... [started] Stashing changes... [skipped] → No partially staged
git commit 提交代码时报错Stashing changes... [started] Stashing changes... [skipped] → No partially staged