- 原文地址:Commit messages guide
- 原文做者:RomuloOliveira
- 译文出自:掘金翻译计划
- 本文永久连接:github.com/xitu/gold-m…
- 译者:Mirosalva
- 校对者:Chorer,zoomdong
一份理解 commit 信息重要性以及如何写好它们的指导手册。css
它能够帮你了解什么是 commit,为何填写好的信息说明比较重要,以及提供最佳实践、计划和(从新)书写良好的 commit 历史的一些建议。html
简而言之,commit 就是你本地仓库中文件的一个快照。 和一些人的想法相反,git 不只存储文件之间的差别,还存储全部文件的完整版本。 对于从一次提交到另外一次提交之间未发生改变的文件,git 仅存储以前已存的同一份文件的连接。git
下面的图片显示了 git 随着时间变化如何存储数据,其中每一个『版本』都是一个 commit:github
为了最大化这些好处,咱们可使用下一节描述的一些好的实践和标准。shell
这些是从个人经验、网络文章和其余指南中收集的一些实践案例。若是您有其余实践(或有不一样意见),请尽管随时打开 Pull Request 并贡献您的意见。json
# 好示例 Use InventoryBackendPool to retrieve inventory backend 复制代码
# 坏示例 Used InventoryBackendPool to retrieve inventory backend 复制代码
但为何要使用祈使形式?后端
一个 Commit 信息描述了提到的变化实际作了什么,它的影响,而非作的内容。api
这篇来自 Chris Beams 的优秀文章 给咱们一个简单的句子,能够帮助咱们以祈使形式来书写更好的 commit 信息:网络
If applied, this commit will <commit message> 复制代码
示例:app
# 好示例 If applied, this commit will use InventoryBackendPool to retrieve inventory backend 复制代码
# 坏示例 If applied, this commit will used InventoryBackendPool to retrieve inventory backend 复制代码
# 好示例 Add `use` method to Credit model 复制代码
# 坏示例 add `use` method to Credit model 复制代码
首字母须要大写的缘由是遵照句子开头使用大写字母的语法规则。
这个实践的使用可能因人而异,团队间亦可能不一样,甚至不一样语言的人群间也会不一样。 大写与否,一个重要的点是要保持标准一致而且遵照它。
# 好示例 Add `use` method to Credit model 复制代码
# 坏示例 Add `use` method 复制代码
# 好示例 Increase left padding between textbox and layout frame 复制代码
# 坏示例 Adjust css 复制代码
不少场景中(例子:屡次提交、屡次变动和重构)这都有助于帮助代码审查者理解代码提交者当时的想法。
# 好示例 修复了 InventoryBackend 子类的方法名 InventoryBackend 派生出的类没有 遵循基类接口 它之因此运行,是由于 cart 以错误的方式 调用了后端实现。 复制代码
# 好示例 Cart 中对 credit 与 json 对象间作序列化和反序列化 基于两个主要缘由将 Credit 实例转化成 dict: - Pickle 依赖于类的文件路径 若是须要重构的话咱们不想破坏任何东西 - Dict 和内建类型在默认状况下是能够经过 pickle 来序列化的 复制代码
# 好示例 Add `use` method to Credit 从 namedtuple 变成 class 是由于咱们须要使用新的值来设置属性(in_use_amount) 复制代码
提交信息的主题和正文被一个空白行分割 附加的空白行被认为是提交信息正文的一部分。
相似 -
,*
和 \
的字符是用来提升可读性的元素。
# 坏示例 Fix this Fix stuff It should work now Change stuff Adjust css 复制代码
推荐主题最多使用 50 个字符,消息体最多使用 72 个字符。
对于项目全部者:选择一个语言并使用该语言书写全部的 commit 信息。理想状况下,它应该匹配代码注释、默认翻译区域(对于作了本地化的应用)等等。
对于项目贡献者:基于已有 commit 历史书写一样语言的 commit 信息。
# 好示例 ababab Add `use` method to Credit model efefef Use InventoryBackendPool to retrieve inventory backend bebebe Fix method name of InventoryBackend child classes 复制代码
# 好示例(葡萄牙语示例) ababab Adiciona o método `use` ao model Credit efefef Usa o InventoryBackendPool para recuperar o backend de estoque bebebe Corrige nome de método na classe InventoryBackend 复制代码
# 坏示例(混合了英语和葡萄牙语) ababab Usa o InventoryBackendPool para recuperar o backend de estoque efefef Add `use` method to Credit model cdcdcd Agora vai 复制代码
这是一个样板,由 Tim Pope 编写,出如今文章高级 Git 手册。
简化变动内容到 50 字符左右或者更少 若有必要,可提供更详细的说明文字。 将它包装成大约 72 个字符左右。 在某些状况下,第一行被视为 commit 的信息主题,余下文字被认为信息正文。 将摘要和正文分离开的空白行颇有必要(除非你忽略了整个正文); 不一样的工具像 `log`、`shortlog`、`rebase`, 可能会变得混乱,若是你同时运行两个。 解释本次 commit 正在解决的问题。 专一于这次变动的缘由,而非如何变动(代码会解释这点)。 这次变动是否有反作用或其余隐性后果? 这里就是解释它们的地方。 空白行以后有更进一步的段落。 - 也能够用要点符号。 - 一般使用连字符或者星号做为要点符号, 前面有一个空格,中间有空白行, 可是约定惯例各不相同。 若是你使用问题跟踪,在底部放置它们的引用, 像下面这样: Resolves: #123 See also: #456, #789 复制代码
这节是 Atlassian 优秀教程中的一个 TL;DR,“Merge 与 Rebase”。
TL;DR: 把你的分支中的 commit 一个接一个地应用到 base 分支,生成一个新树。
TL;DR: 使用两个分支间的差别,建立新的 commit,称做(适当地)merge 提交。
我尤为更倾向于 rebase 而不是 merge,理由包含:
“Squashing” 是处理一系列 commit 并将它们压缩为一个 commit 的过程。
它在多种状况下都有用,例子:
避免在多人协做的公共 commit 或者共享分支中执行 rebase 和 squash。 rebase、squash 重写历史记录、覆盖已有 commit,在共享分支的 commit 中执行以上操做(例子,推送到远程仓库的 commit 或者来自其余分支的 commit)可能形成混淆,而且因为分歧的树干和冲突你们可能会丢失他们的变动(本地和远程的)。
使用它来压制 commit,编辑信息,重写/删除/从新排序 commit,等等。
pick 002a7cc Improve description and update document title pick 897f66d Add contributing section pick e9549cf Add a section of Available languages pick ec003aa Add "What is a commit" section" pick bbe5361 Add source referencing as a point of help wanted pick b71115e Add a section explaining the importance of commit messages pick 669bf2b Add "Good practices" section pick d8340d7 Add capitalization of first letter practice pick 925f42b Add a practice to encourage good descriptions pick be05171 Add a section showing good uses of message body pick d115bb8 Add generic messages and column limit sections pick 1693840 Add a section about language consistency pick 80c5f47 Add commit message template pick 8827962 Fix triple "m" typo pick 9b81c72 Add "Rebase vs Merge" section # Rebase 9e6dc75..9b81c72 onto 9e6dc75 (15 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into the previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out 复制代码
使用它轻松地清理 commit 而且无须一个更复杂的 rebase 操做。 这篇文章提供了如何以及什么时候这么作的很好的示例。
它很是适用于在发布到错误分支上的 commit,无须再次编码。
示例:
$ git cherry-pick 790ab21 [master 094d820] Fix English grammar in Contributing Date: Sun Feb 25 23:14:23 2018 -0300 1 file changed, 1 insertion(+), 1 deletion(-) 复制代码
假设咱们有如下差别:
diff --git a/README.md b/README.md index 7b45277..6b1993c 100644 --- a/README.md +++ b/README.md @@ -186,10 +186,13 @@ bebebe Corrige nome de método na classe InventoryBackend `` # 坏示例(混合英语和葡萄牙语) ababab Usa o InventoryBackendPool para recuperar o backend de estoque -efefef Add `use` method to Credit model cdcdcd Agora vai `` +### 样板 + +这是一个样板,[由 Tim Pope 编写](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html),出如今文章 [**高级 Git 手册**](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project)。 + ## 贡献 感谢任何形式的帮助,能够帮到个人主题示例: @@ -202,3 +205,4 @@ 感谢任何形式的帮助,能够帮到个人主题示例: - [如何书写 Git 的 Commit 信息](https://chris.beams.io/posts/git-commit/) - [高级 Git 手册 —— Commit 指导](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines) +- [A Note About Git Commit Messages](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) 复制代码
咱们可使用 git add -p
来只添加咱们须要的补丁,无须修改已经编写的代码。 将较大的变动拆分红小的 commit 或者重置/检出特殊的变动。
暂存这个区块 [y,n,q,a,d,/,j,J,g,s,e,?]? s 拆分红 2 个区块 复制代码
@@ -186,7 +186,6 @@ `` # 坏示例 (mixes English and Portuguese) ababab Usa o InventoryBackendPool para recuperar o backend de estoque -efefef Add `use` method to Credit model cdcdcd Agora vai `` 暂存这个区块 [y,n,q,a,d,/,j,J,g,e,?]? 复制代码
@@ -190,6 +189,10 @@ `` cdcdcd Agora vai `` +### 样板 + +这是一个样板,[由 Tim Pope 编写](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html),出如今文章 [**高级 Git 手册**](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project)。 + ## 贡献 感谢任何形式的帮助,能够帮到个人主题示例: 暂存这个区块 [y,n,q,a,d,/,K,j,J,g,e,?]? 复制代码
@@ -202,3 +205,4 @@ 感谢任何形式的帮助,能够帮到个人主题示例: - [如何书写 Git 的 Commit 信息](https://chris.beams.io/posts/git-commit/) - [高级 Git 手册 —— Commit 指导](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines) +- [关于 Git 的 Commit 信息的注意事项](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) 复制代码
感谢任何形式的帮助,能够帮到个人主题示例: