在开发过程中我们一般都会用到git管理代码,在git commit提交代码时我们一般对git commit message随便写点简单的描述,可是随着项目参与人数的增多,发现提交的commit记录越来越杂乱,不便查阅,在网上找了下解决方案,总结一下方便在公司项目中运用。

commit message 格式

目前大家比较认可的是Angular团队的提交规范,很多工具也是基于此规范开发的。该提交规范格式如下:

  1. <type>(<scope>): <subject>
  2. <BLANK LINE>
  3. <body>
  4. <BLANK LINE>
  5. <footer>

每一个commit message由header(必填)、body(选填)和footer(选填)组成,header部分包括三个字段:type(必填)、scope(选填)和 subject(必填)。为了方便浏览,每一行的commit message不应超过100个字符。

type

type 用于说明提交的commit的类别,有一下几种类型:

  • feat:添加新功能(feature)
  • fix:改bug
  • docs: 修改文档(documentation)
  • style: 只改样式(不影响代码运行的变动)
  • refactor:重构(即不是新增功能,也不是改bug的代码变动)
  • perf : 代码优化(提升代码性能)
  • test:增加测试
  • chore:构建过程或辅助工具的变动

scope

本次改动影响的范围

subject

对本次改动的简单描述

body

commit 具体修改内容的详细描述, 可以分为多行

footer

一些备注, 通常是 BREAKING CHANGE 或修复的 bug 的链接.

如何规范提交记录

上面介绍的是通用的git commit message规范,可是在git commit的时候如何用这写规范来写呢?

如果是个人项目我们可以为 git 设置 commit template, 每次 git commit 的时候在 vim 中自动带上模板信息, 自己只要按照模板信息填写就行

  1. 修改~/.gitconfig(.git/config文件), 添加:
  1. [commit]
  2.     template = .git_template
  1. 新建.git_template 内容可如下:
  1. # head: <type>(<scope>): <subject>
  2. # - type: feat, fix, docs, style, refactor, test, chore
  3. # - scope: can be empty (eg. if the change is a global or difficult to assign to a single component)
  4. # - subject: start with verb (such as 'change'), 50-character line
  5. #
  6. # body: 72-character wrapped. This should answer:
  7. # * Why was this change necessary?
  8. # * How does it address the problem?
  9. # * Are there any side effects?
  10. #
  11. # footer:
  12. # - Include a link to the ticket, if any.
  13. # - BREAKING CHANGE
  14. #

这样在项目中执行git commit时vim编辑会带上这些信息

如果我们的项目是多人参与的项目,这样就不合适了。这里我们推荐使用cz-customizable工具生成和约束commit message

cz-customizable有两种使用方式,这里我们采用官方推荐的第二种方式

  1. 安装cz-customizable
  1. npm install cz-customizable --save-dev
  1. 修改package.json文件,在scripts中加入commit命令
  1. "scripts" : {
  2. ...
  3. "commit": "./node_modules/cz-customizable/standalone.js"
  4. }
  1. 新建.cz-config.js文件
  1. module.exports = {
  2. types: [
  3. { value: 'feat', name: 'feat: A new feature' },
  4. { value: 'fix', name: 'fix: A bug fix' },
  5. { value: 'docs', name: 'docs: Documentation only changes' },
  6. {
  7. value: 'style',
  8. name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)',
  9. },
  10. {
  11. value: 'refactor',
  12. name: 'refactor: A code change that neither fixes a bug nor adds a feature',
  13. },
  14. {
  15. value: 'perf',
  16. name: 'perf: A code change that improves performance',
  17. },
  18. { value: 'test', name: 'test: Adding missing tests' },
  19. {
  20. value: 'chore',
  21. name:'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation',
  22. }
  23. ],
  24. scopes: [{ name: ''common }, { name: 'route' }, { name: 'components' }],
  25. allowTicketNumber: false,
  26. isTicketNumberRequired: false,
  27. ticketNumberPrefix: 'TICKET-',
  28. ticketNumberRegExp: '\\d{1,5}',
  29. // it needs to match the value for field type. Eg.: 'fix'
  30. /*
  31. scopeOverrides: {
  32. fix: [
  33. {name: 'merge'},
  34. {name: 'style'},
  35. {name: 'e2eTest'},
  36. {name: 'unitTest'}
  37. ]
  38. },
  39. */
  40. // override the messages, defaults are as follows
  41. messages: {
  42. type: "Select the type of change that you're committing:",
  43. scope: '\nDenote the SCOPE of this change (optional):',
  44. // used if allowCustomScopes is true
  45. customScope: 'Denote the SCOPE of this change:',
  46. subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
  47. body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',
  48. breaking: 'List any BREAKING CHANGES (optional):\n',
  49. footer: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',
  50. confirmCommit: 'Are you sure you want to proceed with the commit above?',
  51. },
  52. allowCustomScopes: true,
  53. allowBreakingChanges: ['feat', 'fix'],
  54. // skip any questions you want
  55. skipQuestions: ['body','breaking','footer'],
  56. // limit subject length
  57. subjectLimit: 100,
  58. // breaklineChar: '|', // It is supported for fields body and footer.
  59. // footerPrefix : 'ISSUES CLOSED:'
  60. // askForBreakingChangeFirst : true, // default is false
  61. };

该文件的配置信息可参考options

至此我们在提交代码时不在使用git commit命令,而是使用npm run commit这样就可以按照规范输出commit message。

校验commit message

上面的配置中我们并没有对commit message进行校验,也就是说如果项目中有成员继续使用git commit -m "message"提交仍是可以的,如果想增加commit message校验可以使用validate-commit-msg工具

  1. 安装相关依赖包
  1. npm install validate-commit-msg husky --save-dev
  1. 在package.json文件中增加以下配置
  1. "husky": {
  2. "hooks": {
  3. "commit-msg": "validate-commit-msg"
  4. }
  5. }

这样我们团队中如果有成员使用git commit -m 'message'提交时,会提交不通过的提示

  1. $ git commit -m 'aaa'
  2. husky > commit-msg (node v8.11.3)
  3. INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !
  4. aaa
  5. husky > commit-msg hook failed (add --no-verify to bypass)

至此用cz-customizable规范git commit提交记录配置完成

记录版本发布

在之前的前端开发脚手架项目改动时,我们总是手动编写README文件,将做出的哪些改变一一列出来方便团队成员浏览知晓,后来在网上查阅参考别的项目得知我们可以通过

standard-version工具自动生成CHANGLOG文件记录版本变动

  1. 安装
  1. npm install standard-version --save-dev
  1. 修改package.json文件,在scripts中加入release命令
  1. "scripts": {
  2. ...
  3. "release": "standard-version"
  4. },

执行npm run release即可生成CHANGELOG文件,该文件中包含Features和Bug Fixes的提交记录

standard-version命令参数介绍

  1. --release-as, -r Specify the release type manually (like npm version <major|minor|patch|1.1.0>) [字符串]
  2. // major: 1.0.0 -> 2.0.0, minor: 1.0.0 -> 1.1.0, patch : 1.0.0 -> 1.0.1
  3. --prerelease, -p make a pre-release with optional option value to specify a tag id [字符串]
  4. --infile, -i Read the CHANGELOG from this file [默认值: "CHANGELOG.md"]
  5. --message, -m Commit message, replaces %s with new version [字符串] [默认值: "chore(release): %s"]
  6. --first-release, -f Is this the first release? [布尔] [默认值: false]
  7. --sign, -s Should the git commit and tag be signed? [布尔] [默认值: false]
  8. --no-verify, -n Bypass pre-commit or commit-msg git hooks during the commit phase [布尔] [默认值: false]
  9. --commit-all, -a Commit all staged changes, not just files affected by standard-version [布尔] [默认值: false]
  10. --silent Don't print logs and errors [布尔] [默认值: false]
  11. --tag-prefix, -t Set a custom prefix for the git tag to be created [字符串] [默认值: "v"]
  12. --scripts Provide scripts to execute for lifecycle events (prebump, precommit, [默认值: {}]
  13. --skip Map of steps in the release process that should be skipped [默认值: {}]
  14. --dry-run See the commands that running standard-version would run [布尔] [默认值: false]

// 第一次发布版本

npm run release -f

// 指定发布版本等级

npm run release -r minor

注意

使用standard-version生成CHANGELOG之前需要有完整的package.json文件,特别是有

  1. "repository": {
  2. "type": "git",
  3. "url": "***"
  4. }

否则生成的compare链接不完整,小编就犯过这个错

参考文章

  1. standard-version
  2. cz-customizable
  3. Husky
  4. 规范化 Git 版本提交信息和版本发布
  5. 优雅的提交你的 Git Commit Message

规范git commit提交记录和版本发布记录的更多相关文章

  1. 如何规范git commit提交

    相信很多人使用SVN.Git等版本控制工具时候都会觉得每次提交都要写一个注释有什么用啊?好麻烦,所以我每次都是随便写个数字就提交了,但是慢慢的我就发现了,如果项目长期维护或者修改很久之前的项目,没有一 ...

  2. git commit 、CHANGELOG 和版本发布的标准自动化

    一直以来,因为团队项目迭代节奏很快,每次发布的更新日志和版本更新都是通过人肉来完成的.有时候实在忙的团团转,对于手动的写这些更新信息就显得力不从心了.对于团队新来的小伙伴,有时候遇到些紧急情况,就更显 ...

  3. Git Commit 提交规范

    写好 Commit message 好处多多: 1.统一团队Git commit 日志风格 2.方便日后 Reviewing Code 3.帮助我们写好 Changelog 4.能很好的提升项目整体质 ...

  4. git commit 提交不了 error: pathspec 'project'' did not match any file(s) known to git.

    1. 问题--使用git将代码提交到码云,使用到以下命令时: git commit -m 'init project' # 报错 error: pathspec 'project'' did not ...

  5. git commit 提交失败

    git commit -m 'xxx' 报错 报错信息 当前分支:master 远程分支:gitlib.xxx error: cannot spawn .git/hooks/commit-msg: N ...

  6. windows和ubuntu下git commit提交后如何保存和退出,回到命令行

    问题一: windows下git commit后会进入vim界面,不知道怎么操作 解决办法: 1.输入小写字母i,此时进入编辑模式,可以输入你想输入的内容 2.按下esc键,此时退出编辑模式,输入英文 ...

  7. git commit 提交的时候,出现*** Please tell me who you are. git config --global 。。。问题

    $ git commit -a -m 'v6' *** Please tell me who you are. Run git config --global user.email "you ...

  8. 项目可以怎么规范Git commit ?

    通常情况下,commit message应该清晰明了,说明本次提交的目的,具体做了什么操作.但是在日常开发中,大家的commit message都比较随意,中英文混合使用的情况有时候很常见,这就导致后 ...

  9. [译]如何取消本地的git commit提交?

    git reset HEAD~1 原文来源:https://stackoverflow.com/questions/4850717/how-to-cancel-a-local-git-commit

随机推荐

  1. 【2018寒假集训 Day2】【动态规划】钱币兑换(exchange)(自己翻译的2333)

    钱币兑换(exchange) 问题描述: Dave偶然获得了未来几天的美元(dollars)与马克(marks)之间的兑换率.例如Dave开始有100marks,请编写个程序帮助Dave找出最好的买卖 ...

  2. python分支循环

    1.遍历循环 for i in range(5) for i in range (M,N,K) for c in s: for c in 'python' print(c,end="&quo ...

  3. 设计模式之工厂模式(Factory)

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/53348412 本文出自: [HansChen的博客] 设计模式系列文章: 设计模式之 ...

  4. C语言之修改常量

    前言:指针!菜鸟的终点,高手的起点.漫谈一些进阶之路上的趣事:记录一些语言本身的特性以及思想,没有STL,也没有API! 0x01: 程序内存中的存储划分 对于程序在内存中是如何分布的,网上有多个解释 ...

  5. X86架构CPU常识(主频,外频,FSB,cpu位和字长,倍频系数,缓存,CPU扩展指令集,CPU内核和I/O工作电压,制造工艺,指令集,超流水线与超标量)

    1.主频 主频也叫时钟频率,单位是MHz,用来表示CPU的运算速度. CPU的主频=外频×倍频系数.很多人认为主频就决定着CPU的运行速度,这不仅是个片面的,而且对于服务器来讲,这个认识也出现了偏差. ...

  6. div水平垂直居中的六种方法

    在平时,我们经常会碰到让一个div框针对某个模块上下左右都居中(水平垂直居中),其实针对这种情况,我们有多种方法实现. 方法一: 绝对定位方法:不确定当前div的宽度和高度,采用 transform: ...

  7. ThinkPHP5——模型(model)的使用

    在使用ThinkPHP5的过程中,我经常使用db进行数据库操作的,后来接触到了模型(model),刚使用时感觉跟db没啥区别,后来查官网知道模型基类中还提供了较多的其他的方法可以方便使用例如获取器.修 ...

  8. 漫谈边缘计算(三):5G的好拍档

    边缘计算的热度迅速攀升,还有一个不得不提的因素,就是5G的发展. [5G推动云计算从集中化向分布式演进] 在第一篇文章(<漫谈边缘计算(一):边缘计算是大势所趋>)中我提到,传统的云计算技 ...

  9. 【开发者portal在线开发插件系列三】字符串 及 可变长度字符串

    基础篇 基础场景见上面两个帖子,这里单独说明字符串和可变长度字符串的用法. 话不多说,开始今天的演(表)示(演) Profile和插件开发 添加一个string类型的属性: 在插件里添加一条数据上报消 ...

  10. Dict.setdefault()

    """ setdefault方法参数输入已有键,返回对应值,找不到已有键,创建新键,值为None """ >>> dict ...