Logo
Using AI to write Commit Messages

Using AI to write Commit Messages

April 27, 2024

Writing descriptive and clear commit messages is an essential practice for any software developer. A well-crafted commit message not only helps other developers to understand what changes have been made to the codebase, but also makes it easier to track down bugs and resolve conflicts. In this blog post, I will discuss two approaches for writing effective commit messages: the Conventional Commits specification and a tool called OpenCommit.

The Conventional Commits specification is a widely-used formatting convention that provides a set of rules for structuring commit messages. By following this convention, developers can ensure that their commit messages are consistent, descriptive, and easy to understand. The convention requires that each commit message be structured as follows:

<type>[optional scope]: <description> [optional body] [optional footer(s)]

The commit type can include categories such as "feat" for new features, "fix" for bug fixes, "chore" for changes that don't modify source or test files, and so on. The commit message should be written in the imperative, present tense, and should be capitalized and not end with a period.

While following the Conventional Commits specification can be helpful, it still requires the developer to manually write the commit message. This can be time-consuming and distracting. That's where OpenCommit comes in.

OpenCommit is a tool that uses OpenAI's GPT-3.5 language model to generate commit messages automatically. To use OpenCommit, you simply install the tool and run it in the root of your project. It will then generate a commit message for you, based on the changes you've made to the code. This approach saves time and allows developers to focus on writing code, rather than crafting commit messages.

You install it via NPM installing it globally with the following command.

npm install -g opencommit

Then, you can run it in the root of your project:

opencommit

To use OpenCommit, you need to have a GPT-3 API key. This is a paid service, but it's very affordable, costing on average $0.07 USD per day. The commit messages generated by OpenCommit are descriptive, consistent, and follow the Conventional Commits specification. This makes it easy for other developers to understand what changes have been made to the codebase.

In conclusion, both the Conventional Commits specification and OpenCommit can be helpful tools for writing effective commit messages. By following the Conventional Commits specification, developers can ensure that their commit messages are structured in a consistent and descriptive manner. And by using OpenCommit, developers can save time and focus on writing code, while still generating clear and descriptive commit messages.