Writing Relevant Git Commit Comments

As developers, we often struggle a lot in writing git comments when we are committing to our code. As a result writing comments are often ignored and random comments are written.

In this short yet useful article, I will give you a starting guide on writing the code in our commits. The prerequisites of this column require the basic use of git commands, GitHub, and an IDE so let us get started.

  1. New Feature- Suppose I am working on a new feature in any application and want to publish then we can commit this using git commit -m “<custom comment>”.

Here for a new feature, we can write it as “feat: added new components”. So the final command will be as follows: -

git commit -m “feat: added new components”

2. Error Resolution- Let us face it, we can not avoid bugs. After the resolution of the bug, we can push our error resolution with the comment commit “fix:<writing the bug>”. For example, if we worked on code that results due to missing parenthesis then we do this using

git commit -m “fix : missing parenthesis

3. Refactoring — Suppose you are working on the React JSX and you are given to refactor the whole react application to typescript (TSX). Then you can take 4 to 5 files of the project convert it to React TSX, do inside file changes and commit. The reason for taking 4 to 5 files in every commit is the feasibility of it and we know what changes are being made, otherwise, it will be difficult to debug if the refactoring breaks the application.

Refactor commit comment example can be like this

git commit -m “refactor: JSX->TSX component A, B,C ”

4. Depend — You installed some npm libraries like moment.js or other libraries which are required to complete your work and want to commit that then you can use them. An example if you are given work on date and time and you added moment.js library for manipulating the dates and on day one you only added the library which is not present in the development repository and want to push the library into your repository then you can use this

git commit -m “dependency: added moment.js”

Summary

The commenting rule can be summarized as a single line

“verb: <short summary>”

Verb - The work that you are trying to do ie- (add, refactor, fix, depend)

Short summary - Short version of what you did include in a single line which is usually in the past tense.

Hope you got some new information about git comments.