Organize Your Git Commits Using Commitizen
Tags: github, tutorial, codenewbie, discuss
Published: 2021-06-01 09:15:24

Git commit messages plays an important role in the life of developers. If your git commit messages are clean and organized you can easily understand and refer to previous code changes using commit messages itself. It helps other teammates as well to visualize the evolution of the feature development or bug fixes step by step easily.

In this blog post, I will explain more about a tool called  [commitizen](https://commitizen-tools.github.io/commitizen/)  which can help your team to adopt to writing organized git commit messages with less effort.

### What is Commitizen?

Commitizen is a tool designed for teams.

Its main purpose is to define a standard way of committing rules and communicating them using the CLI provided by commitizen.

The reasoning behind it is that it is easier to read, and enforces writing descriptive commits.

Besides that, having a convention on your commits makes it possible to parse them and use them for something else, like generating automatically the version or a changelog.


### Features

- Command-line utility to create commits with your rules. By Default, it uses  [Conventional commits](https://www.conventionalcommits.org/) rules for commit messages.
- Display information about your commit rules (commands: schema, example, info)
- Bump version automatically using semantic versioning based on the commits. It uses semantic versioning means it uses MAJOR.MINOR.PATCH format for versioning the releases.

<table border=1>
   <tr>
      <th> Increment </th>
      <th> Description </th>
      <th> Conventional commit map </th>
   </tr>
  <tr>
     <td> MAJOR </td>
     <td> Breaking changes introduced  </td>
     <td> BREAKING CHANGE </td>
  </tr>
  <tr>
     <td> MINOR </td>
     <td> New features  </td>
     <td> feat </td>
  </tr>
  <tr>
     <td> PATCH </td>
     <td> Fixes  </td>
     <td> fix + everything else </td>
  </tr>
</table>

- Generate a changelog using  [Keep a changelog](https://keepachangelog.com/en/1.0.0/). 

### Installation

#### macOS

On macOS, it can be installed via homebrew:

```
brew install commitizen
```

#### Python

On other operating systems, it can be installed via `pip`

##### Global Installation

```
sudo pip3 install -U Commitizen
```
##### Local Project Installation

```
pip install -U commitizen
```
### Git Commit using Commitizen

After installing commitizen you can run `cz commit` inside your git repository and it will show the following options.

```
➜  course-tracker git:(master) ✗ cz commit

? Select the type of change you are committing  (Use arrow keys)
 » fix: A bug fix. Correlates with PATCH in SemVer
   feat: A new feature. Correlates with MINOR in SemVer
   docs: Documentation only changes
   style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
   refactor: A code change that neither fixes a bug nor adds a feature
   perf: A code change that improves performance
   test: Adding missing or correcting existing tests
   build: Changes that affect the build system or external dependencies (example scopes: pip, docker, npm)
   ci: Changes to our CI configuration files and scripts (example scopes: GitLabCI)
```

If you observe you can see 9 options that cover most of the activities in the development lifecycle starting from 
- features
- bug fixes
- documentation
- code styling
- refactor
- performance improvement
- unit/integration testing 
- prepare build
- changes for CI pipelines

In this example, I will select `docs` as I made few minor changes to my README file in my project. Also updated remaining steps like adding 
- `subject` for the changes in the commit.
- `is breaking change?` in this example it's simple change.

```
➜  course-tracker git:(master) ✗ cz commit

? Select the type of change you are committing  docs: Documentation only changes
? Scope. Could be anything specifying place of the commit change (users, db, poll):
 project-setup
? Subject. Concise description of the changes. Imperative, lower case and no final dot:
 Update Setup Instructions
? Body. Motivation for the change and contrast this with previous behavior:

? Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer  No
? Footer. Information about Breaking Changes and reference issues that this commit closes:


docs(project-setup): Update Setup Instructions

 1 file changed, 1 insertion(+)

Commit successful!
```
 
Now, our git log will look like this below.

```
commit 22d248315af3176952d56dd0cb712667900a37ba (HEAD -> master)
Author: Vishnu Chilamakuru 
Date:   Tue Jun 1 14:20:23 2021 +0530

    docs(project-setup): Update Setup Instructions
```

My git commit message is properly organized now for corresponding subjects and use-cases and it is easy to refer to my project git timeline to refer to features, fixes, docs, etc...Below is the sample timeline of  [the repository](https://github.com/commitizen/conventional-commit-types/commits/master)  using commitizen. 


![git-commits-using-commitizen.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1622537851181/2jswo6eRK.png)

Hope you like the article.

<hr>

## Thank you for reading
Hope you find these resources useful. If you like what you read and want to see more about system design, microservices, and other technology-related stuff... You can follow me on 

- Twitter - <a href="https://twitter.com/vishnuchi?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-size="large" data-show-count="false">Follow @vishnuchi</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
- Subscribe to my weekly newsletter [here](https://www.getrevue.co/profile/vishnuch).

<hr>
<a href="https://www.buymeacoffee.com/vishnuchi" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 20px !important;width: 100px !important;" ></a>