For years, my git history was cluttered with generic "wip" commit messages that did nothing for anyone who had to inspect the changes later on. If a teammate looked through a commit from six months ago, "wip" offered no insight. This problem led me to reconsider how I document my changes.
I decided to automate a part of my workflow. Rather than breaking my flow by pausing to write a detailed commit message, I integrated Claude into my shell via a simple bash function. With one command, I can have a commit message generated automatically based on the changes in my git diff.
My Commit Function
Here's the core part of the function:
function commit() {
commitMessage="$*"
git add .
if [ "$commitMessage" = "" ]; then
diff_input=$(echo "=== Summary ===" && git diff --cached --stat && echo -e "\n=== Diff (truncated if large) ===" && git diff --cached | head -c 50000)
commitMessage=$(echo "$diff_input" | claude -p "Write a single-line commit message for this diff. Output ONLY the message, no quotes, no explanation, no markdown.")
git commit -m "$commitMessage"
return
fi
eval "git commit -a -m '${commitMessage}'"
}
Using the function without arguments stages your changes and sends a summary and a truncated diff to Claude. Claude then returns a single-line commit message that clearly describes the change. If you provide your own message, it is used directly.
Dotfiles and Their Importance
Dotfiles (like .zshrc or .bashrc) control the behavior of your terminal and command line tools. Keeping these files in a git repository lets you easily replicate your development environment across machines. In my setup, I include this automated commit function among other useful tools in my dotfiles.
How the AI Generation Works
Instead of sending the entire diff to Claude, I send two carefully selected pieces of information:
- Summary: Generated by
git diff --cached --stat, showing a concise picture of file changes. - Diff (truncated): Limited to 50,000 characters to avoid overloading Claude with unnecessary details.
Claude processes this input with the prompt "Write a single-line commit message for this diff..." and returns a commit message like:
- Add caching layer to user repository
- Fix N+1 query in post index
- Remove deprecated payment gateway integration
These messages replace the vague "wip" messages with practical descriptions.
Adding a Spinner for a Smoother Experience
To improve the user experience, especially while waiting the 2-3 seconds it takes for Claude to generate a message, I added a spinner animation. This small detail provides visual feedback and transforms the process into a more polished tool. The full function includes steps for launching the spinner in the background, handling interrupts, and cleaning up after the message is generated.
By automating commit messages, I’ve streamlined my workflow without compromising on quality, making it easier for my team to understand the changes over time.