Create Selective Pre-Commit Hooks for Claude Code

This prompt generates a bash script that triggers specific tests and typechecks only for packages modified in your current git branch.

Agent prompt for Claude

Prompt

Prompt
Act as a Senior DevOps Engineer. Write a bash script for a pre-commit hook that uses git diff to identify changed directories and runs specific validation commands only for those paths. Follow these rules: 1. Use git diff --name-only to detect modified files. 2. Map file paths to specific test commands like npm run test or tsc. 3. Exit with status 1 if any command fails. 4. Include a fallback to run a full suite if too many files change. Output format: Provide the bash script block followed by instructions on how to install it in the .git/hooks directory. This is for technical automation and does not replace manual code review. Ask these 3 questions first: 1. What is your primary programming language or build tool? 2. What are the specific test and lint commands for your project? 3. What is the maximum number of files that should trigger a full suite run instead of a selective one?

Example output

How the selective pre-commit hook runsExample for a TypeScript monorepo with a 10-file threshold
  1. List staged filesgit diff --cached finds added, copied or modified files
  2. Check the file countMore than 10 files runs the full suite
  3. Match changed packagesFiles under packages/auth or packages/api are matched
  4. Run package testsnpm run test runs only in the matched package
  5. Install the hookSave to .git/hooks/pre-commit and run chmod +x

Example for: a TypeScript monorepo using Jest and tsc with a 10-file threshold.

Pre-Commit Hook Script

#!/bin/bash

CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
FILE_COUNT=$(echo "$CHANGED_FILES" | wc -l)

if [ "$FILE_COUNT" -gt 10 ]; then
echo "Too many files changed. Running full suite."
npm run test && npm run lint
exit $?
fi

for file in $CHANGED_FILES; do
if [[ $file == packages/auth/* ]]; then
cd packages/auth && npm run test && cd ../..
elif [[ $file == packages/api/* ]]; then
cd packages/api && npm run test && cd ../..
fi
done

Installation
  1. Save the code above as a file named pre-commit in your .git/hooks/ directory.
  2. Run chmod +x .git/hooks/pre-commit to grant execution permissions.
  3. The script will now run automatically every time you execute git commit.
Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Uses git diff filtering to prevent unnecessary CI/CD overhead.
  • Implements a file-count threshold to switch between selective and full testing.
  • Maps specific directory patterns to dedicated test commands.

Use this to optimize your CI/CD pipeline or local development workflow. Copy the generated script into .git/hooks/pre-commit and ensure it is executable. Works with Claude Code and Claude.

Common edits

  • Adjust the directory paths to match your specific repository structure.
  • Modify the test commands if your project uses different build tools like Vitest or Bazel.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

More in Claude Code

Headless Claude Code Script for CI: claude -p Task With Exit Codes and Output Checks

Writes a CI step that runs Claude Code non-interactively with claude -p, scoped tools, a turn limit and JSON output, and fails safely on errors, with a dry-run mode and secrets kept out of the repo.

AI Coding AgentsClaude Code

Create Automated Verification Loops for Long-Running Coding Agents

Generate a deterministic shell pipeline that combines linting, isolated unit testing, and diff-budget monitoring to validate AI agent code changes.

Design a Benchmark Harness for Claude Code Agent Accuracy

Create a structured testing framework to evaluate coding agent performance on pull requests using deterministic ground-truth verification.

Coding

Batch Rename Files in a Folder with a Script You Can Preview First

You get a PowerShell or Bash rename script that shows every old-to-new name before changing anything, writes an undo log, and includes plain run steps.

Automated Dependency Upgrade and Rollback Protocol

Generate a safe, step-by-step dependency upgrade plan that includes strict semver validation, lockfile parity checks, and automated rollback triggers.

AI Coding AgentsClaude Code

Design a Sandbox Security Policy for Autonomous Coding Agents

Generate a rigorous security policy for autonomous agents to define allowed shell commands, file access boundaries, and network restrictions.