Skip to content

Instantly share code, notes, and snippets.

@sangdth
Created February 23, 2026 08:35
Show Gist options
  • Select an option

  • Save sangdth/abc97fd1044d01d30ef120ba37c8f7ad to your computer and use it in GitHub Desktop.

Select an option

Save sangdth/abc97fd1044d01d30ef120ba37c8f7ad to your computer and use it in GitHub Desktop.
Auto amend new change in commit message for `gt modify`

gtma

Generates conventional commit messages using AI and applies them via Graphite CLI (gt modify).

Requirements

Setup

Add the gtma function to your shell:

source ~/.zshrc  # or source the file containing gtma

Usage

gtma           # Generate message and amend current commit
gtma -c        # Generate message and create new commit
gtma --commit  # Same as -c

How It Works

  1. Detects staged/unstaged/untracked changes
  2. Sends diff to agent for conventional commit message generation
  3. Prepends new message on top of existing commit message
  4. Applies via gt modify --message

Message Format

<type>(scope): <description>

<body>

<previous commit message>

Examples

# Amend with AI-generated message
gtma

# Create new commit with AI-generated message
gtma -c
gtma() {
  local DO_COMMIT=false
  [[ "$1" == "-c" || "$1" == "--commit" ]] && DO_COMMIT=true

  local CHANGES=$(git diff HEAD; git ls-files --others --exclude-standard | xargs -I {} git diff --no-index /dev/null {})

  if [ -z "$CHANGES" ]; then
    echo "◯ No changes detected."
    return 0
  fi

  local OLD_MSG=$(git log -1 --format=%B 2>/dev/null)

  echo "🤖 Cursor is generating commit message..."

  local PROMPT="Based on the DIFF below, generate a full Git commit message.
  
  Format:
  - Header: <type>(scope): <description> (max 60 chars, imperative mood)
  - Blank line
  - Body: One or two paragraphs explaining the 'why' and 'what'. Wrap text at 72 chars.
  
  Constraints:
  - Types: feat, fix, docs, style, refactor, perf, test, chore.
  - Output ONLY the raw commit message text. No markdown blocks, no commentary.

  DIFF:
  $CHANGES"

  local MSG=$(agent -p "$PROMPT")
  if [ -z "$MSG" ]; then
    echo "❌ Generation failed."
    return 1
  fi

  if [ -n "$OLD_MSG" ]; then
    MSG="${MSG}\n\n${OLD_MSG}"
  fi

  echo "📝 $MSG"

  if $DO_COMMIT; then
    gt modify --all --commit --message "$MSG"
  else
    gt modify --all --message "$MSG"
  fi
}
@sangdth
Copy link
Author

sangdth commented Feb 23, 2026

Fix the \n\n when amend new message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment