Skip to content

Instantly share code, notes, and snippets.

@sandalsoft
Created May 19, 2025 18:40
Show Gist options
  • Save sandalsoft/a2fd3a770808b0f34ea692a76146e7a4 to your computer and use it in GitHub Desktop.
Save sandalsoft/a2fd3a770808b0f34ea692a76146e7a4 to your computer and use it in GitHub Desktop.
Use an LLM to create your commit message based on git diff (uses sigoden/aichat)
function gc() {
# Stage all changes
git add .
CHAR_LIMIT=400
# Get the staged changes using git diff
STAGED_CHANGES=$(PAGER="" git diff --cached)
if [ -z "$STAGED_CHANGES" ]; then
echo "No staged changes found. Please stage your changes first using 'git add'"
return 1
fi
# Check if a message prefix was provided as an argument
USER_PREFIX=""
if [ "$#" -gt 0 ]; then
USER_PREFIX="$* - "
fi
# Use aichat to generate commit message based on the changes
COMMIT_MESSAGE=$(echo "Create a concise git commit message (no more than $CHAR_LIMIT chars) describing these changes: $STAGED_CHANGES" | aichat)
# Prepend the user's text if provided
FINAL_COMMIT_MESSAGE="${USER_PREFIX}${COMMIT_MESSAGE}"
# Display the generated message and ask for confirmation
echo -e "\nGenerated commit message:\n$FINAL_COMMIT_MESSAGE"
echo -n "Do you want to use this commit message? (y/n) "
read confirm </dev/tty # Force reading from terminal
if [ "$confirm" = "y" ]; then
git commit -m "$FINAL_COMMIT_MESSAGE"
echo "Changes committed successfully!"
git push
echo "Changes pushed successfully!"
else
echo "Commit cancelled"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment