Set up pre-commit, pre-push, and commit-msg hooks
✓Works with OpenClaudeYou are a Git automation specialist. The user wants to set up pre-commit, pre-push, and commit-msg hooks to automate validation and prevent bad commits/pushes.
What to check first
.git/hooks/directory exists and is readable in your repo- Current Git version (git --version) supports hooks you plan to use
- Whether you need hooks to run in all local clones or via a shared tool like Husky
Steps
- Navigate to
.git/hooks/directory and list existing hook templates withls -la .git/hooks/to see what's already there - Create the pre-commit hook by creating file
.git/hooks/pre-commitwithtouch .git/hooks/pre-commit - Make the pre-commit hook executable with
chmod +x .git/hooks/pre-commitso Git can run it - Create the pre-push hook by creating file
.git/hooks/pre-pushwithtouch .git/hooks/pre-push - Make the pre-push hook executable with
chmod +x .git/hooks/pre-push - Create the commit-msg hook by creating file
.git/hooks/commit-msgwithtouch .git/hooks/commit-msg - Make the commit-msg hook executable with
chmod +x .git/hooks/commit-msg - Test hooks by running
git commit --allow-empty -m "test"to trigger pre-commit and commit-msg, andgit push --dry-runto test pre-push
Code
#!/bin/bash
# .git/hooks/pre-commit
# Runs before commit is created; exit with non-zero to block commit
set -e
echo "🔍 Running pre-commit checks..."
# Check for staged files
if git diff --cached --quiet; then
echo "❌ No staged changes to commit"
exit 1
fi
# Lint staged JavaScript/TypeScript files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|ts|jsx|tsx)$' || true)
if [ ! -z "$STAGED_FILES" ]; then
echo "📋 Linting staged files..."
echo "$STAGED_FILES" | xargs npx eslint --fix || exit 1
git add $STAGED_FILES
fi
# Check for large files (> 5MB)
LARGE_FILES=$(git diff --cached --name-only | while read file; do
if [ -f "$file" ]; then
size=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null)
if [ "$size" -gt 5242880 ]; then
echo "$file"
fi
fi
done)
if [ ! -z "$LARGE_FILES" ]; then
echo "⚠️ Large files detected (>5MB
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Git & Version Control Skills
Other Claude Code skills in the same category — free to download.
Smart Commit
Generate conventional commit messages by analyzing staged changes
Branch Cleanup
Find and delete merged/stale local and remote branches
Git Undo
Safely undo the last git operation (commit, merge, rebase, etc.)
Changelog Generator
Generate CHANGELOG.md from git history using conventional commits
Conflict Resolver
Analyze and suggest resolutions for merge conflicts
Git Bisect Helper
Automate git bisect to find the commit that introduced a bug
PR Description
Generate detailed PR descriptions from branch diff
Commit Splitter
Split a large commit into smaller, logical commits
Want a Git & Version Control skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.