Operations 12 min read

How to Automate Your Daily Dev Tasks with Claude Code Hooks

This guide explains how Claude Code Hooks can automatically run git status, dependency checks, linting, testing and other routine commands when you open a project, edit files, commit code, or perform risky operations, turning repetitive manual steps into a smooth, error‑free workflow.

AI Code to Success
AI Code to Success
AI Code to Success
How to Automate Your Daily Dev Tasks with Claude Code Hooks
Every day you open a project and repeat the same steps: check git status, install dependencies, run tests… Wouldn't it be great if these actions were automated?

01 The Pain of Repetitive Tasks

Imagine opening a project each morning and manually running git status, npm install, npm run lint, etc. Repeating these steps wastes time, and errors happen when a step is forgotten.

Open terminal, run git status to see uncommitted changes.

Run npm install to ensure dependencies are up to date.

Run npm run lint to check code style.

Open Claude Code and start working.

Doing this every day gets annoying, especially when you forget a step—like npm install —and waste time debugging.

Hooks are designed to solve exactly this problem.

02 What Are Hooks?

Hooks are automatic triggers for Claude Code that run predefined commands when certain events occur, such as opening a session, using a tool, or committing code.

Analogy:

When you open a project → SessionStart Hook → automatically runs git status.

When you edit a file → preToolUse Hook → automatically backs up the file.

When you commit code → preCommit Hook → automatically runs tests.

When you perform a dangerous operation → userPromptSubmit Hook → double confirmation.

03 Four Core Hooks

SessionStart

When triggered? When you open a Claude Code session.

What it can do?

Show git status.

Check if dependencies are installed.

Load project context.

Display a welcome message.

Basic example: show git status on start

{
  "hooks": {
    "SessionStart": {
      "command": "git status"
    }
  }
}

Advanced example: multiple checks

{
  "hooks": {
    "SessionStart": {
      "command": "echo '=== Project status ===' && git status --short && echo '' && echo '=== Dependency check ===' && if [ ! -d 'node_modules' ]; then echo '⚠️ Run npm install'; else echo '✅ Dependencies OK'; fi"
    }
  }
}

preToolUse / postToolUse

When triggered? Before or after a tool is used.

Automatically backup files before editing.

Log after command execution.

Auto‑format after file changes.

Example: backup before edit

{
  "hooks": {
    "preToolUse": {
      "command": "cp $CLAUDE_FILE_PATH $CLAUDE_FILE_PATH.backup",
      "when": "tool:Edit"
    }
  }
}

Example: voice notification after deployment

{
  "hooks": {
    "postToolUse": {
      "command": "say 'Deployment complete!'",
      "when": "tool:Bash && command_contains:deploy"
    }
  }
}

preCommit / postCommit

When triggered? Before or after git commit.

Run tests automatically before commit.

Check code style before commit.

Tag after commit.

Send notification after commit.

Example: lint and test before commit

{
  "hooks": {
    "preCommit": {
      "command": "npm run lint && npm test"
    }
  }
}

Example: push after commit

{
  "hooks": {
    "postCommit": {
      "command": "git push"
    }
  }
}

userPromptSubmit (Permission Control)

When triggered? Before executing a potentially dangerous command.

Second confirmation for risky actions.

Validate input.

Block dangerous operations.

Example: confirm before rm -rf

{
  "hooks": {
    "userPromptSubmit": {
      "command": "if echo '$CLAUDE_PROMPT' | grep -q 'rm -rf'; then echo '⚠️ Dangerous operation! Confirm deletion'; exit 1; fi",
      "block": true
    }
  }
}

04 Full‑stack Real‑World Example: Smart Development Environment

Scenario: Daily development of a React project.

Pain points:

Need to check many things before starting.

Often forget to format code, causing PR rejections.

Sometimes discover failing tests after committing.

Unclear when deployment finishes.

Solution: Use Hooks to automate the whole flow.

{
  "hooks": {
    "SessionStart": {
      "command": "echo '🚀 React project environment check' && git status --short && echo '' && npm run outdated || echo '✅ Dependencies OK'"
    },
    "preToolUse": {
      "command": "if [[ '$CLAUDE_TOOL' == 'Edit' && '$CLAUDE_FILE_PATH' == *.js* ]]; then eslint --fix '$CLAUDE_FILE_PATH'; fi",
      "when": "tool:Edit"
    },
    "preCommit": {
      "command": "npm run lint && npm test"
    },
    "postToolUse": {
      "command": "if [[ '$CLAUDE_COMMAND' == *deploy* ]]; then say 'Deployment complete'; fi",
      "when": "tool:Bash && command_contains:deploy"
    }
  }
}

Resulting benefits:

✅ Automatic environment and dependency check on start.

✅ Auto‑format JS files on edit.

✅ Run tests before commit.

✅ Voice notification after deployment.

05 Best Practices and Pitfalls

Configuration File Location

Hooks are defined in .claude/settings.json (project level) or ~/.claude/settings.json (global).

Common Mistakes

❌ Mistake 1: Overly complex commands – keep hooks fast.

{
  "hooks": {
    "SessionStart": {
      "command": "docker-compose up -d && npm install && npm run build && npm run test"
    }
  }
}

✅ Correct: Only essential checks; heavy tasks should be manual.

❌ Mistake 2: No error handling.

{
  "hooks": {
    "preCommit": {
      "command": "npm run lint"
    }
  }
}

✅ Correct: Add error handling.

{
  "hooks": {
    "preCommit": {
      "command": "npm run lint || { echo '❌ Lint failed, aborting commit'; exit 1; }"
    }
  }
}

❌ Mistake 3: Wrong condition syntax.

{
  "hooks": {
    "preToolUse": {
      "command": "echo 'editing file'",
      "when": "tool:Edit && file_matches:*.js"
    }
  }
}

✅ Correct syntax:

{
  "hooks": {
    "preToolUse": {
      "command": "echo 'editing file'",
      "when": "tool:Edit",
      "if": "file_matches:**/*.js"
    }
  }
}

Performance Tips

Avoid time‑consuming operations: Keep hook commands short.

Use conditional triggers: Control execution with when and if.

Run heavy tasks in background: Append & to the command.

Debugging Techniques

Log hook execution:

{
  "hooks": {
    "SessionStart": {
      "command": "echo '[Hook] SessionStart triggered' >> /tmp/claude-hooks.log && git status"
    }
  }
}

Test configuration manually:

# Manual test
echo '=== Test SessionStart ===' && git status --short
# Validate JSON
cat .claude/settings.json | jq .

06 Summary

Hooks are the automation engine of Claude Code, reducing repetitive work, preventing human error, and boosting development efficiency.

Reduce repetitive operations.

Avoid oversights.

Increase productivity.

Key takeaways:

Start simple and gradually add complexity.

Handle errors to prevent hook failures from breaking the workflow.

Regularly review and prune unused hooks.

Prefer project‑level configuration; use global as a fallback.

CLIautomationhooksproductivitydev workflowClaude Code
AI Code to Success
Written by

AI Code to Success

Focused on hardcore practical AI technologies (OpenClaw, ClaudeCode, LLMs, etc.) and HarmonyOS development. No hype—just real-world tips, pitfall chronicles, and productivity tools. Follow to transform workflows with code.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.