DEV Community

Cover image for AI generated git commit messages
Brian Douglas
Brian Douglas

Posted on • Edited on

AI generated git commit messages

Over my decorated 10-year career in development, I have seen some of the worst commit messages. I don't blame the developer on this either, because it is hard to remember what you just did at the time of the git commit.

image was generated using midjourney

In this 9 days of OpenAI series, I am looking at AI projects and their code to help demystify how any dev can build AI-generated projects.

Find more AI projects using OpenSauced

The last thing I want to do is traverse a git diff and figure it out after a productive day of coding. I love the Nutlope/aicommits.

GitHub logo Nutlope / aicommits

A CLI that writes your git commit messages for you with AI

What is Nutlope/aicommits?

Aicommits is a CLI that writes your git commit messages for you with AI. During my 9 days of OpenAI, you will see projects from @nutlope frequently. He has been shipping cool projects and sharing them on Twitter.

How does it work?

This post is meant to focus on the AI part of the code, but as soon as I looked at the GitHub, I was surprised to see the use of TABS, jk. I was impressed by this CLI tool that caught my eye, cleye (cleverly named). I built a few CLIs back in my day, and this cleye is the chosen tool for building the aicommits interactions on the command line. I will take a deeper look at cleye in the future and perhaps make something with it.

If you'd like to see the CLI implementation, it is a quick read in the src/cli.ts.



// src/cli.ts

const request = https.request({
  port: 443,
  hostname: 'api.openai.com',
  path: '/v1/completions',
  method: 'POST',
  headers: {
      'Content-Type': 'application/json',
      'Content-Length': postContent.length,
      Authorization: `Bearer ${apiKey}`,
  },

  ...


Enter fullscreen mode Exit fullscreen mode

I have not built anything with OpenAI as of yet, but this is a clean example of how someone could approach it.

Now looking at the code src/utils/openai.ts, I can see OpenAI being invoked and using the /v1/completions path. Per the README, this is not the ChatGPT completions but regular GTP-3 text completions.

After the REST call, there is some clean-up of the response and this clever error checker. This is required because of the frequent OpenAI downtime and server loads.



// src/utils/openai.ts

if (response.statusCode === 500) {
    errorMessage += '; Check the API status: https://status.openai.com';
}


Enter fullscreen mode Exit fullscreen mode

Finally, taking a looking at the createCompletion function, this is the actual place the magic is made. I found this more understandable than reading the OpenAI documentation, which I found a little overwhelming. I wish it had the ability to search (why does it not have search?).

I left comments below on what each line is doing.



// src/utils/openai.ts

const completion = await createCompletion(apiKey, {
    model, // text-davinci-003 - made for longer output, and consistent instruction
    prompt, // promptTemplate provided by nutlope
    temperature: 0.7, // higher the number, the more random the output
    top_p: 1, // like temperature but different results
    frequency_penalty: 0, // decreasing the model's likelihood of repeating the same line
    presence_penalty: 0, // increasing the model's likelihood of talking about new topics
    max_tokens: 200, // how much will this cost you? 
    stream: false, // partial message sending is off
    n: completions, // How many chat completion choices to generate for each input message
});


Enter fullscreen mode Exit fullscreen mode

There is a lot more I'd love to dig into, but I will leave the rest for you to take look. I do recommend installing aicommits locally to try it out. Just be sure to sign up for OpenAI to add your token.

demo of aicommits on the command line

If I need to correct something, or if you have some insight into the code, please comment. I enjoyed walking through the code and learning how this works. Thanks to Nutlope for sharing this with us, and be sure to contribute back upstream. Open Source FTW!

Also, if you have a project leveraging OpenAI, leave a link in the comments. I'd love to take a look and include it in my 9 days of OpenAI series.

Stay saucy.

Top comments (19)

Collapse
 
drhyde profile image
David Cantrell

I don't blame the developer on this either, because it is hard to remember what you just did at the time of the git commit.

git commit -v really helps here. Since I started enforcing it on myself (through use of a shell function that intercepts all my attempts to run git) my commit messages have got much better.

Collapse
 
vhoyer profile image
Vinícius Hoyer

git add --patch|-p has helped a lot in this also, both tools we all should be using, actually

Collapse
 
jcolag profile image
John Colagioia (he/him)

I have to walk a fine line, in this response, because...

  1. I love the technical challenge and want to encourage that sort of thing, but
  2. The premise seems horrifying and will generate ill-will from colleagues.

First, if you don't remember what work you did, then you probably put too much into your commits. While I realize that nobody's perfect and things slip through the cracks, each commit should do a specific thing, and someone can ideally add a specific feature (and nothing else) by cherry-picking other otherwise moving a defined set of commits.

Then, changes (in a professional environment) should all be connected to a ticket, so that people can track the work happening without analyzing the repository. Commits should call out the relevant ticket numbers and probably echo the language used in the ticket. I'm never going to search a repository for "the time that Zemzem removed a text box." I want "the work done on ticket #4586, ideally seeing the individual tasks involved in finishing that ticket."

Finally, you're allowed to use a graphical interface, when you commit, and look at the changes that you're committing. Again, if you did a bunch of things at once, you should probably use such a tool, so that you can commit specific lines that go together, instead of full files.

Again, that doesn't take away from the technical achievement or the entertainment value. And (like anything with generative AI) if you use the results as a springboard to what you'll really write, that'll probably serve you better. But I definitely worry about trying to "outsource" communication with your closest colleagues...

Collapse
 
aiocean profile image
AI Ocean

Look like developers have the sam idea, I also did the same thing with you. But i named it ai-commit instead. Use conversation form to ajust the message

Collapse
 
bdougieyo profile image
Brian Douglas

Please drop a link if its open source. Also to be clear this isn't my project, but a project open sourced by @nutlope

Collapse
 
eamodio profile image
Eric Amodio

If you use VS Code the latest pre-release version of GitLens also provides the ability to use ChatGPT to generate commit messages (and probably soon to explain commits). There is even a setting so the user can control the commit message style as part of the prompt.

Collapse
 
adriens profile image
adriens

Adopted here ;-p

Collapse
 
thebrown profile image
Saleumsack

It's very nice, but sometimes I'm too lazy to think about the commit message after the long hour code.

Collapse
 
0vortex profile image
TED Vortex (Teodor Eugen Duțulescu)

the too many requests are killing my vibe tho :<

Collapse
 
k1lgor profile image
k1lgor

It's nice, I tested it yesterday, but please note that too many commits (requests) in a short period of time will result in an error 400 - bad request. 👏🔝🎉

Collapse
 
bdougieyo profile image
Brian Douglas

Good call out. There are some obvious areas of improvements and I think this project is set up well for some one to take what is already there and make something useful for a lot of people.

Collapse
 
bharat55566 profile image
Bharat

Nice one

Collapse
 
sevapp profile image
Vsevolod

Very simple and inspiring implementation. I'll probably do a couple of experiments too =)