Maintain proper commit messages

Maintain proper commit messages

Maintain proper commit messages using commitizen & husky

ยท

3 min read

I think you are a developer who loves maintaining code using Git repo and maintain task using Jira, but doesn't have any idea to write proper git commit messages ๐Ÿซฃ

Are you writing commit messages like this,

git commit -m 'task done'

or 

git commit -m 'home page impl done'

When you read your code after 2 weeks definitely know you cannot understand what you have done in the selected commit :( you need to open the code and see the changes to identify the code changes you have done ๐Ÿฅต

And also if you try to write proper commit messages you still cannot maintain all the commit messages in the same manner

If you need to maintain all your commit messages like this

feat: SP-1409 Remove feature flag usage for updates related to rebranding

Then this article is for you ๐Ÿซต


Here we are going to use commitizen for maintaining nice git commits and husky for git hooks

Steps to maintain proper git commits

Install commitizen

yarn add -D commitizen

Make your repo commitizen friendly

npx commitizen init cz-conventional-changelog --save-dev --save-exact

then you need to make run commitizen when you run git commit so for then, we need to install huskey ๐Ÿ• to run git hooks

Install huskey

yarn add -D husky

Edit package.json > prepare script and run it once:

npm pkg set scripts.prepare="husky install"
npm run prepare

Create git hook for commitizen

npx husky add .husky/prepare-commit-msg "exec < /dev/tty && node_modules/.bin/cz --hook || true"
git add .husky/prepare-commit-msg

You are done ๐Ÿ”ฅ yeeeeeee !!!

An additional step to Jira friendly

to make commits Jira-friendly we need to install to the following library

yarn add -D cz-conventional-changelog-for-jira-smart-commits

And you need to config commitizen to use the above library, create a new file .cz.json copy following lines to the file

{
  "path": "./node_modules/cz-conventional-changelog-for-jira-smart-commits",
  "skipScope": true
}

Now when you run git commit in the terminal it will ask few questions and create proper commit message, When you work with a team this will be very helpful

 ๎‚  main โ— ? ๎‚ฐ git commit                                                                                                                                                                                                                                                        ๎‚ฒ dilipchandima ๎‚ฒ 19:46:22
cz-cli@4.3.0, cz-conventional-changelog-for-jira-smart-commits@1.0.5

? Select the type of change that you're committing: feat:     A new feature
? Enter JIRA issue (AK-12345) (optional): DILEEPA-001
? Write a short, imperative tense description of the change: 
  [------------------------------------------------------------------------] 54 chars left
   feat: DILEEPA-001 add commitizen to the repo
? Provide a longer description of the change: (press enter to skip)

? Are there any breaking changes? No
? Does this change affect any open issues? Yes
? If issues are closed, the commit requires a body. Please enter a longer description of the commit itself:
 -
? Jira Issue ID(s) (required):
 DILEEPA-001
? Select the Workflow for JIRA Issue (todo, in progress, etc.):
 IN PROGRESS: Transition the issue to progress
? Time spent (i.e. 3h 15m) (optional):
 1h
? Jira comment (optional):


Commit preview:

   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚                                                  โ”‚
   โ”‚   feat: DILEEPA-001 add commitizen to the repo   โ”‚
   โ”‚                                                  โ”‚
   โ”‚   DILEEPA-001 #time 1h #in-progress              โ”‚
   โ”‚                                                  โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

? Are you sure that you want to commit? Yes
[main 70a3285] feat: DILEEPA-001 add commitizen to the repo
 1 file changed, 1 insertion(+), 1 deletion(-)
ย