Skip to main content
hook

smart-commit — Claude Code hook: auto-commit after edits

A Claude hook that commits the changed file right after Edit or Write. Useful when you would rather not build git history by hand.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --hook git-workflow/smart-commit
Open on GitHub

What smart-commit does

smart-commit attaches to PostToolUse events for the Edit and Write tools. Once Claude has changed a file, the hook checks that you are inside a git repository, stages the file and commits it. For edits the message is built from the file name and the size of the change: under 10 lines is minor, under 50 is moderate, anything larger is major. New files get a simpler message with Add new file and the name.

There is no script to maintain. Two shell commands live in .claude/settings.json and lean on git add, git diff --cached --numstat and git commit. If the file has not changed or the commit fails, the hook exits quietly, so you will not see an error.

The catch: a commit fires for every changed file right after each edit. On a repo with many small tweaks the log fills up with near-identical entries, and you never write the message yourself. Worth testing on a throwaway branch first.

What it helps with

  • record a file edit in git without a manual commit
  • keep a change history while the agent works
  • separate small edits from large ones by line count
  • commit a freshly created file
  • stop forgetting git during a Claude session

Example prompts for the agent

  • Edit src/api/client.js and let smart-commit handle the commit
  • Create docs/setup.md — the hook will commit it
  • Check git log to confirm a new commit appeared after the edit

How to install smart-commit

1claude-code-templates

npx claude-code-templates@latest --hook git-workflow/smart-commit

2Manually: put the files in place

.claude/settings.json

The hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json

Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command

Questions about smart-commit

How do I install smart-commit in Claude?

Run npx claude-code-templates@latest --hook git-workflow/smart-commit, then check the entry in .claude/settings.json.

What does smart-commit do?

After Edit or Write it stages the changed file and commits it. The message depends on the number of changed lines, or on the file being new.

When does the hook not fire?

Outside a git repository, when CLAUDE_TOOL_FILE_PATH is empty, and when the file has no changes. Commit errors are suppressed, so check git log.

Source text

smart-commit.json · MIT
{
  "description": "Intelligent git commit creation with automatic message generation and validation. Creates meaningful commits based on file changes.",
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit",
        "hooks": [
          {
            "type": "command",
            "command": "if git rev-parse --git-dir >/dev/null 2>&1 && [[ -n \"$CLAUDE_TOOL_FILE_PATH\" ]]; then git add \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null; CHANGED_LINES=$(git diff --cached --numstat \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null | awk '{print $1+$2}'); if [[ $CHANGED_LINES -gt 0 ]]; then FILENAME=$(basename \"$CLAUDE_TOOL_FILE_PATH\"); if [[ $CHANGED_LINES -lt 10 ]]; then SIZE=\"minor\"; elif [[ $CHANGED_LINES -lt 50 ]]; then SIZE=\"moderate\"; else SIZE=\"major\"; fi; MSG=\"Update $FILENAME: $SIZE changes ($CHANGED_LINES lines)\"; git commit -m \"$MSG\" \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true; fi; fi"
          }
        ]
      },
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "if git rev-parse --git-dir >/dev/null 2>&1 && [[ -n \"$CLAUDE_TOOL_FILE_PATH\" ]]; then git add \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null; FILENAME=$(basename \"$CLAUDE_TOOL_FILE_PATH\"); git commit -m \"Add new file: $FILENAME\" \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true; fi"
          }
        ]
      }
    ]
  }
}

© davila7 · MIT · davila7/claude-code-templates

Similar hooks

Everything for Git and GitHub →

From the same set205

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor