How to Write Good Git Commit Messages like a pro

Β·

3 min read

What exactly is a Commit Message?

A commit message is a piece of descriptive text that is added by the developer who made the commit in the commit object.

Why should you write good Commit Messages?

There are several uses for commit messages:

  • Future readers will have no trouble understanding what changed and why it changed.
  • To make it easier to undo certain modifications.
  • Making changes to the release notes.

Each of these use cases require a consistent commit message style.

Commit Options

  • -m (for adding the commit message)
    git commit -m "your commit message here"
    
  • -a or --all (add and commit tracked files with a single command)

    git commit -a -m "your commit message here"
    
  • --amend (allows you to modify and add changes to the most recent commit)

    git commit --amend
    

    Note: It's great to use amend to clean up local commits, and once you're done, you can push the updated version to a shared repository. However, it's best to refrain from changing commits that have already been made public.

How to write good commit messages

Here's a great template of a good commit message.

[optional Ticket-Id] <type>[optional scope]: <description>

[optional body]
(JIRA-231) feat (dashboard): add sign-out button

----or----

build: update version 

----or----

(JIRA-333) fix (profile): dummy commit

IMPORTANT CHANGE
to show how to add body in the commit

The commit type can be

  • feat: Commits, that adds a new feature
  • fix: Commits, that fixes a bug
  • refactor: Commits, that rewrite/restructure your code, however does not change any behaviour
  • perf: Commits are special refactor commits, that improve performance
  • style: Commits, that do not affect the meaning (white-space, formatting, missing semi-colons, etc)
  • test: Commits, that add missing tests or correcting existing tests
  • docs: Commits, that affect documentation only
  • build: Commits, that affect build components like build tool, ci pipeline, dependencies, project version, ...
  • ops: Commits, that affect operational components like infrastructure, deployment, backup, recovery...
  • chore: Miscellaneous commits e.g. modifying .gitignore

The commit scope can be

A scope MUST consist of a noun describing a section of the codebase surrounded by parenthesis.

feat(profile): add button for update call

You can follow these rules for creating the great commit message

  • Limit the subject line to 50 characters
  • Capitalize the subject/description line
  • Do not end the subject line with a period
  • Separate subject from body with a blank line
  • Use the imperative mood in the subject line
  • Wrap the body at 72 characters
  • Use the body to explain what and why

Note: Commit messages with bodies are not so easy to write with the -m option. You can set VSCode as your default git editor, by execute the following command in your shell:

git config --global core.editor "code --wait"

you can add the following to the VSCode global settings.json file to set the wrap rule

"[git-commit]": {
  "editor.rulers": [72, 80]
}

Final Note

The most important part of a commit message is that it should be clear and meaningful.

I hope you enjoyed reading this article. Please share it with your friends and colleagues. If you have any resources that can help you with the above points, please share them with me and everyone else in the comments. It will be beneficial!

Thank you so much! 😊


πŸ‘‰ Follow Me: LinkedIn Twitter Github