Last active
March 4, 2025 06:18
-
-
Save datajoely/3a6437abf46c6527c74ab2e4de5e5529 to your computer and use it in GitHub Desktop.
Using @simonw's LLM library to generate a git commit message based on the current git diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate the commit message using llm | |
current_branch=$(git rev-parse --abbrev-ref HEAD) | |
git_diff=$(git diff "$current_branch") | |
commit_message=$(echo "$git_diff" | llm prompt --model gpt-4o-mini " | |
- Generate a conventional commit message based on the provided git diff. | |
- Start with one of the following prefixes: 'build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'style', 'test'. | |
- Summarize the changes at a high level without listing every code modification. | |
- Use concise bullet points to describe key changes (up to 5 bullets). | |
- Skip detailed descriptions for cosmetic changes by ruff. | |
- **Do not include any Markdown formatting or code block delimiters (e.g., no triple backticks).** | |
Format: | |
{prefix}: {appropriate emoji} {summary} | |
- {appropriate emoji} bullet one | |
- {appropriate emoji} bullet two | |
- {appropriate emoji} bullet three | |
... | |
") | |
# Strip any remaining triple backticks just in case | |
commit_message=$(echo "$commit_message" | sed '/^```$/d') | |
# Preview the generated message | |
echo "----------------------------------------" | |
echo "Generated commit message:" | |
printf "%s\n" "$commit_message" | |
echo "----------------------------------------" | |
# Prompt for confirmation | |
while true; do | |
read -p "Do you want to use this commit message? (y/n) " confirm | |
if [[ "$confirm" == "y" ]]; then | |
git commit -m "$commit_message" | |
echo "Commit made with the generated message." | |
break | |
elif [[ "$confirm" == "n" ]]; then | |
echo "Commit aborted." | |
break | |
else | |
echo "Invalid input. Please enter 'y' or 'n'." | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is nice man adding to my personal dotfiles!