Skip to content

Instantly share code, notes, and snippets.

@fritzprix
Last active January 12, 2025 09:47
Show Gist options
  • Save fritzprix/5d2aa93a1d1e117ed374bb18dbd3bb27 to your computer and use it in GitHub Desktop.
Save fritzprix/5d2aa93a1d1e117ed374bb18dbd3bb27 to your computer and use it in GitHub Desktop.
🤖 Generate git commit messages automatically using local LLM (Ollama). Simple bash script that analyzes your git diff and creates meaningful commit messages. No API keys, no cloud - runs locally with Ollama.
#!/bin/bash
# Get the git diff and save it to a temporary file
git diff --cached > /tmp/git_diff.txt
# If there's no diff, exit
if [ ! -s /tmp/git_diff.txt ]; then
echo "No staged changes to commit"
exit 1
fi
# Create the prompt with the diff content
prompt="Given the following git diff, please write a clear and concise git commit message that explains the changes. Focus on WHAT changed and WHY. Use present tense, imperative mood. Keep it under 72 characters for the first line, then add more details if needed after a blank line:\n\n$(cat /tmp/git_diff.txt)"
# Run the prompt through ollama and save the response
ollama run llama3.2 "$prompt" > /tmp/commit_msg.txt
# Show the proposed commit message and ask for confirmation
echo -e "\nProposed commit message:"
echo "------------------------"
cat /tmp/commit_msg.txt
echo "------------------------"
echo -e "\nDo you want to proceed with this commit message? (y/n)"
read answer
if [ "$answer" = "y" ]; then
# Perform the commit using the generated message
git commit -F /tmp/commit_msg.txt
echo "Changes committed successfully!"
else
echo "Commit canceled"
fi
# Clean up temporary files
rm /tmp/git_diff.txt /tmp/commit_msg.txt
@fritzprix
Copy link
Author

🤖 LLM-Powered Git Commit Messages

Generate meaningful git commit messages automatically using Large Language Models (LLM) through Ollama.

🌟 Features

  • Automatically generates commit messages based on staged changes
  • Uses local LLM through Ollama for privacy and speed
  • Interactive confirmation before committing
  • Follows git commit message best practices

📋 Prerequisites

  • Ollama installed and running
  • A compatible LLM model pulled (default: llama2)
  • Git

🚀 Installation

  1. Save the script:
curl -o llm-commit.sh https://raw.githubusercontent.com/YOUR_GITHUB_USERNAME/GIST_ID/llm-commit.sh
chmod +x llm-commit.sh
  1. Optional: Add to your path or create an alias:
# Add to ~/.bashrc or ~/.zshrc
alias llm-commit='/path/to/llm-commit.sh'

💻 Usage

  1. Stage your changes as usual:
git add .
  1. Run the script:
./llm-commit.sh
  1. Review the generated commit message and confirm (y/n)

📜 The Script

#!/bin/bash

# Get the git diff and save it to a temporary file
git diff --cached > /tmp/git_diff.txt

# If there's no diff, exit
if [ ! -s /tmp/git_diff.txt ]; then
    echo "No staged changes to commit"
    exit 1
fi

# Create the prompt with the diff content
prompt="Given the following git diff, please write a clear and concise git commit message that explains the changes. Focus on WHAT changed and WHY. Use present tense, imperative mood. Keep it under 72 characters for the first line, then add more details if needed after a blank line:\n\n$(cat /tmp/git_diff.txt)"

# Run the prompt through ollama and save the response
ollama run llama2 "$prompt" > /tmp/commit_msg.txt

# Show the proposed commit message and ask for confirmation
echo -e "\nProposed commit message:"
echo "------------------------"
cat /tmp/commit_msg.txt
echo "------------------------"
echo -e "\nDo you want to proceed with this commit message? (y/n)"
read answer

if [ "$answer" = "y" ]; then
    # Perform the commit using the generated message
    git commit -F /tmp/commit_msg.txt
    echo "Changes committed successfully!"
else
    echo "Commit canceled"
fi

# Clean up temporary files
rm /tmp/git_diff.txt /tmp/commit_msg.txt

🛠️ Customization

You can modify the script to:

  • Use a different LLM model by changing llama3.2 to your preferred model
  • Customize the prompt template
  • Add additional git commit options

🤝 Contributing

Feel free to fork, improve, and submit PRs! Some ideas for improvements:

  • Add configuration file support
  • Implement different LLM providers
  • Add commit message templates
  • Improve error handling

📝 License

MIT License - feel free to use and modify as needed!

🙏 Credits

  • Inspired by discussions in the Ollama community
  • Uses Ollama for local LLM inference

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