{"slug": "git-commit-message-ai", "title": "Git Commit Message AI", "summary": "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.", "body_md": "# -----------------------------------------------------------------------------\n# AI-powered Git Commit Function\n# Copy paste this gist into your ~/.bashrc or ~/.zshrc to gain the `gcm` command. It:\n# 1) gets the current staged changed diff\n# 2) sends them to an LLM to write the git commit message\n# 3) allows you to easily accept, edit, regenerate, cancel\n# But - just read and edit the code however you like\n# the `llm` CLI util is awesome, can get it here: https://llm.datasette.io/en/stable/\n\n# Unalias gcm if it exists (to prevent conflicts)\nunalias gcm 2>/dev/null\n\n# Define the AI-powered gcm function using gemini\ngcm() {\n    # Function to generate commit message using the gemini model\n    generate_commit_message() {\n        git diff --cached | llm -m gemini-1.5-flash-latest \"\nBelow is a diff of all staged changes, coming from the command:\n\\`\\`\\`\ngit diff --cached\n\\`\\`\\`\nPlease generate a concise, one-line commit message for these changes.\"\n    }\n\n    # Function to read user input compatibly with both Bash and Zsh\n    read_input() {\n        if [ -n \"$ZSH_VERSION\" ]; then\n            echo -n \"$1\"\n            read -r REPLY\n        else\n            read -p \"$1\" -r REPLY\n        fi\n    }\n\n    # Main script\n    echo \"Generating AI-powered commit message using gemini...\"\n    commit_message=$(generate_commit_message)\n\n    while true; do\n        echo -e \"\\nProposed commit message:\"\n        echo \"$commit_message\"\n\n        read_input \"Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? \"\n        choice=$REPLY\n\n        case \"$choice\" in\n            a|A )\n                if git commit -m \"$commit_message\"; then\n                    echo \"Changes committed successfully!\"\n                    return 0\n                else\n                    echo \"Commit failed. Please check your changes and try again.\"\n                    return 1\n                fi\n                ;;\n            e|E )\n                read_input \"Enter your commit message: \"\n                commit_message=$REPLY\n                if [ -n \"$commit_message\" ] && git commit -m \"$commit_message\"; then\n                    echo \"Changes committed successfully with your message!\"\n                    return 0\n                else\n                    echo \"Commit failed. Please check your message and try again.\"\n                    return 1\n                fi\n                ;;\n            r|R )\n                echo \"Regenerating commit message using gemini...\"\n                commit_message=$(generate_commit_message)\n                ;;\n            c|C )\n                echo \"Commit cancelled.\"\n                return 1\n                ;;\n            * )\n                echo \"Invalid choice. Please try again.\"\n                ;;\n        esac\n    done\n}", "url": "https://wpnews.pro/news/git-commit-message-ai", "canonical_source": "https://gist.github.com/HtutLynn/7fe83d64d6381726ac06e9fb7f972643", "published_at": "2024-08-26 18:17:08+00:00", "updated_at": "2026-05-22 05:07:09.192751+00:00", "lang": "en", "topics": ["developer-tools", "artificial-intelligence", "large-language-models", "open-source"], "entities": ["LLM", "Gemini", "Datasette", "Git"], "alternates": {"html": "https://wpnews.pro/news/git-commit-message-ai", "markdown": "https://wpnews.pro/news/git-commit-message-ai.md", "text": "https://wpnews.pro/news/git-commit-message-ai.txt", "jsonld": "https://wpnews.pro/news/git-commit-message-ai.jsonld"}}