Skip to content

Instantly share code, notes, and snippets.

@valmat
Last active July 25, 2025 20:43
Show Gist options
  • Select an option

  • Save valmat/44822e1b7c6884bebb25b3ff005117fe to your computer and use it in GitHub Desktop.

Select an option

Save valmat/44822e1b7c6884bebb25b3ff005117fe to your computer and use it in GitHub Desktop.
AI git commit filler
#!/bin/env bash
set -e
# model="gpt-4.1";
# temperature=0.8;
model="o3";
temperature=1;
endpoint_url="https://api.openai.com/v1/chat/completions"
api_key="$OPENAI_API_KEY"
prompt="Below is the output of \`git diff HEAD\`."`
`"\nPlease provide a perfect git commit message."`
`"\nAlways use an emoji as the first character. Use emojis in the message text when appropriate."`
`"\nUse past tense in the message title. The commit message is intended for git, so its format is plain text (not Markdown)."`
`"\nIt would be nice to list the modified files in the commit message."
# checking whether the argument was passed to the script
if ! [[ $# -eq 0 ]]; then
prompt="${prompt}\n\nUse the following hint to generate the commit message: \`$1\`."
fi
prompt="${prompt}"`
`"\nThe commit message language is strictly English."`
`"\n\n(# If there is an error in the code, please report it in the comments to the commit)."
git add . --all
message=$(git diff HEAD)
if [ -z "$message" ]; then
echo "Nothing to commit, working tree clean" >&2
exit 1
fi
echo "$message" > "/tmp/gitai-message.txt"
json_payload=$(jq -n \
--arg model "$model" \
--arg temperature "$temperature" \
--arg prompt "$prompt" \
--rawfile message "/tmp/gitai-message.txt" \
'{
model: $model,
temperature: ($temperature | tonumber),
messages: [
{
role: "system",
content: $prompt
},{
role: "user",
content: $message
}
]
}')
echo "$json_payload" > "/tmp/gitai-payload.txt"
rm -f "/tmp/gitai-message.txt"
response=$(curl -s "$endpoint_url" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
--data-binary "@/tmp/gitai-payload.txt")
# Clean up the temporary file
rm "/tmp/gitai-payload.txt"
# Checking if the "error" key is in the response
if echo "$response" | jq -e '.error' > /dev/null; then
echo "Error: $(echo "$response" | jq -r '.error.message')" >&2
exit 2
else
# If there is no error, trying to extract the desired message
content=$(echo "$response" | jq -e -r '.choices[0].message.content')
# Checking if the message was extracted
if ! [[ $? -eq 0 ]]; then
# If the message could not be extracted,
# output the original text of the response to stderr
echo "Failed to extract the message. Response was:" >&2
echo "$response" >&2
exit 3
fi
fi
description_file=$(mktemp)
echo "$content" | sed '/^`\+$/d' > "$description_file"
nano "$description_file"
cat "$description_file"
cat "$description_file" | sed '/^#/d' | sed -e 's/^[ \t\n]*//' -e 's/[ \t\n]*$//' > "${description_file}.tmp"
mv "${description_file}.tmp" "$description_file"
git commit -a --file "$description_file"
rm "$description_file"
@valmat
Copy link
Author

valmat commented Jul 25, 2025

gitai is a bash script for Linux and MacOS that helps you create beautiful, informative, and consistent git commit messages using OpenAI. It adds relevant emojis, writes concise titles in past tense, includes a summary, lists changed files, and even uses English exclusively.

A commit message will not only be informative and stylish, but also include AI-powered code review—flagging possible bugs and improvement suggestions in the commit comment section.

Features

  • ✨ Uses an OpenAI-compatible API to generate perfect commit messages for your changes (easily switch to any compatible provider, such as xAI, by changing a couple of lines in the script).
  • 🐞 Reviews your code changes and suggests potential bugs or improvement tips directly in the commit message comments.
  • 📝 Automatically summarizes your code diff
  • 💬 Smartly inserts relevant emojis in titles and text
  • 📄 Lists modified files in the message
  • 💡 Accepts an optional hint for more context
  • 🏷️ Ensures messages are always in English
  • 🔒 Won't commit when there are no staged changes
  • 🖊️ Allows last-moment manual edits before committing

Requirements

  • Operating System: Linux or MacOS (Windows is not supported)
  • Dependencies:
    • jq (for JSON parsing)
    • curl (usually pre-installed)
    • nano (or modify the script to use another editor if desired)
    • OpenAI API Key: Set $OPENAI_API_KEY in your environment

Installation

  1. Install dependencies:

    # For Debian/Ubuntu and derivatives
    sudo apt-get install jq curl nano
    
    # For macOS (using Homebrew)
    brew install jq curl nano
  2. Download the script:

    • Copy the latest script from the GitHub Gist or download it directly:

      curl -o ~/bin/gitai https://gist.github.com/valmat/44822e1b7c6884bebb25b3ff005117fe/raw/906208f64f918a9990f56dd63f99177d71b7b023/gitai.sh
      chmod +x ~/bin/gitai
  3. Add to your $PATH:

    • Make sure ~/bin is in your PATH. You can add this to your ~/.bashrc, ~/.zshrc, or equivalent:

      export PATH="$HOME/bin:$PATH"
    • Restart your terminal or run source ~/.zshrc (or your shell config file).

  4. Set your OpenAI API key:

    export OPENAI_API_KEY="sk-...your key here..."
    • For convenience, add this line to your shell config file.

Usage

  1. Navigate to your project directory:

    cd /path/to/your/git/project
  2. Run the script:

    gitai
    • The script will:
      • Automatically git add all changes
      • Generate an AI-powered commit message based on the diff
      • Open the message in your preferred editor (nano by default) for final tweaks
      • Commit after you save and close the editor
  3. (Optional) Provide a hint for the AI:

    gitai "Fix for production crash on startup"
    • The hint will be appended to the prompt fed to the AI, improving message accuracy.

Notes

  • The script does not support Windows.
  • You can change the default editor by editing the script and replacing nano with your preferred text editor.
  • Your AI usage will count towards your OpenAI API billing, so use responsibly!

Example

# Typical usage
gitai

# With a custom hint for the commit message
gitai "Refactored backend integration"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment