프리-툴 훅으로 Claude Code를 길들인 방법 (당신도 그래야 합니다)
Source: Dev.to
Background
저는 jo4.io라는 URL 단축기와 분석 기능을 만들면서 Claude Code를 AI 페어 프로그래머로 사용하고 있습니다. 생산성 향상은 엄청나지만, Claude가 명령을 실행하는 데 … 다소 과열된 모습을 보이는 것을 금방 눈치챘습니다.
The Problem
통계 추적 버그를 디버깅하던 중 Claude가 다음과 같은 행동을 시작했습니다:
cd /some/path명령을 실행해 터미널 작업 디렉터리를 뒤섞음- 검토 없이
git commit및git push시도 - 가끔
git reset --hard같은 파괴적인 Git 명령을 실행
Claude가 버그가 있는 것은 아닙니다—그냥 도움이 될 거라 생각해서 행동할 뿐입니다. 하지만 프로덕션 코드베이스에서는 커밋과 푸시가 이루어지는 것을 엄격히 제어해야 합니다.
Solution: PreToolUse Hooks
Claude Code는 PreToolUse hooks라는 강력하고 아직 충분히 활용되지 않은 기능을 제공합니다. 이를 통해 도구 호출(예: Bash 명령)을 가로채고 실행 전에 차단할 수 있습니다.
Hook Configuration (settings.json)
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/block-git.sh",
"timeout": 10
},
{
"type": "command",
"command": "~/.claude/block-cd.sh",
"timeout": 10
}
]
}
]
}
}
Blocking cd Commands (block-cd.sh)
#!/bin/bash
input=$(cat)
command=$(echo "$input" | jq -r '.tool_input.command // empty')
trimmed_command=$(echo "$command" | sed 's/^[[:space:]]*//')
# Block standalone cd, allow (cd /path && command)
if [[ "$trimmed_command" =~ ^cd[[:space:]] ]]; then
cat "The fix is complete. When you’re ready, you can commit with:
> \`git commit -m 'Fix stats tracking by extracting request data before async'\`"
fi
자동으로 커밋하고 푸시하는 대신 Claude가 제어권을 저에게 돌려주었습니다. 저는 변경 사항을 검토하고 테스트를 실행한 뒤 만족스러울 때 커밋했습니다.
Benefits Observed
- Safety – 실수로 강제 푸시하거나 하드 리셋을 하는 일이 없습니다.
- Reviewability – 언제든지 무엇이 커밋되는지 확인할 수 있습니다.
- Learning – Claude가 의도한 동작을 설명해 주어 학습 효과가 있습니다.
- Trust – 읽기 전용 작업에는 Claude에게 더 많은 자율성을 부여하면서, 쓰기 작업은 게이트를 두어 안전하게 관리할 수 있습니다.
Setup Instructions
-
Create the hooks directory
mkdir -p ~/.claude -
Add
settings.json(as shown above) to~/.claude/settings.json. -
Create the shell scripts (
block-git.shandblock-cd.sh) in~/.claude/. -
Make the scripts executable
chmod +x ~/.claude/*.sh -
Restart Claude Code to load the new configuration.
Future Hook Ideas
- 민감한 디렉터리에 대한
rm -rf차단. - 데이터베이스 마이그레이션에 확인 절차 요구.
- 모든 명령을 감사 파일에 기록.
Hook 시스템은 매우 유연합니다: 특정 도구를 매칭하고, 명령 내용을 검사하며, 맞춤형 오류 메시지를 반환할 수 있습니다.
Call to Action
Claude Code hooks를 사용하고 계신가요? 댓글로 여러분의 설정을 공유해 주세요!
Building jo4.io – a modern URL shortener with advanced analytics. Check it out at