auto-git-add — Claude Code hook: what it does, how to install
Stages modified files with git add immediately after Claude Code edits them. Keeps your staging area updated without manual git commands.
npx claude-code-templates@latest --hook git-workflow/auto-git-addWhat auto-git-add does
This hook hooks into the PostToolUse event for Edit, MultiEdit, and Write actions. When Claude Code edits or creates a file, the hook runs a shell script.
The command checks $CLAUDE_TOOL_FILE_PATH and verifies git presence with git rev-parse --git-dir. If both conditions pass, it stages the file using git add. It suppresses all errors via 2>/dev/null || true. Outside of a git repository, the hook does nothing.
What it helps with
- stage modified files right after Claude Code edits them
- eliminate manual git add commands during coding sessions
- track edited files across Edit, MultiEdit, and Write tools
Example prompts for the agent
- Fix the typo in src/index.ts
- Update the port configuration in config.json to 8080
- Create a button component in src/components/Button.tsx
How to install auto-git-add
1claude-code-templates
npx claude-code-templates@latest --hook git-workflow/auto-git-add2Manually: put the files in place
.claude/settings.jsonThe 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 auto-git-add
How to install auto-git-add in Claude Code?
Run npx claude-code-templates@latest --hook git-workflow/auto-git-add in your terminal, or place the hook configuration inside .claude/settings.json.
What does auto-git-add do?
It runs git add on any file modified by Claude Code as soon as the tool finish writing.
Which Claude Code tools trigger this hook?
The hook triggers on Edit, MultiEdit, and Write actions.
Source text
auto-git-add.json · MIT
{
"description": "Automatically stage modified files with git add after editing. Helps maintain a clean git workflow by staging changes as they're made.",
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|MultiEdit|Write",
"hooks": [
{
"type": "command",
"command": "if [[ -n \"$CLAUDE_TOOL_FILE_PATH\" ]] && git rev-parse --git-dir >/dev/null 2>&1; then git add \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true; fi"
}
]
}
]
}
}
© davila7 · MIT · davila7/claude-code-templates