cd /news/developer-tools/git-commit-message-ai · home topics developer-tools article
[ARTICLE · art-7589] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Git Commit Message AI

Script for a shell function called `gcm` that uses an AI model (Gemini) to automatically generate a one-line git commit message from the staged diff. The function allows users to accept, edit, regenerate, or cancel the proposed message before committing. It is designed to be added to a user's `.bashrc` or `.zshrc` file and requires the `llm` CLI utility.

read2 min views18 publishedAug 26, 2024

unalias gcm 2>/dev/null

gcm() {
    generate_commit_message() {
        git diff --cached | llm -m gemini-1.5-flash-latest "

Below is a diff of all staged changes, coming from the command: ``` git diff --cached ``` Please generate a concise, one-line commit message for these changes."

    }

    read_input() {
        if [ -n "$ZSH_VERSION" ]; then
            echo -n "$1"
            read -r REPLY
        else
            read -p "$1" -r REPLY
        fi
    }

    echo "Generating AI-powered commit message using gemini..."
    commit_message=$(generate_commit_message)

    while true; do
        echo -e "\nProposed commit message:"
        echo "$commit_message"

        read_input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? "
        choice=$REPLY

        case "$choice" in
            a|A )
                if git commit -m "$commit_message"; then
                    echo "Changes committed successfully!"
                    return 0
                else
                    echo "Commit failed. Please check your changes and try again."
                    return 1
                fi
                ;;
            e|E )
                read_input "Enter your commit message: "
                commit_message=$REPLY
                if [ -n "$commit_message" ] && git commit -m "$commit_message"; then
                    echo "Changes committed successfully with your message!"
                    return 0
                else
                    echo "Commit failed. Please check your message and try again."
                    return 1
                fi
                ;;
            r|R )
                echo "Regenerating commit message using gemini..."
                commit_message=$(generate_commit_message)
                ;;
            c|C )
                echo "Commit cancelled."
                return 1
                ;;
            * )
                echo "Invalid choice. Please try again."
                ;;
        esac
    done
}
── more in #developer-tools 4 stories · sorted by recency
── more on @llm 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/git-commit-message-a…] indexed:0 read:2min 2024-08-26 ·