Skip to content

Instantly share code, notes, and snippets.

@laurentperrinet
Forked from fritzprix/llm-commit.sh
Last active August 2, 2025 09:29
Show Gist options
  • Save laurentperrinet/176591df03d272fa7a2d14892c4a2622 to your computer and use it in GitHub Desktop.
Save laurentperrinet/176591df03d272fa7a2d14892c4a2622 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
# trying auto-commit tools - https://gist.github.com/fritzprix/5d2aa93a1d1e117ed374bb18dbd3bb27
echo -e "\nShould I git pull first? (y/n)"
read answer
if [[ -z "$answer" || "$answer" =~ ^[Yy]$ ]]; then
git pull
fi
# Get the git diff and save it to a temporary file
git add -u
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
cat /tmp/git_diff.txt
# 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 gemma3:4b "$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 [[ -z "$answer" || "$answer" =~ ^[Yy]$ ]]; 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
echo -e "\nShould I git push now? (y/n)"
read answer
if [[ -z "$answer" || "$answer" =~ ^[Yy]$ ]]; then
git push
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment