Git生态探索之Commit message 和 Change log 编写最佳实践

背景

最近在学习规范如何使用git来更高效的开发,发现一个比较好用的来规范comment的工具,所以想来记录一下。一般来说,commit message 应该清晰明了,说明本次提交的目的。所以需要一些规范来使这些comment变得可读,commitizen则是最近发现的一款比较易用的工具。

git的提交一般的初学者都会使用git commit -m "hello world"来提交comment,但是一些像hello world这样没有意义的comment让人无法理解这次的提交到底是为了什么,所以我们就要规范一下comment的规范了。

PS:下面是一些基础介绍如果大佬请直接查看第二部分

1. commit message format(信息域)

commit message一般分为三个部分Header,Body 和 Footer

<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>
其中,Header 是必需的,Body 和 Footer 可以省略

Example:
PS D:\git\pythonPractice> git log
commit 58a7a966acb9aa2fffc0e02c9ce3be64b8949991 (HEAD -> master)
Author: Zhiwei Tian <[email protected]>
Date:   Fri Aug 17 17:38:36 2018 +0800

    feat(serve): add grpc server


1.1 HEAD

  • type用于说明 commit 的类别,只允许使用下面7个标识
feat:新功能(feature)
fix:修补bug
docs:文档(documentation)
style: 格式(不影响代码运行的变动)
refactor:重构(即不是新增功能,也不是修改bug的代码变动)
test:增加测试
chore:构建过程或辅助工具的变动

  • scope 用来说明本次Commit影响的范围,即简要说明修改会涉及的部分,比如数据层、控制层、视图层等,
  • subjectcomment所在的位置,这次提交的简短描述

1.2 Body 是对本次 commit 的详细描述,可以分成多行

1.3 Footer 部分只用于两种情况

  • 不兼容变动

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

  • 关闭 Issue

如果当前 commit 针对某个issue,那么可以在 Footer 部分关闭这个 issue (可依次关闭过个issueCloses #123, #245, #992)

1.4 Revert

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

revert: type(scope):  some comment
This reverts commit bfe307ce57d87677c6c473c228e6c2ed8b81dcec.

Body部分的格式是固定的,必须写成This reverts commit &lt;hash>.,其中的hash是被撤销 commit 的 HSHA 标识符。
如果当前 commit 与被撤销的 commit,在同一个发布(release)里面,那么它们都不会出现在 Change log 里面。如果两者在不同的发布,那么当前 commit,会出现在 Change log 的Reverts小标题下面

2. 使用commitizen来执行规范

因为commitizennode的模块,所以前提需要安装node(官网下载地址)

  1. 全局安装commitizennode模块
npm install -g commitizen

  1. 在项目目录下运行命令
commitizen init cz-conventional-changelog --save --save-exact

  1. 此时可能会报找不到package.json的错误,使用下面命令来自动生成一个项目的package,然后在运行2中的命令.
npm init --yes

运行完以上一律使用git cz 代替git commit来提交代码,同时会显示一下选项来自动生成符合格式的commit message.

PS D:\git\pythonPractice> git cz
[email protected], [email protected]

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: (Use arrow keys)
> 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
  test:     Adding missing tests or correcting existing tests
(Move up and down to reveal more choices)

按照提示,你可以写出规范的message了
idea有插件可以使用git commit template

commitizen同时可以检查commit message是否符合格式.
生成change log,还又一些高级用法比如ghooks
这里就不细说了.详细请查看参考链接和validate-commit-msg

  1. 现在项目中可能多出来dir:node_nodules, file:package.json, package-lock.json这些目录和文件,这是node安装模块产生的,如果不是node项目都可以忽略掉,熟悉node的同学肯定都知道哪些是有用的了.

参考链接:
阮一峰的博客
Jartto's blog

转载于:https://www.jianshu.com/p/55adc2330acd

猜你喜欢

转载自blog.csdn.net/weixin_33881041/article/details/91108969