Skip to content

Instantly share code, notes, and snippets.

@viko16
Last active January 15, 2026 15:18
Show Gist options
  • Select an option

  • Save viko16/86b17b691a7dc4e6ecdab4fdb3c47bfa to your computer and use it in GitHub Desktop.

Select an option

Save viko16/86b17b691a7dc4e6ecdab4fdb3c47bfa to your computer and use it in GitHub Desktop.
Git Commit Prompt 精简版

Read only staged files (git add) and ignore unstaged changes. Generate a Conventional Commits–compliant commit message based on the staged diff.

Format:

<type>(optional scope): <summary>
- <bullet 1>
- <bullet 2>
- <bullet 3>
- <bullet 4?> (optional, total 3–5)

Rules:

  • Choose an appropriate type (feat / fix / chore / refactor / docs / test, etc.)
  • Summary: concise, imperative, describes the main change
  • Body: 3–5 concrete bullets, each reflecting an actual staged change
  • Do git commit only (no push, no tag)
  • If nothing is staged, say so and stop
# 放到 ~/.zshrc 里
gitcommit() {
# 1. 预检查
if git diff --cached --quiet; then
echo "❌ No staged changes!"
return 1
fi
echo "🤖 Thinking..."
# 2. 生成 (通过 Pipe 传递 diff)
# 这种写法比参数传递更安全,不会因为 diff 太长报错
local msg=$(git diff --cached | claude -p "根据输入的 diff 生成一个简体中文的 conventional commit message,body 可以按需写 2~5 点。要求:只输出 message 纯文本,不要包含 Markdown 代码块,不要解释,不要引号。")
# 如果生成为空(防错)
if [[ -z "$msg" ]]; then
echo "❌ 生成失败"
return 1
fi
# 3. 展示
echo "\n\033[1;32mProposed Message:\033[0m"
echo "$msg"
echo ""
# 4. 交互:回车提交,Ctrl+C 取消
echo -n "按 [Enter] 确认提交,或 [Ctrl+C] 取消..."
read -r # 等待回车
# 5. 执行
git commit -m "$msg"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment