Claude Code現在能全自動推送代碼了,立刻刪掉你的commit腳本!
第一章:功能概述
? 核心功能:通過Claude Code的Stop鉤子實現自動Git提交與推送
? 觸發時機:每次Claude Code完成代碼任務響應時自動執行
? 操作流程:
檢查Git倉庫變更
暫存所有更改
創建包含會話ID和時間戳的描述性提交
推送至遠程倉庫
顯示提交狀態通知
第二章:配置步驟
Step 1:創建鉤子腳本
? 腳本路徑:~/.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輸入獲取會話ID(jq依賴必需)
非Git倉庫自動跳過
無變更時跳過提交
生成包含5個文件變更摘要的提交信息(含會話ID/時間戳/文件數)
提交失敗時返回警告,推送失敗時提示權限問題
Step 2:設置腳本權限
? 執行命令:chmod +x ~/.claude/hooks/auto-commit-after-task.sh
Step 3:配置Claude設置
? 修改~/.claude/settings.json:
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "/home/YOUR_USERNAME/.claude/hooks/auto-commit-after-task.sh"
}
]
}
]
}
}? 關鍵提示:必須替換YOUR_USERNAME為實際用戶名
Step 4:重啟生效
? 重啟Claude Code后鉤子將在新會話激活
第三章:提交示例
? 提交信息格式:
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第四章:定制選項
僅提交不推送
? 刪除腳本中的推送代碼段
# 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變量格式
臨時禁用鉤子
? 在settings.json中添加:
{
"disableAllHooks": true
}第五章:其他鉤子事件
? 支持的事件類型:
Stop(本文使用)
SessionEnd(會話結束時觸發)
PreToolUse/PostToolUse(工具調用前后)
UserPromptSubmit(用戶提交提示時)
? 文檔參考:https://docs.claude.com/en/docs/claude-code/hooks
第六章:故障排查
鉤子未運行
? 執行權限檢查:ls -la ~/.claude/hooks/auto-commit-after-task.sh
? 依賴驗證:安裝jq(Linux: sudo apt install jq / Mac: brew install jq)
? 配置校驗:使用JSON驗證器檢查settings.json語法
提交成功但推送失敗
? 檢查遠程倉庫配置:git remote -v
? 驗證推送權限及SSH密鑰/憑證
查看運行日志
? 在Claude Code輸出中檢查提示消息(如? Auto-committed and pushed changes)































