Blog Logo

I'm Never Writing a Commit Again 🄸

Introduction

If you read it right, I’m never writing a commit again, well, that’s not entirely true, I am going to write it, but I’m going to worry less about writing it… I’m going to do them in a lighter, more carefree way.

Don’t know what I mean? Well, I’m going to go from this image.webp to this image.webp

without doing anything, just writing something basic like ā€˜changes’ or not even that.

The importance of a good commit message

Committing is something very simple that we do constantly every day, but do we give it the relevance it deserves? Let me talk to you about the importance of commits,

You might think it’s no big deal, but I have to tell you that a good commit message is essential to keep a project organized and understandable. We all know that in software development a large part of time is spent on maintaining and reading code.

Imagine how much that task can be facilitated with clear and descriptive commit messages.

In addition, in group work environments, where there is more than one person, a good commit history is like an open book of the project’s evolution. And well, even if you are programming alone. All this helps you understand why certain changes were made and also facilitates teamwork.

My solution

I don’t think programmers write bad commits or that there is a problem at a general level, but I do understand that often, we are so focused on the code that we don’t stop to write a perfect commit message. Either because we want to upload it quickly to check that the pipeline passes or for any other reason that distracts us when committing.

That’s why I created a script, super basic, that every time you make a commit, passes your message through the free Gemini API and rewrites it following the rules you specify, that is, a custom prompt.

The script uses a Python dependency that fetches Gemini in the background, it is easy to use because it only needs your Gemini API key, but the code could be modified to fetch your favorite API of any LLM.

The ā€œmagicā€ for it to execute every time we commit is found in the git hooks, you don’t have to worry about that, since I left another script in the code that adds the necessary hook to execute our code to the projects in a directory you specify. Apart from that, it modifies the configuration so that in new projects this applies by default, so it would never be necessary to execute it again.

Customization

And best of all, this system is fully configurable. You can adjust the prompt to follow your company’s standards, your team’s or simply the ones you prefer. For example, I have set it to follow Conventional Commits standards, but you can literally tell it to reply with insults or a very unprofessional phrase.

In addition, I have added a flag that is very easy to modify in the script in case you also want the modified code (i.e. the git diff) to be passed to the API. This is useful if you want the commit message to have context about the changes made. For example, it could summarize the most important changes or highlight a key function you have implemented.

Of course, I put it in a flag in the code because I understand that due to privacy or security issues it is not possible to send the code to an external API. No problem, just don’t activate the flag and the script will only work with the message you provide, without sharing any code snippets.

Although well, according to Google’s privacy policies, Gemini does not use conversation data to train its models, but that’s up to you, I’m not getting into that.

image.webp

I leave you more info about that here

https://cloud.google.com/vertex-ai/generative-ai/docs/data-governance?hl=es-419

https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/multimodal-faqs?hl=es-419

And what do I gain from all this?

Automating commit message generation not only saves you time, but also reduces cognitive load. Our brain has a limited capacity to make decisions and concentrate on complex tasks. By delegating repetitive and less critical tasks, such as polishing commit messages, we free up mental resources for what really matters: solving problems and writing quality code.

Nah, actually I don’t know, I was just bored and it seemed entertaining and sometimes I’m lazy to commit properly… that’s all.

Code

šŸ‘‰ https://github.com/edunavajas/git-commit-enhancer

I’m not going to go into low-level detail about what you have to do to install it because it says everything in the README, but basically, for the script to work you must first install the dependencies which is done from the file called dependencies, it’s obvious right? Well unless you have Windows then you will have to follow some manual steps… sorry, it’s not my fault you program in Windows… And finally I have another Python script there for the ā€œSetupā€ so you can indicate a folder of existing repositories so that the hook we talked about before is applied to them.

I don’t know if it will be useful to anyone, but it helps me a lot in my day to day!


What do you think?

Leave your opinion, question or suggestion. Comments are synced with GitHub Discussions .

← Back to blog