Automating Git Commits with AI: A Quick Guide Explained In 5 Minutes
Welcome to this quick guide on how to use AI tools and combine them with context engineering and custom slash commands. For those unfamiliar, context engineering is the practice of providing sufficient context to an AI to ensure it can execute its tasks correctly.
A powerful feature to leverage is the use of custom slash commands, which allow you to automate your everyday tasks. What makes this particularly powerful is the ability to prefix these commands with an exclamation mark to execute terminal commands directly. The best way to demonstrate these concepts is by walking through a practical example.
The Problem with Manual Git Commits
Consider a standard Git repository. This one, for instance, contains the source code for a project on building multi-agent systems. When working with any Git repository, you frequently have a set of changes that need to be committed and pushed to a remote. The challenge lies in writing a concise and descriptive commit message that accurately summarizes these changes.
While many IDEs can generate commit messages, and they are often quite good, they typically present a couple of common problems. First, they may not capture the full context of the changes. Second, if you follow a specific commit message convention—such as tagging the commit type (e.g., feat
, fix
) and adhering to character limits—IDE-generated messages often ignore these rules. This can lead to frustrating outcomes, like truncated subject lines that could have easily fit on a single line. Beyond formatting, the goal is always to have more descriptive and meaningful messages.
Automating Commit Messages with AI
Here’s how you can automate the entire process—from providing context to generating the final commit message—using an AI-powered command-line tool. By launching the AI tool in the CLI, we can use a custom command, let's call it git-commit
.
This command can read a predefined set of Git best practices. A key advantage is that these AI tools can often interface with existing editor configurations, allowing you to combine the strengths of both. As the command runs, it generates a commit message that summarizes all the changes, adheres to the specified style guidelines, and creates a title concise enough to display properly in platforms like GitHub.
After confirming the generated message, the tool performs the commit. A quick check of the Git log will show a perfectly formatted and descriptive commit message.
How to Set It Up in 3 Simple Steps
So, how is this accomplished? The setup is surprisingly straightforward.
Step 1: Define Your Git Best Practices
First, you need to define a set of rules for your Git commits. You can find numerous examples of these rule sets online, or you can use an AI assistant to generate one for you. For example, you could prompt it with: Generate a set of Git best practices for my repository.
This will produce a configuration file outlining your standards.
Step 2: Create a Custom Command
Next, create a custom command. This typically involves creating a specific directory structure, such as a .ai_tools/commands
folder in your project root. Inside this folder, you can create a markdown file named after your command, for instance, git-commit.md
. The filename will serve as the command name you invoke in the terminal.
Within this file, you'll specify which terminal tools the command is allowed to execute. For a Git commit command, you would permit tools like git add
, git status
, and git commit
. You also add a brief description, which will be displayed in the terminal when the command is run.
Step 3: Provide Context with Bash Commands
A key feature enabling this automation is the ability to execute bash commands. This is often done by prefixing the command with an !
.
To provide the necessary context to the AI, you can chain several commands together. The prompt for the AI would instruct it to:
1. Run !git status
to see staged changes.
2. Execute !git diff
to get the details of the code modifications.
3. Check the current branch with !git branch --show-current
.
4. Review the last 10+ commits with !git log -n 10
for historical context.
Using this information, the AI can then generate a single, high-quality commit message that aligns with your predefined best practices.
That's all it takes to create beautiful, informative, and consistently formatted Git commits. This simple automation can significantly streamline your development workflow.
Hopefully, this guide has been useful in demonstrating the power of context engineering—providing sufficient information to your LLM—and how to leverage bash command execution within custom AI commands.
Join the 10xdev Community
Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.