V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
cf020031308
V2EX  ›  git

Git Commit Message 规范与模板

  •  3
     
  •   cf020031308 · 2020-03-12 21:27:15 +08:00 · 6579 次点击
    这是一个创建于 1499 天前的主题,其中的信息可能已经有所发展或是发生改变。

    业界标准:AngularJS

    来自 AngularJS 所用的规范,格式如下:

    <type>(<scope>): <subject>
    
    <body>
    
    <footer>
    

    各段内容说明:

    • type: feat/fix/docs/refactor/perf/test/style/revert/chore
    • scope: 影响范围(组件或文件),可省略
    • subject: 第一人称现在时,动词开头,小写开头,英语 50 字符以内,结尾无句号
    • body: 第一人称现在时,说明动机,与之前的对比,英语每行 72 字符以内
    • footer: 与前不兼容的改动用 'BREAKING CHANGE:' 开头,给出修复方案。对 Issues 进行操作

    自动生成 ChangeLog

    标准化的 Git Commit Message 可以用工具过滤出 feat 和 fix 的 Commit 生成 ChangeLog,例如:

    git log --format='%s (%h)' --reverse --grep '^\(feat\|fix\)' --since=2020-01-01 --before=2020-02-01 | sed 's/([^)]*):/:/' | sort -k1,1 -s
    

    可以添加到 ~/.gitconfig 里:

    [alias]
        change-of-last-month = !sh -c 'git log --format=\"%s (%h)\" --reverse --grep \"^\\(docs\\|feat\\|fix\\|perf\\|refactor\\|test\\)\" --since=`date -v-1m +\"%Y-%m-01\"` --before=`date +\"%Y-%m-01\"` | sed \"s/([^)]*):/:/\" | sort -k1,1 -s' 
    

    之后用 git change-of-last-month 就可以输出上个月的 ChangeLog。

    配置模板

    1. 自定义一个模板

    比如这是我的模板,权作 Commit 时的提醒:

    # feat/fix/docs/refactor/perf/test/style/revert/chore(scope/issue): changelog
    
    # Modify issues if necessary
    # Details if any
    

    # 开头的行会被作为注释过滤掉。

    2. 加入到 git 配置中

    git config --global commit.template path/to/template
    

    也可以在 ~/.gitconfig 中手动添加

    [commit]
        template = path/to/template
    

    3. 提交时使用模板

    之后提交时,使用 git commit 不带 -m 参数,就可以调出模板。

    19 条回复    2020-03-14 03:57:54 +08:00
    loading
        1
    loading  
       2020-03-12 21:29:57 +08:00 via Android   ❤️ 1
    我最近也在使用这个。

    如果各位实在不知道如何写,可以直接抄一个这个:
    whatthecommit.com
    hantsy
        2
    hantsy  
       2020-03-12 22:01:33 +08:00
    Good job
    labulaka521
        3
    labulaka521  
       2020-03-12 22:09:05 +08:00
    收藏了
    hallDrawnel
        4
    hallDrawnel  
       2020-03-12 22:31:11 +08:00
    我们组就在用这个
    xiqingongzi
        5
    xiqingongzi  
       2020-03-12 22:43:24 +08:00 via iPhone
    gitstyle.js.orf
    xiqingongzi
        6
    xiqingongzi  
       2020-03-12 22:43:31 +08:00 via iPhone   ❤️ 1
    Smash
        7
    Smash  
       2020-03-12 23:06:07 +08:00
    Google 的规范 Github 上应用得挺广泛的
    sunwei0325
        8
    sunwei0325  
       2020-03-12 23:33:38 +08:00
    生成 changelog 的不错, 现在用的是一个 npm 命令 conventional-changelog
    noqwerty
        9
    noqwerty  
       2020-03-13 00:11:59 +08:00 via Android
    用了一段时间 commitizen 了,挺香的,逼着自己写详细的 commit message
    xiadada
        10
    xiadada  
       2020-03-13 00:15:28 +08:00 via iPhone
    @noqwerty 这个配置好恶心
    noqwerty
        11
    noqwerty  
       2020-03-13 00:20:37 +08:00
    @xiadada #10 很方便啊,如果用 Angular 规范的话按着文档来就三行命令的事,之后就 git cz 就可以了。

    https://github.com/commitizen/cz-cli#conventional-commit-messages-as-a-global-utility
    CBS
        12
    CBS  
       2020-03-13 01:22:52 +08:00
    学习了。
    geelaw
        13
    geelaw  
       2020-03-13 03:13:11 +08:00 via iPhone
    Imperative 是“祈使语气”,而不是“第一人称”,而且根据最先的原文这个只适用于对代码改变的描述(原因描述需要其他人称和语气)。
    blueset
        14
    blueset  
       2020-03-13 07:01:08 +08:00 via Android
    zhuzhibin
        15
    zhuzhibin  
       2020-03-13 09:06:51 +08:00 via iPhone
    不是 git cz?
    RoshanWu
        16
    RoshanWu  
       2020-03-13 09:43:05 +08:00
    自动生成 ChangeLog 如果不那么追求定制化,需要比较傻瓜的方式,可以用用我的这个:
    https://github.com/roshanca/autochangelog

    这里是生成的例子:
    https://github.com/roshanca/autochangelog/blob/master/CHANGELOG.md
    FaiChou
        17
    FaiChou  
       2020-03-13 10:45:11 +08:00
    style 这种类型是对代码层面对优化 lint,还是指前端样式上的调整?
    Poarry
        18
    Poarry  
       2020-03-13 15:25:11 +08:00
    收藏了
    sepdy
        19
    sepdy  
       2020-03-14 03:57:54 +08:00 via iPhone
    我倒是觉得能表述的内容太少。gitmoji 用起来更好玩。看起来没那么“单调”
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2842 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:10 · PVG 15:10 · LAX 00:10 · JFK 03:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.