git 原理详解及实用指南_如何编写良好的提交消息:实用的Git指南

git 原理详解及实用指南

To create a useful revision history, teams should first agree on a commit message convention to use. This also applies to personal projects.

要创建有用的修订历史记录,团队应该首先同意要使用的提交消息约定。 这也适用于个人项目。

Recently on Hashnode I asked, "Which commit message convention do you use at work?" and I got some amazing responses with users explaining the conventions they use at work and for their personal projects.

最近在Hashnode上,我问: “您在工作中使用哪种提交消息约定?” 我收到了一些用户的好评,解释了他们在工作中和用于个人项目时所使用的约定。

In this article, I'll go over how to write good commit messages and why you should.

在本文中,我将介绍如何编写良好的提交消息以及为什么要这样做。

PS:本文首次发表在我的博客在这里 (PS: This article was first published on my blog here.)

Git版本控制简介 (Introduction to version control with Git)

Version control software is an essential part of modern-day software developer practices.

版本控制软件是现代软件开发人员实践的重要组成部分。

By far, Git is the most widely used version control system in the world. It is a distributed and actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel.

到目前为止, Git是世界上使用最广泛的版本控制系统。 它是一个分布式且积极维护的开源项目,最初由Linux操作系统内核的著名创建者Linus Torvalds于2005年开发。

New to Git? Check out the official getting started guide or this slide from a past talk I gave.

刚接触Git? 请查看官方的入门指南或我以前的演讲中的这张幻灯片

什么是提交消息? (What is a commit message?)

The commit command is used to save changes to a local repository after staging in Git. However, before you can save changes in Git, you have to tell Git which changes you want to save as you might have made tons of edits. A great way to do that is by adding a commit message to identify your changes.

在Git中暂存后,使用commit命令将更改保存到本地存储库。 但是,在Git中保存更改之前,您必须告诉Git您要保存哪些更改,因为您可能已经进行了大量的编辑。 做到这一点的一种好方法是添加一个提交消息以标识您的更改。

提交选项 (Commit Options)

  • -m

    -米

This option sets the commit's message.

此选项设置提交的消息。

git add static/admin/config.yml
git commit -m "Setup multiple roles for netlify-cms git gateway"
  • -a or --all

    -a或--all

This option automatically commits all (including new) tracked, modified or deleted files.

此选项自动提交所有(包括新的)跟踪,修改或删除的文件。

git commit -a -m "Add a new role for netlify-cms git gateway"
  • --amend

    - 修改

This option rewrites the very last commit with any currently staged changes or a new commit message and should only be performed on commits that have not been pushed to a remote repository, yet.

此选项将使用任何当前暂存的更改或新的提交消息重写最后的提交,并且应仅对尚未推送到远程存储库的提交执行此操作。

git add .
git commit --amend -m "Update roles for netlify-cms git gateway"

为什么要编写良好的提交消息? (Why should you write good commit messages?)

You might say, "It's just a personal project." Yes, you work alone now, but what happens when you work with a team or contribute to open source?

您可能会说:“这只是一个个人项目。” 是的,您现在一个人工作,但是当您与团队一起工作或为开源做出贡献时会怎样?

A well-crafted Git commit message is the best way to communicate context about a change to other developers working on that project, and indeed, to your future self.

精心设计的Git提交消息是与正在进行该项目的其他开发人员以及您未来的自我交流有关更改的上下文的最佳方法。

Have you ever tried running git log on one of your old projects to see the "weird" commit messages you have used since its inception? It can be hard to understand why you made some changes in the past, and you'll wish you read this article earlier :).

您是否曾经尝试在您的一个旧项目上运行git log来查看自创建以来使用的“怪异”提交消息? 很难理解为什么您过去进行了一些更改,并且希望您早些阅读本文:)。

Commit messages can adequately communicate why a change was made, and understanding that makes development and collaboration more efficient.

提交消息可以充分传达进行更改的原因,以及可以使开发和协作更加有效的理解。

如何使用Git编写提交消息 (How to write commit messages with Git)

Before now, I only used git commit -m "Fix X to allow Y to use Z" on my personal projects with just a subject and no extra description. This is great for small and clear fixes like git commit -m "Fix typo in README.md, but in cases of more extensive changes, you would need to add some extra details.

在此之前,我只在个人项目上使用了git commit -m "Fix X to allow Y to use Z" ,并且仅提供了一个主题,没有附加描述。 这对于小而清晰的修复非常git commit -m "Fix typo in README.md ,例如git commit -m "Fix typo in README.md ,但是在更广泛的更改的情况下,您需要添加一些额外的细节。

编辑器方法 (Editor method)

Run git commit without a message or option and it'll open up your default text editor to write a commit message.

运行没有消息或选项的git commit ,它将打开您的默认文本编辑器以编写提交消息。

To configure your "default" editor:

配置“默认”编辑器:

git config --global core.editor nano

This would configure Git to use nano as your default editor. Replace "nano" with "emacs," "vim," or whatever your preference is.

这会将Git配置为使用nano作为默认编辑器。 用“ emacs”,“ vim”或任何您喜欢的名称替换“ nano”。

In the opened editor, the first line is the subject (short description), leave a blank line after it, and everything else is the extended description (body).

在打开的编辑器中,第一行是主题(简短描述),其后留空行,其他所有内容都是扩展描述(正文)。

<Summarize change(s) in around 50 characters or less>

<More detailed explanatory description of the change wrapped into about 72
characters>

命令行方式 (Command Line method)

git commit -m "Subject" -m "Description..."

The first -m option is the subject (short description), and the next is the extended description (body).

第一个-m选项是主题(简短描述),第二个是扩展描述(正文)。

如何编写良好的提交消息 (How to write good commit messages)

There are several conventions used by different teams and developers to write good commit messages. I'll only outline some general rules and tips for writing commit messages–you have to decide what convention you want to follow. And if you work for a company or contribute to open source, you have to adapt to their convention :).

不同的团队和开发人员使用几种约定来编写良好的提交消息。 我将仅概述一些用于编写提交消息的一般规则和技巧-您必须确定要遵循的约定。 而且,如果您在公司工作或为开源做贡献,则必须适应他们的惯例:)。

For consistency, you can use one convention for work and another for personal projects as you might change jobs sometime, and the convention might also change.

为了保持一致性,您可以在工作中使用一种约定,而在个人项目中使用另一种约定,因为您有时可能会更改工作,并且约定也可能会更改。

Be sure to check out this thread for some amazing commit message conventions or add yours to help someone make a decision.

一定要签出该线程以了解一些惊人的提交消息约定,或者添加您的约定以帮助某人做出决定。

Here's a great template of a good commit message originally written by Tim pope

这是Tim Pope最初编写的良好提交消息的一个很好的模板

Capitalized, short (50 chars or less) summary

More detailed explanatory text, if necessary.  Wrap it to about 72
characters or so.  In some contexts, the first line is treated as the
subject of an email and the rest of the text as the body.  The blank
line separating the summary from the body is critical (unless you omit
the body entirely); tools like rebase can get confused if you run the
two together.

Write your commit message in the imperative: "Fix bug" and not "Fixed bug"
or "Fixes bug."  This convention matches up with commit messages generated
by commands like git merge and git revert.

Further paragraphs come after blank lines.

- Bullet points are okay, too

- Typically a hyphen or asterisk is used for the bullet, followed by a
  single space, with blank lines in between, but conventions vary here

- Use a hanging indent

If you use an issue tracker, add a reference(s) to them at the bottom,
like so:

Resolves: #123

Looks great, right? Here's how you can make yours great too:

看起来不错吧? 您也可以通过以下方法使自己的实力变得出色:

  1. Specify the type of commit:

    指定提交的类型:
  • feat: The new feature you're adding to a particular application

    壮举:您要添加到特定应用程序的新功能
  • fix: A bug fix

    修复:一个错误修复
  • style: Feature and updates related to styling

    样式:与样式相关的功能和更新
  • refactor: Refactoring a specific section of the codebase

    重构:重构代码库的特定部分
  • test: Everything related to testing

    测试:与测试相关的所有内容
  • docs: Everything related to documentation

    docs:与文档相关的所有内容
  • chore: Regular code maintenance.[ You can also use emojis to represent commit types]

    杂务:常规代码维护。[您也可以使用表情符号来表示提交类型]
  1. Separate the subject from the body with a blank line

    用空白行将主体与身体分开
  2. Your commit message should not contain any whitespace errors

    您的提交消息不应包含任何空格错误
  3. Remove unnecessary punctuation marks

    删除不必要的标点符号
  4. Do not end the subject line with a period

    主题行不要以句号结尾
  5. Capitalize the subject line and each paragraph

    大写主题行和每个段落
  6. Use the imperative mood in the subject line

    在主题行中使用命令式心情
  7. Use the body to explain what changes you have made and why you made them.

    用正文解释您进行了哪些更改以及为什么进行更改。
  8. Do not assume the reviewer understands what the original problem was, ensure you add it.

    不要假定审阅者了解原始问题是什么,请确保将其添加。
  9. Do not think your code is self-explanatory

    不要以为您的代码是不言自明的
  10. Follow the commit convention defined by your team

    遵循团队定义的提交约定

结论 (Conclusion)

The most important part of a commit message is that it should be clear and meaningful. In the long run, writing good commit messages shows how much of a collaborator you are. The benefits of writing good commit messages are not only limited to your team, but indeed expand to yourself and future contributors.

提交消息最​​重要的部分是它应该清晰而有意义。 从长远来看,编写良好的提交消息可以表明您有多少协作者。 编写良好的提交消息的好处不仅限于您的团队,而且的确扩展到您自己和将来的贡献者。

Want to learn more about Git and become a professional "version controller"? Check out these excellent resources:

想更多地了解Git并成为专业的“版本控制器”吗? 查看以下优秀资源:

翻译自: https://www.freecodecamp.org/news/writing-good-commit-messages-a-practical-guide/

git 原理详解及实用指南

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值