Claude Code現(xiàn)在能全自動(dòng)推送代碼了,立刻刪掉你的commit腳本!
第一章:功能概述
? 核心功能:通過(guò)Claude Code的Stop鉤子實(shí)現(xiàn)自動(dòng)Git提交與推送
? 觸發(fā)時(shí)機(jī):每次Claude Code完成代碼任務(wù)響應(yīng)時(shí)自動(dòng)執(zhí)行
? 操作流程:
檢查Git倉(cāng)庫(kù)變更
暫存所有更改
創(chuàng)建包含會(huì)話ID和時(shí)間戳的描述性提交
推送至遠(yuǎn)程倉(cāng)庫(kù)
顯示提交狀態(tài)通知
第二章:配置步驟
Step 1:創(chuàng)建鉤子腳本
? 腳本路徑:~/.claude/hooks/auto-commit-after-task.sh
#!/bin/bash
# Auto-commit and push after Claude Code completes a task
# This hook runs when Claude finishes responding (Stop event)
set -e
# Parse JSON input
INPUT=$(cat)
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // "unknown"')
WORKING_DIR=$(pwd)
# Only run if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo '{"systemMessage": "Not a git repository, skipping auto-commit",
"suppressOutput": true}'
exit 0
fi
# Check if there are any changes to commit
if git diff --quiet && git diff --cached --quiet; then
echo '{"systemMessage": "No changes to commit", "suppressOutput": true}'
exit 0
fi
# Get a brief summary of what changed for the commit message
CHANGED_FILES=$(git status --short | head -5)
NUM_FILES=$(git status --short | wc -l)
# Create commit message
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
COMMIT_MSG="Auto-commit after Claude Code session
Session: $SESSION_ID
Time: $TIMESTAMP
Files changed: $NUM_FILES
Changes:
$CHANGED_FILES
?? Generated with Claude Code
"
# Stage all changes
git add -A
# Commit
if git commit -m "$COMMIT_MSG" > /dev/null 2>&1; then
# Try to push (silently fail if no remote or push fails)
if git push > /dev/null 2>&1; then
echo '{"systemMessage": "? Auto-committed and pushed changes", "suppressOutput":
false}'
else
echo '{"systemMessage": "? Auto-committed changes (push failed - no remote or
auth issue)", "suppressOutput": false}'
fi
else
echo '{"systemMessage": "?? Commit failed", "suppressOutput": true}'
fi? 核心邏輯:
解析JSON輸入獲取會(huì)話ID(jq依賴必需)
非Git倉(cāng)庫(kù)自動(dòng)跳過(guò)
無(wú)變更時(shí)跳過(guò)提交
生成包含5個(gè)文件變更摘要的提交信息(含會(huì)話ID/時(shí)間戳/文件數(shù))
提交失敗時(shí)返回警告,推送失敗時(shí)提示權(quán)限問(wèn)題
Step 2:設(shè)置腳本權(quán)限
? 執(zhí)行命令:chmod +x ~/.claude/hooks/auto-commit-after-task.sh
Step 3:配置Claude設(shè)置
? 修改~/.claude/settings.json:
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "/home/YOUR_USERNAME/.claude/hooks/auto-commit-after-task.sh"
}
]
}
]
}
}? 關(guān)鍵提示:必須替換YOUR_USERNAME為實(shí)際用戶名
Step 4:重啟生效
? 重啟Claude Code后鉤子將在新會(huì)話激活
第三章:提交示例
? 提交信息格式:
Auto-commit after Claude Code session
Session: 3e5e7a78-c91c-4c3f-842f-4294b4714c35
Time: 2025-10-09 20:07:11
Files changed: 3
Changes:
M main.py
M config.py
A new_file.py
?? Generated with Claude Code第四章:定制選項(xiàng)
僅提交不推送
? 刪除腳本中的推送代碼段
# Try to push (silently fail if no remote or push fails)
if git push > /dev/null 2>&1; then
echo '{"systemMessage": "? Auto-committed and pushed changes", "suppressOutput":
false}'
else
echo '{"systemMessage": "? Auto-committed changes (push failed - no remote or auth
issue)", "suppressOutput": false}'
fi替換為:
echo '{"systemMessage": "? Auto-committed changes", "suppressOutput": false}'自定義提交信息
? 直接修改腳本中的COMMIT_MSG變量格式
臨時(shí)禁用鉤子
? 在settings.json中添加:
{
"disableAllHooks": true
}第五章:其他鉤子事件
? 支持的事件類型:
Stop(本文使用)
SessionEnd(會(huì)話結(jié)束時(shí)觸發(fā))
PreToolUse/PostToolUse(工具調(diào)用前后)
UserPromptSubmit(用戶提交提示時(shí))
? 文檔參考:https://docs.claude.com/en/docs/claude-code/hooks
第六章:故障排查
鉤子未運(yùn)行
? 執(zhí)行權(quán)限檢查:ls -la ~/.claude/hooks/auto-commit-after-task.sh
? 依賴驗(yàn)證:安裝jq(Linux: sudo apt install jq / Mac: brew install jq)
? 配置校驗(yàn):使用JSON驗(yàn)證器檢查settings.json語(yǔ)法
提交成功但推送失敗
? 檢查遠(yuǎn)程倉(cāng)庫(kù)配置:git remote -v
? 驗(yàn)證推送權(quán)限及SSH密鑰/憑證
查看運(yùn)行日志
? 在Claude Code輸出中檢查提示消息(如? Auto-committed and pushed changes)































