Git commit 规范

4,484 阅读3分钟

为什么要规范git commit

首先看看目前项目的git commit记录,主要存在以下几个问题:

  1. 没有统一规范,全看个人自由发挥
  2. 有些commit信息很模糊,比如update
  3. 无明确的分类,需求、修复bug、优化不清晰
  4. commit影响范围未知

我们再看看社区里面比较流行的Angular规范的commit记录: Angular规范的commit看完感觉更加的一目了然,分类清晰,方便快速浏览。还可以通过过滤某些commit分类,快速生成change log。

git commit 规范

参考Angular团队的commit规范,结合项目实际情况,制定以下commit规范。

  1. commit message格式如下:
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>

type和subject必需,scope、body、footer可选。

  • type:commit的类型,有以下几种:
type描述
feat新增feature
fix修复bug
docs修改文档,如readme.md
style修改代码格式,不改变代码逻辑,如逗号、缩进、空格等
refactor代码重构,没有新增功能或修复bug
perf优化相关,如提升性能、用户体验等
test测试用例,包括单元测试、集成测试
ci修改ci配置文件或脚本,如jenkins fastlame
chore修改构建脚本、或者增加依赖库、工具等
revert回滚之前的commit

scope:commit影响的范围,可以是影响的文件名、模块名、组件名、国家等

subject:commit的简短描述,符合50/72 formatting

body:commit的详细描述,符合50/72 formatting

footer:备注,通常是Breaking changes或者Closed issues

subject限定50字符,加上type和scope,我们限定head不超过60个字符。

  1. revert是一种特殊的commit,用于回滚前面的commit,标准的message格式为:
revert: fix: home banner crash #12345
    
This reverts commit aa7b6ed2d93bd9b79faccf1ac9d086c98c52141f.

考虑到SourceTree和git revert命令默认生成的message如下,也允许这种格式的提交。

Revert "fix: home banner crash #12345"
    
This reverts commit aa7b6ed2d93bd9b79faccf1ac9d086c98c52141f.
  1. fix类型的提交,要求在footer带上bug号或bug链接。
fix(home): 分类商品图片错乱

RecyclerView复用引起

Closes https://www.tapd.cn/*****************

git commit 模板

为git设置commit template,每次git commit的时候在vim中带出,时刻提醒自己。

修改~/.gitconfig,添加:

[commit]
template = ~/.gitmessage

新建~/.gitmessage文件,内容如下:

# GIT COMMIT MESSAGE FORMAT
#
# <type>(<scope>): <subject>
# <BLANK LINE>
# <body>
# <BLANK LINE>
# <footer>
#
# head
# - type: feat, fix, docs, style, refactor, perf, test, chore, revert
# - scope: scope of this change, can be empty
# - 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 bug, if any.
# - BREAKING CHANGE
#

配置好之后,我们在终端或者SourceTree发起commit,都会带出模板信息。

Android Studio可以使用Git Commit Template插件提交。

git hook 检查

对于前端开发同学来说,机器上有node环境,可以很方便的使用commitizen和commitlint来检查commit message格式。

没有note环境,就只能通过git hook来检查了。

以下是一段检查的shell脚本,拷贝到工程目录下.git/hooks/commit-msg文件中,并chmod +x .git/hooks/commit-msg赋予执行权限。

#!/bin/bash
################################################################################
# Store this file as .git/hooks/commit-msg in your repository in order to
# enforce checking for proper commit message format before actual commits. You
# may need to make the script executable by 'chmod +x .git/hooks/commit-msg'.
################################################################################
filename="$1"
lineno=0

error() {
    echo "CHECKIN STOPPED DUE TO INAPPROPRIATE LOG MESSAGE FORMATTING!"
    echo "$1"
    echo ""
    exit 1
}

while read -r line
do
    # Ignore comment lines (don't count line number either)
    [[ "$line" =~ ^#.* ]] && continue

    let lineno+=1
    length=${#line}

    # Subject line tests
    if [[ $lineno -eq 1 ]]
    then
        # Compatible with "Merge*************"
        [[ "$line" =~ ^Merge.* ]] && continue

        # Compatible with "Revert *************"
        [[ "$line" =~ ^Revert.* ]] && continue

        [[ $length -gt 60 ]] && error "Limit the subject line to 60 characters"

        type=$(echo $line | awk 'BEGIN{FS=":"} {print $1}')
        [[ ${#type} -eq 0 ]] && error "Type cann't be empty"

        if [[ ! "$type" =~ ^(feat|fix|docs|style|refactor|perf|test|chore|revert|ci|build)$ && ! "$type" =~ ^(feat|fix|docs|style|refactor|perf|test|chore|revert|ci|build)\(.*\)$ ]]
        then
            error "Type must be [feat,fix,docs,style,refactor,perf,test,chore,revert,ci,build]"
        fi
    fi

    # Rules related to the commit message body
    [[ $lineno -eq 2 ]] && [[ -n $line ]] && error "Separate subject from body with a blank line"
    [[ ! $lineno -gt 1 ]] && [[ $length -gt 72 ]] && error "Wrap the body at 72 characters"
done < "$filename"

exit 0

我们再xjb commit看看:

git commit -m "Just a simple commit"

CHECKIN STOPPED DUE TO INAPPROPRIATE LOG MESSAGE FORMATTING!
Type must be [feat,fix,docs,style,refactor,perf,test,chore,revert,ci,build]
git commit -m "Long long long long long long long long long long long long long long long long long long long long long long long long long long long"

CHECKIN STOPPED DUE TO INAPPROPRIATE LOG MESSAGE FORMATTING!
Limit the subject line to 60 characters

完美,这下不能瞎提交了。

shell脚本?windows咋办?bat不会写,后面再说吧。

参考文献