Write good Git commit message

For a long time, I didn’t even know there were “best practices” for writing Git commit messages. When I first encountered Git, the commit information was described as “here, you can write a short message about the intent of the commit.”

Inferior submission information

See the submission information below. If you want to merge them, you really don’t know what’s added and what’s changed, what they do, or why you need these commits. The same is true if you’re searching for something in history. You scroll down to submit your history. It’s a waste of time.

cd3e27a contact page
aee9d0d comments
eac95e5 list of online users, some other changes because of server
fae5636 little edit
Copy the code

Quality submission information

Now, look at these submissions again, don’t you feel better? That’s what I think, anyway.

43ec6aa Fix error when the URL is not reachable
4fe84ab Add error message if something went wrong
753aa05 Add server fingerprint check
df3a662 Fix shadow box closing problem
Copy the code

How to write good submission information

A complete COMMIT message should have its format — a topic, a body, and an optional conclusion made up of solved/closed issues.

Subject

Git’s Commit help page has a nice description of the subject of the commit information: a single line of text (less than 50 characters) summarizing the changes, followed by a blank line. The subject should begin with a capital letter and not end with a period. And, importantly, it has to be a form of coercion. Chris Beams wrote a simple rule for this:

Git commit information topics should always be formatted in such a way that if a commit is applied, the commit will “…” . Such as:

Replace the content in double quotes with the topic you wrote for this submission, which should be an action.

  • If applied, the commit willDelete unnecessary files.
  • If applied, the commit willAdd (Add)grepoptions.
  • If applied, the commit willFix an error when protocol is missing.

For bad submissions, this would not be true:

  • If applied, the commit willContact page.
  • If applied, the commit willlist of online users, some other changes because of server.

Git projects themselves use this approach. When you Merge something, Git generates a commit message that looks like this: “Merge branch…” “Or “Revert…” when rolled back. .

Body (Body)

In the body you can write what was changed and why. Each line of text should not exceed 72 characters. Of course, not every submission needs to have a body message.

Bottom line

Finally, you can add the fixed or related issue of the commit. This can be a link, a number or, if you are using GitHub, you can write: conversion #N/watches #N, where the N stands for issue ID.

The sample

This is a submission from my personal warehouse:

Fix error when protocol is missing

First, it checks if the protocol is set. If not, it changes the url and
add the basic http protocol on the beginning.
Second, it does a "preflight" request and follows all redirects and
returns the last URL. The process then continues with this URL.

Resolves #17
Copy the code

Write in the last

Thank you for reading and I hope you learned something new. If you have other tips on how to write better commit messages, or how to use this tool better, please leave a comment.

Generating change logs

Another benefit of writing commit information this way is that it is easy to generate change logs.

# show whole commit history
$ git log --oneline --decorate --color

# show history from one tag to another
$ git log0.0.9.. 0.0.10 - oneline, decorate, color

# show history form tag to headThe git log 0.0.9.. HEAD --oneline --decorate --colorCopy the code

The result of this command is your commit list.

21629ee Fix getResources bug
aa13384 Update docs
44de44c Add HSTS check
Copy the code

Commitizen

There’s a command line tool on GitHub called Commitizen that makes it easier to write submissions. When you want to submit, you just type git Cz and it will ask you a set of questions and then create the correct submission information for you.

reference

Git commit manpage

Closing issues using keywords on GitHub

How to Write a Git Commit Message post from Chris Beams

Angular Commit Message Format