Commit Message Suggestion Bot

Sujoy GaurSujoy Gaur
3 min read

Introducing the Commit Message Suggestion AI Agent:

Writing a clear, concise, and meaningful commit messages is essential for maintaining a clean project history. Yet, many developers either skip this step or struggle to express their changes effectively. Enter the Commit Message Suggestion AI Agent — a smart assistant that will go through your staged changes and will give you the clear commit suggestion which will help you to maintain a good history of your commits.

What Is It?

The Commit Message Suggestion AI Agent is a tool that automatically generates commit messages based on the code changes in your changed files which are staged. By analyzing difference,of file before and after, it suggests context messages that are easy to understand by other developers.

How it Works:
Difference Analysis
The agent hooks into your Git workflow to analyze staged changes.

Contextual Understanding
It uses an AI model trained on large codebases and commit histories to understand what the change is doing.

Message Generation
Based on the diff and project conventions, it suggests a commit message.

If you change any file and add to staged than run this agent ,it will go through your code and gives you the concise and relatable commit message so that other developer can understand your commit history.
It will take your consent first and then commit the code.

Getting started:

install GoogleGenerative

npm install @google/generative-ai

Import GoogleGenerative AI:

import { GoogleGenerativeAI } from "@google/generative-ai";

Initialize Gemini Pro Model:

// Access your API key (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI('process.env.GEMINI_API_KEY');
const model = genAI.getGenerativeModel({ model: "models/gemini-1.5-flash" });
///write a system prompt for example:
  const prompt = `Generate a concise and clear git commit message based on the following diff:\n\n${diff}`;

generate commit function example:

async function generateCommitMessage(diff) {
  try {
    const prompt = `Generate a concise and clear git commit message based on the following diff:\n\n${diff}`;

    if (!model) {
      console.error("Gemini Pro model is not initialized.");
      return null;
    }

    const result = await model.generateContent({
      contents: [{ role: "user", parts: [{ text: prompt }] }],
    });
    const response = await result.response;
    const text = response.text();
    return text ? text.trim() : null;
  } catch (err) {
    console.error("Error generating commit message:", err);
    return null; // Return null in case of an error
  }
}
/// function to check the file diffs:

async function getGitDiff() {
  try {
    const diff = await git.diff(['--cached']);
    return diff;
  } catch (err) {
    console.error("Error getting git diff:", err);
  }
}
///Function to make commits

async function makeCommit(message) {
  if (message) {
    const answer = await rl.question(`Do you want to commit with the following message?\n\n${message}\n\n(y/N): `);

    if (answer.toLowerCase() === 'y') {
      try {
        await git.commit(message);
        console.log("✅ Commit created:", message);
      } catch (err) {
        console.error("Error committing:", err);
      }
    } else {
      console.log("Commit cancelled by user.");
    }
  } else {
    console.log("No commit message to create.");
  }
}

Smarter Commits, Better Codebase

The Commit Message Suggestion AI Agent saves time, increase clarity, and reinforces good development practices. Whether you’re a solo developer or part of a massive dev team, this tool is your next productivity booster.

3
Subscribe to my newsletter

Read articles from Sujoy Gaur directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sujoy Gaur
Sujoy Gaur

Dedicated and results-oriented Full-Stack Developer. Proficient in Vue.js,React.js, AdonisJS, Node.js, and Express.js, AWS. Passionate about problem-solving, collaborating with teams, and leveraging technology to make a meaningful impact. Build scalable, user-friendly applications that simplify complex workflows.