Tech Blog
GitGitHubSoftware Engineering

Writing Good Commit Messages 🎉

Nov 27, 20223 min read
Writing Good Commit Messages 🎉

Let's start with what is a commit in git?

Commit in git is used to save a snapshot of all the changes made in the working repository.

A commit message typically contains

  • Subject line
  • Body (opt)
  • Footer (opt)

Now, how to write a good subject line?

The subject line is a 1 line description of the entire change you made. This should not cross 50 chars.

To incorporate this, subject lines are defined in this syntax

markdown
1<type>(optional scope): <description>

Let's break these 3 👇

Type:

Type basically defines what type of change are you making.

These are a few types defined :

  • feat
  • fix
  • docs
  • style
  • refactor
  • test

Type is very important in every commit, it helps the other person know the kind of change made to the codebase.

Defining each type:

  • feat: A new feature is introduced with the changes made.
  • fix: A bug is fixed with the changes made.
  • docs: Changes related to documentation are made.
  • style: Changes related to formatting, missing semi colons, etc. are made. These changes do not affect the meaning of the code.
  • refactor: Changes related to refactoring the code are made. These changes do not affect the meaning of the code.
  • test: Changes related to adding or modifying tests are made.

Optional Scope in the Subject line

This scope provides context & should be the name of the core component changed, not necessarily the file name.

For example, If you write a new REST API, then the scope will be REST API and not the name of the file where you wrote the API.

Description in Subject line

The description is the most important part of a commit message.

  • Give a small & apt description of the change
  • Use present tense to describe the change.
  • Don't capitalize the 1st letter & don't end with a dot.

Body of a commit message (Optional)

This is an optional field, you can choose to add this if the change needs more explanation. To add a body you need to add a blank line after the Subject line and then start to write the body. This is usually used to inform a breaking change.

Footer of a commit message (Optional)

Footer is also an optional field, which is usually used to add the JIRA ticket ids or Github Issue references.

Clubbing everything you just learned

Examples :

markdown
1feat(cart): add a checkout button

OR

markdown
1fix(auth): resolve login issue

👆 The above is a git commit message with only the subject line

With body & footer👇

markdown
1feat(cart): add a checkout button
2 
3Added checkout button, which will now redirect to the payments page with all the relevant data
4 
5Issue #315

That’s it, folks.