Skip to content

Instantly share code, notes, and snippets.

@gcgbarbosa
Created May 1, 2025 12:46
Show Gist options
  • Select an option

  • Save gcgbarbosa/2a27ae4a33663acebdd0f61c21e52eed to your computer and use it in GitHub Desktop.

Select an option

Save gcgbarbosa/2a27ae4a33663acebdd0f61c21e52eed to your computer and use it in GitHub Desktop.
Generate a conventional commit on the CLI from git diff

You will need llm installed to run the function bellow.

Add it to your rc file (.bashrc, zshrc, ...)

gcllm() {
  # generate commit message
  local msg
  msg=$(
    echo "Generate a conventional commit message for <diff>$(git diff --staged)</diff>. The output will be passed directly to git commit -F -" \
      | llm \
      | sed '/```/d'
  )

  # print message
  printf "\nProposed commit message:\n---\n%s\n---\n" "$msg"

  # ask if we want to proceed
  printf "Proceed with commit? [y/N] " 
  read -r answer

  # move on
  if [[ $answer =~ ^[Yy]$ ]]; then
    # use a here-doc so newlines are preserved
    git commit -F <(printf "%s\n" "$msg")
  else
    echo "Commit aborted."
  fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment