Skip to content

Instantly share code, notes, and snippets.

@Talor-A
Created February 4, 2025 22:20
Show Gist options
  • Save Talor-A/b237a22c4f0c5f843060d8b6163d6725 to your computer and use it in GitHub Desktop.
Save Talor-A/b237a22c4f0c5f843060d8b6163d6725 to your computer and use it in GitHub Desktop.
git-llm: automatically create commit messages. powered by `llm` by simon willison
#!/bin/zsh
# -----------------------------------------------------------------------------
# the `llm` CLI util is awesome, can get it here: https://llm.datasette.io/en/stable/
generate_commit_message() {
# Get last 3 commit messages
last_commits=$(git log -3 --pretty=format:"- %s" 2>/dev/null)
git diff --cached | llm "
Below is a diff of all staged changes, coming from the command:
\`\`\`
git diff --cached
\`\`\`
For context, here are the last 3 commit messages:
$last_commits
Please generate a concise, one-line commit message for these changes that follows a similar style to the previous commits.
you can include thoughts, but make sure you prefix with # as comments for the editor.
start your response with # Analysis:
"
}
gcm() {
# Main script
temp_output=$(mktemp)
# Check if there are any staged changes
if [ -z "$(git diff --cached)" ]; then
echo "No staged changes."
return 1
fi
echo -n "\x1b[2m"
echo "thinking..."
generate_commit_message | tee "$temp_output"
# can insert awk '{ print $0; fflush(); }' before tee to get line-by-line output
# instead of token-by-token output, if you prefer.
echo -n "\x1b[0m"
commit_message=$(tail -n 1 "$temp_output")
rm "$temp_output"
# Create a temporary file for the commit message
temp_file=$(mktemp)
echo "$commit_message" > "$temp_file"
# Open the default git editor
if git config --get core.editor > /dev/null 2>&1; then
# Use git's configured editor
$(git config --get core.editor) "$temp_file"
else
# Fallback to default editor
${VISUAL:-${EDITOR:-vi}} "$temp_file"
fi
# Read the edited message
edited_message=$(cat "$temp_file")
rm "$temp_file" # Clean up
# Check if the message is empty (user aborted)
if [ -z "$edited_message" ]; then
echo "Commit cancelled."
return 1
fi
# Commit with the edited message
if git commit -m "$edited_message"; then
echo "Changes committed successfully!"
return 0
else
echo "Commit failed. Please check your changes and try again."
return 1
fi
}
gcm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment