Why?为什么要这么做
- 便于CodeReview
- 提高代码整体质量
- 便于以后快速想起某个分支提交了哪些内容
- 方便其他人 git blame
How,怎么去做
参考: Angular 规范的 Commit message 格式
每次提交,Commit message 都包括三个部分:Header,Body 和 Footer。
其中,Header 是必需的,Body 和 Footer 可以省略。
1. Header
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>
Header 部分只有一行,包括三个字段:type(必需)、scope(可选)和 subject(必需)。
type 用于说明 commit 的类别,只允许使用下面 7 个标识。
- feat 新功能(feature)
- fix 修补bug
- docs 文档 (documentation)
- style 格式(不影响代码运行的变动)
- refactor 重构 (即不是新增功能,也不是修改 bug 的代码变动)
- test 增加测试
- chore 构建过程、辅助工具的变动
- perf 提高性能
如果 type 为 feat 和 fix,则该 commit 将肯定出现在 Change log 之中。
scope: 用于说明commit影响的范围
subject是commit目的的简单描述,不超过50个字符
- 以动词开头,使用第一人称现在时,比如 change,而不是 changed 或 changes
- 第一个字母大写
- 不以句号结尾
2. Body
Body 部分是对本次 commit 的详细描述,可以分成多行。下面是一个范例。
More detailed explanatory text, if necessary. Wrap it to
about 72 characters or so.
Further paragraphs come after blank lines.
- Bullet points are okay, too
- Use a hanging indent
3. Footer
Footer 部分只用于不兼容变动和关闭 Issue。
推荐
- 第一行满足基本要求(采用Type+Subject格式),简要说明,紧接着为一个空行
- 注释最好包含一个连接指向你们项目的 issue/story/card。一个完整的连接比一个 issue numbers 更好
- 提交信息中包含一个简短的故事,能让别人更容易理解你的项目
- 告诉Reviewers 你提交的有哪些改变和这些变化可能影响的地方
例子
example 1:
Fix bug where user can't signup.
[Bug #2873942]
Users were unable to register if they hadn't visited the plans
and pricing page because we expected that tracking
information to exist for the logs we create after a user
signs up. I fixed this by adding a check to the logger
to ensure that if that information was not available
we weren't trying to write it.
###############################################
example 2
Redirect user to the requested page after login
https://trello.com/path/to/relevant/card
Users were being redirected to the home page after login, which is less
useful than redirecting to the page they had originally requested before
being redirected to the login form.
* Store requested path in a session variable
* Redirect to the stored location after successfully logging in the user
Attention!!!
永远不在 git commit 上增加 -m <msg> 或 --message=<msg> 参数,而单独写提交信息
一个不好的例子 git commit -m "Fix login bug"