Automate Your Git Commit Messages with AI
2025-06-12 08:18
TOC
本篇中文版:使用 AI 自動生成 Git Commit 訊息
What This Post Is About
After finishing some code—like fixing a bug or adding a new feature—writing Git commit messages can feel like a chore. Most of the time, we just jot down something quick that only makes sense to us at that moment. But when we need to revisit a past update, it becomes a time-consuming scavenger hunt to figure out which commit changed what.
That kind of mental effort? Let’s hand it over to AI.
August used the OpenAI API with the budget-friendly gpt-4o-mini model to instantly generate commit messages. Since it uses the API, there’s a small cost involved, but with gpt-4o-mini being so affordable, even heavy usage might cost less than $1 a month.
If you’ve stumbled upon this post, chances are you’ve been coding for a while and are looking to save time on writing commit messages. This post keeps things simple—no deep dives into setup. Instead, we’ll just drop the essentials and link to more detailed guides if you need them.
Listen in: Key takeaways from Google’s NotebookLM.
What You Need: OpenAI API Key
To have AI write your commit messages, you’ll need an OpenAI API key.
Just a heads-up: using AI isn’t free, but choosing the right model makes it super cost-effective.
We’ve covered how to get the key in previous posts, so here’s a link to that section:
CodiumAI PR-Agent: AI-Powered Code Reviews on GitLab
What You Need: Install Node.js
You probably already have Node.js installed—most frontend devs do. But if not, you can grab it from the official site:
After installing, run the following in your terminal to check if it worked:
Install Required Packages
To call the OpenAI API, you’ll need two packages: axios and dotenv.
axios: Handles HTTP requests and supports promises and interceptors.dotenv: Loads environment variables from a.envfile, which keeps your secrets safe.
Install them using npm:
|
|
Create a .env File
The .env file is where you’ll store your OpenAI API key. Since the key grants access to your account, keeping it private is important.
Your .env should look like this:
|
|
Make sure you don’t commit this file to your repo unless it’s a private project or hosted on a private Git server.
Add commit.cjs Script
This is the script that uses the OpenAI API to generate commit messages.
August tested a few prompts and landed on one that works well, but feel free to tweak it. If you come up with a better one, share it!
Here’s the script link:
The script skips over files with .min. in the name. If you want to skip additional files or folders, you can modify the filter, like so:
|
|
How to Use It
Once your code changes are staged with git add, just open a terminal in your project folder and run:
|
|
After a few seconds (depending on how many files changed), you’ll get a commit message back from OpenAI.
Here’s an example of what it looks like:

Example of an AI-generated Git commit message with a Summary and Description.
It gives you two parts: a short summary and a more detailed, bullet-style description.
Since different Git tools display commit messages differently—some only show the summary while others like Fork allow both—you can pick whichever works best for you.
Translated by ChatGPT.