Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save valmat/cd64141685f2655c4a02d59902962ca3 to your computer and use it in GitHub Desktop.
Automatically creates a new tag and AI generated description for it
#!/bin/env bash
set -e
model="gpt-4.1";
temperature=0.8;
endpoint_url="https://api.openai.com/v1/chat/completions"
api_key="$OPENAI_API_KEY"
# the hash of the commit that was marked with the last tag
latest_tag_hash=$(git rev-list --tags --max-count=1)
# the name of the last tag, using its hash
latest_tag=$(git describe --tags --abbrev=0 $latest_tag_hash)
minor_ver="${latest_tag##*.}"
base_ver="${latest_tag%.*}"
((minor_ver+=1))
new_tag="${base_ver}.${minor_ver}"
prompt="Below is the output of \`git log <latest_tag>..HEAD\`."`
`"\nPlease provide a perfect tag ($new_tag) message."`
`"\nUse emojis in the message text when appropriate. Use past tense."`
`"\nProvide a message ready for direct substitution "`
`"(don't use \`\`\` and provide multiline message)."`
`" The message is intended for git, so format it as a plain text (not Markdown)."
if ! [[ $# -eq 0 ]]; then
prompt="${prompt}\n\nUse the following hint provided by the developer to generate the tag message: \`$1\`."`
`"\nThe message language is strictly English."
fi
# Output the commit log from the previous tag to the HEAD (current state)
# use the found name of the previous tag to start the story
message=`git log $latest_tag..HEAD`
echo "$latest_tag -> $new_tag"
if [ -z "$message" ]; then
echo "Nothing to commit, working tree clean" >&2
exit 1
fi
echo "$message" > "/tmp/gitaitag-message.txt"
json_payload=$(jq -n \
--arg model "$model" \
--arg temperature "$temperature" \
--arg prompt "$prompt" \
--rawfile message "/tmp/gitaitag-message.txt" \
'{
model: $model,
temperature: ($temperature | tonumber),
messages: [
{
role: "system",
content: $prompt
},{
role: "user",
content: $message
}
]
}')
echo "$json_payload" > "/tmp/gitaitag-payload.txt"
rm -f "/tmp/gitaitag-message.txt"
response=$(curl -s "$endpoint_url" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
--data-binary "@/tmp/gitaitag-payload.txt")
# Clean up the temporary file
rm "/tmp/gitaitag-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" > "$description_file"
nano "$description_file"
git tag -a "$new_tag" -F "$description_file"
cat "$description_file"
rm "$description_file"
@valmat
Copy link
Author

valmat commented Jul 25, 2025

gitaitag: AI-powered Git Tag Message Generator

gitaitag is a bash script for Linux and MacOS that helps you create clear, expressive, and professional git tag messages using an OpenAI-compatible API.
It automatically suggests the next tag version, summarizes all changes since the last tag, and generates a tag description complete with appropriate emojis and an English, developer-friendly style.

No more struggling over "release notes" or tag comments—let the AI do the heavy lifting!


Features

  • 🚀 OpenAI-compatible: Uses any compatible generative API (e.g., OpenAI, xAI, etc.)—switch by editing just a couple of lines.
  • 🏷️ Automatic tag bump: Calculates the next tag version for you (e.g., “v1.2.3” → “v1.2.4”) based on the last tag.
  • ✍️ AI-powered changelog: Analyzes all commits since the last tag and generates a meaningful, multi-line tag message.
  • 🤖 Emojis & style: Makes tag messages more memorable and reader-friendly by adding relevant emojis where appropriate.
  • 💡 Hint support: Optionally improve tag message accuracy by providing your own context/hint to the AI.
  • 🏷️ Always in English: Ensures messages are generated in strict English.
  • ✏️ Manual review: Allows you to review and edit the tag message before it's saved and the tag is created.
  • 🔒 Safe: Won’t proceed if no new commits are available since the last tag.

Requirements

  • Operating System: Linux or MacOS (Windows is not supported)
  • Dependencies:
    • jq (for JSON parsing)
    • curl (for API calls)
    • nano (or modify in the script for your favorite editor)
    • 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 script from the GitHub Gist or use direct download:

      curl -o ~/bin/gitaitag https://gist.github.com/valmat/cd64141685f2655c4a02d59902962ca3/raw/656979de3739a3aab9ed9b6e108982155d0833db/gitaitag.sh
      
      chmod +x ~/bin/gitaitag
  3. Add to your $PATH:

    • Ensure ~/bin is in your PATH (add this to ~/.bashrc, ~/.zshrc, etc.):

      export PATH="$HOME/bin:$PATH"
    • Restart your terminal or source your shell config file.

  4. Set your OpenAI API key:

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

Usage

  1. Go to your git project directory:

    cd /path/to/your/repo
  2. Run the script:

    gitaitag
    • The script will:
      • Retrieve the latest git tag and suggest the next version (e.g., “v1.2.3” → “v1.2.4”)
      • Analyze all commits since the last tag
      • Generate an AI-powered tag message (multi-line, with emojis and clear description)
      • Open it in your editor (nano by default) for optional edits
      • Create an annotated git tag with the generated message
  3. (Optional) Provide a hint for the AI:

    gitaitag "Major refactor and new API integration"
    • The hint will help the AI fine-tune the tag message.

Notes

  • The script does not support Windows.
  • You can change the default editor by modifying the line with nano in the script.
  • If there are no new commits since the previous tag, it will safely abort.
  • Each AI query counts towards your OpenAI (or other provider) API usage.

Example

# Standard usage
gitaitag

# With a context/hint for the tag message
gitaitag "Initial stable version for production"

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