|

How to write great git commit messages

what is it?

Commitizen is a handy cli tool. It helps you to comply with commit guidelines. The most popular of these is conventional commits. You can freely configure it the way you want though. See it as eslint for commits 😉

It offers an easy to use cli with meaningful descriptions for every option:
Screenshot-2021-11-24-at-14.29.42

yes please, but why?

Using commit guidelines in your projects brings many benefits. For example automagical generation of changelogs, easy semantic versioning, easier to read gitlog. Commitizen-cli removes the need to remember the options or syntax your project requires.

how to use it

You have multiple ways to use commitizen. You can install it globally or locally. For local installation and adding commit hooks see the official github.

global install

Install it as a global npm package and also install the conventional-changelog adapter (or any other of your choice).
commitizen:

yarn global add commitizen cz-conventional-changelog

Tell commitizen to use the conventional-changelog adapter:

echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

and thats it!

for extra spice:

In 2021 it's almost mandatory to use emojis in your commit messages. Your BackEnd colleagues will love it - trust me 😂

You can easily automate "emojifying" your commits with devmoji.

Install it as a global npm package:

yarn global add devmoji

Add a git template directory to your global git config:
~/.gitconfig

[init]
	templatedir = ~/.dotfiles/git/templates/

Add a template for the prepare-commit-msg hook:
~/.dotfiles/git/templates/hooks/prepare-commit-msg

#!/bin/sh

COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat $COMMIT_MSG_FILE)

echo "${COMMIT_MSG}" | devmoji > $1

This hook runs every time before you commit something. You can test drive it like this:
Screenshot-2021-11-24-at-14.22.44
If you want to add it to existing repositories you need to run git init to apply the new hook.

If you also want emojis in commits which didn't use devmoji, but follow conventional commit guideline, you can add this as an alias:
~/.dotfiles/git/aliases

alias glog='git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit --decorate --date=short --color | devmoji | head -n 20'

~/.zshrc

source ~/.dotfiles/git/aliases

Use devmoji --list to view available devmoji:
devmoji --list

Have fun using emojis in your commits 🚀