Auto Git Add Check
All-in-one hook covering automatically, stage, modified, files. Includes structured workflows, validation checks, and reusable patterns for git workflow.
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 -pfor 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
| Aspect | Details |
|---|---|
| Trigger Point | PostToolUse on Edit, MultiEdit, and Write operations |
| Staging Method | git add "$CLAUDE_TOOL_FILE_PATH" for the modified file |
| Git Check | Verifies current directory is a git repository before staging |
| File Path Source | $CLAUDE_TOOL_FILE_PATH environment variable from tool context |
| Failure Handling | Errors suppressed with || true to prevent workflow interruption |
| Scope | Stages 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
| Parameter | Type | Default | Description |
|---|---|---|---|
matcher | string | Edit|MultiEdit|Write | Tool types that trigger automatic staging |
include_new_files | boolean | true | Whether to stage newly created files in addition to modified files |
exclude_patterns | string | (none) | Glob patterns for files that should not be auto-staged |
verbose | boolean | false | Whether to output a message confirming each staging operation |
dry_run | boolean | false | Log staging operations without actually running git add |
Best Practices
-
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.
-
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*.lockto theexclude_patternsto prevent these from cluttering your staging area. -
Review staged changes before committing - Run
git diff --stagedbefore 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. -
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.
-
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
-
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, orgit reset HEADto unstage everything before curating your commit. -
Hook fires but file is not staged - The
$CLAUDE_TOOL_FILE_PATHvariable 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. -
Staging fails silently in non-git directories - The hook checks for a git repository with
git rev-parse --git-dirand 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 withgit initif staging is needed.
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Pre-Commit Security Scanner
Pre-commit hook that scans staged files for hardcoded secrets, API keys, passwords, and sensitive data patterns before allowing commits.
Agents Md Watcher
Streamline your workflow with this automatically, loads, agents, configuration. Includes structured workflows, validation checks, and reusable patterns for automation.
Automated Build Inspector
Boost productivity using this automatically, trigger, build, processes. Includes structured workflows, validation checks, and reusable patterns for automation.