A

Auto Git Add Check

All-in-one hook covering automatically, stage, modified, files. Includes structured workflows, validation checks, and reusable patterns for git workflow.

HookClipticsgit workflowv1.0.0MIT
0 views0 copies

Auto Git Add Check

Automatically stages modified files with git add after every edit operation, maintaining a clean staging area that reflects all Claude Code changes.

When to Use This Hook

Attach this hook when you need to:

  • Automatically stage every file modification as it happens so your git staging area always reflects the current state of Claude Code changes
  • Eliminate the manual step of running git add after each edit, streamlining the commit preparation workflow
  • Ensure no file modifications are accidentally left unstaged when you are ready to commit changes

Consider alternatives when:

  • You prefer to selectively stage specific changes using git add -p for granular commit control
  • Your workflow involves reviewing and curating changes before staging, and automatic staging would undermine that review process

Quick Start

Configuration

name: auto-git-add-check type: hook trigger: PostToolUse category: git-workflow

Example Trigger

# Claude edits src/app.ts via Edit tool # Hook fires: # git add src/app.ts # File automatically staged

Example Output

Auto Git Add: src/app.ts
Action: File staged successfully
Git Status:
  M src/app.ts (staged)

Auto Git Add: src/utils/helper.ts
Action: New file staged successfully
Git Status:
  A src/utils/helper.ts (staged)

Core Concepts

Auto Staging Overview

AspectDetails
Trigger PointPostToolUse on Edit, MultiEdit, and Write operations
Staging Methodgit add "$CLAUDE_TOOL_FILE_PATH" for the modified file
Git CheckVerifies current directory is a git repository before staging
File Path Source$CLAUDE_TOOL_FILE_PATH environment variable from tool context
Failure HandlingErrors suppressed with || true to prevent workflow interruption
ScopeStages only the specific file modified by the tool, not all changes

Auto Staging Workflow

PostToolUse (Edit/MultiEdit/Write)
    |
    v
[Check $CLAUDE_TOOL_FILE_PATH is set?]
    |          |
  Empty       Set
    |          |
 [Skip]       v
         [Is this a git repository?]
              |          |
             No         Yes
              |          |
           [Skip]       v
                   [git add "$CLAUDE_TOOL_FILE_PATH"]
                        |
                   +----+----+
                   |         |
                Success   Error
                   |         |
                [Staged]  [Suppressed]

Configuration

ParameterTypeDefaultDescription
matcherstringEdit|MultiEdit|WriteTool types that trigger automatic staging
include_new_filesbooleantrueWhether to stage newly created files in addition to modified files
exclude_patternsstring(none)Glob patterns for files that should not be auto-staged
verbosebooleanfalseWhether to output a message confirming each staging operation
dry_runbooleanfalseLog staging operations without actually running git add

Best Practices

  1. Use with deliberate commit practices - Auto staging works best when combined with thoughtful commit messages. Since every edit is staged automatically, take extra care when composing commits to group logically related changes rather than committing everything at once.

  2. Add exclude patterns for generated files - Files that are regenerated during builds, like compiled output or lock files, should not be auto-staged. Add patterns like dist/* and *.lock to the exclude_patterns to prevent these from cluttering your staging area.

  3. Review staged changes before committing - Run git diff --staged before committing to review all auto-staged changes. This compensates for the loss of the manual staging review step and ensures you understand what will be included in the commit.

  4. Disable for exploratory sessions - When experimenting with code changes that you may want to discard, disable auto-staging temporarily. Otherwise you will need to unstage files manually before discarding changes.

  5. Combine with conventional commit hooks - Auto staging pairs well with pre-commit hooks that enforce commit message conventions. The staging happens automatically during development, and the commit hook validates the commit message format when you are ready to commit.

Common Issues

  1. Files staged that should not be committed - If you edit a file experimentally and then decide against the change, the auto-stager has already staged it. Use git reset HEAD <file> to unstage specific files, or git reset HEAD to unstage everything before curating your commit.

  2. Hook fires but file is not staged - The $CLAUDE_TOOL_FILE_PATH variable may be empty for certain tool invocations, particularly Bash operations that do not target specific files. The hook correctly skips staging when the path is empty, which is expected behavior.

  3. Staging fails silently in non-git directories - The hook checks for a git repository with git rev-parse --git-dir and skips staging if the check fails. If you are working outside a git repository, no error is reported and no staging occurs. Initialize a repository with git init if staging is needed.

Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates