关于git:Git-约定式提交

4次阅读

共计 2546 个字符,预计需要花费 7 分钟才能阅读完成。

概述

每次提交代码的时候,咱们都须要为咱们本次批改的内容增加一段形容,例如:

git commit -m "Initial commit";
git commit -m "修复了一些已知问题。";
git commit -m "减少了新个性。";

但实际上有些 commit message 千奇百怪,比方以下这种:


一次 commit 应该精确的阐明本次提交的目标和批改内容,比方:

git commit -m "chore: upgrade org.bouncycastle:bcprov-jdk15on from 1.69 to 1.70";
git commit -m "perf: optimize the code on global zookeeper";
git commit -m "docs(readme): fix typo";

这种写法来源于 Angular 团队的 Git 约定式提交,你能够从以下链接来浏览它们的标准:

  1. Git Commit Message Conventions
  2. Understanding Semantic Commit Messages Using Git and Angular
  3. Angular 提交信息标准
  4. 约定式提交

应用约定式提交

约定式提交约定了 commit message 的格局,如下所示:

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LIKE>
<footer>

提交音讯由 header、body、footer 组成,咱们来具体介绍它们。

Message header

header 由 type、scope、subject 组成,在残缺的约定式提交格局,它占据的局部是:

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

所代表的含意是:

  • type: 必选,本次提交的类型。
  • scope: 可选,示意本次提交影响的范畴。
  • subject: 必选,对本次提交简短的形容。

    type

    type 的值必须是以下列表中的一个:

  • build/chore👷‍♀️: 用于构建零碎(包含脚本、配置或工具)和依赖的变动。
  • ci: 用于零碎继续集成、继续部署相干的配置文件、脚本文件、配置或者工具。
  • docs📝: 用于标识我的项目相干文档的更改。
  • feat✨: 用于标识新性能。
  • fix🐛: 用于标识 bug 修复。
  • perf⚡️: 用于标识性能晋升。
  • refactor: 用于标识代码重构,既不增加新性能也不修复谬误 – 例如删除冗余代码、简化代码、重命名变量等。
  • style: 用于标记代码格式化,代码格调调制,修复 checkstyle 等问题。
  • tets: 用于标记测试相干的更改,批改现有测试或增加新的测试。
  • revert: 用户分支回滚。
    用一张图阐明:
scope

scope 能够依照模块、包进行划分,具体应用可依据团队制订的标准,例如:

git commit -m "feat(logger): support JSONL log output format";
git commit -m "feat(dao): add global controller excepiton handler";
git commit -m "feat(api): add reqeust interceptor"
subject

对本次提交简短的形容,有以下准则:

  1. 首字母小写。
  2. 不以句号结尾。
示例

header 只是 commit message 的第一行,能够应用 git commit -m 来实现本次提交,例如:

git commit -m "chore(pom.xml): upgrade jackson-databind from 2.10.1 to 2.11.4"

上述例子中提交的 type 是 chore,紧接着在 () 两头指定了 scope 影响的范畴或者说批改的文件是 pom.xml,而后跟上一个英文 : 与一个空格就是咱们的 subject。须要留神的的是 : 是英文的,在 : 之后还须要一个空格与 subject 作为宰割。

之前提到过 scope 是可选的,如果咱们疏忽 scope,那么上述例子能够改为:

git commit -m "chore: upgrade jackson-databind from 2.10.1 to 2.11.4"

Message body

body 是可选的,能够介绍批改的起因以及具体的内容:

fix(users): remove getFriends API endpoint

In accordance with new privacy policy, remove the getFriends API endpoint. 
(The endpoint will continue to exist for a while and return a deprecation notice)

须要留神的是,在 header 与 body 之前须要有一个空行。

Message footer

footer 也是可选的,如果本次提交是批改 issue 的话,那么能够在页脚援用 issue,例如:

fix(users): remove getFriends API endpoint

In accordance with new privacy policy, remove the getFriends API endpoint. 
(The endpoint will continue to exist for a while and return a deprecation notice)

Implements #2105

在 footer 与 body 之间也须要一个空行。

带来的益处

生成 changelog

浏览记录

咱们能够浏览指定 type 的提交记录,例如显示 feat、fix、perf 的提交记录:

git log --oneline --grep "^feat|^fix|^perf"

commit message emoji

将 emoji 增加到 commit message 中会使咱们的提交更加乏味,也不便浏览,例如:


你能够在以下链接找到 commit message type 对应的 emoji:

  1. https://gitmoji.dev/
  2. commit-message-emoji
  3. GitCommitEmoji
  4. why-i-use-emojis-in-my-git-commits
  5. how-to-use-emoji-in-your-commit-message

如果你应用的是 VS Code 编辑器或者你应用的包管理器是 npm,那么你还能够找到对应的插件:

  1. npm-gitmoji-cli
  2. gitmoji-vscode
正文完
 0