git-add-changes — Claude Code hook: automatic Git staging
After Claude Code edits or writes a file, this hook stages that file with Git. It prepares the change for a later commit.
npx claude-code-templates@latest --hook post-tool/git-add-changesWhat git-add-changes does
The hook runs after Edit and Write. It reads the changed file’s path from CLAUDE_TOOL_FILE_PATH and passes that path to git add, staging the file rather than every change in the project.
Errors are silenced, and the command returns successfully outside a Git repository. The hook won’t interrupt work there, but Git cannot stage the file without a repository. Install it with npx claude-code-templates@latest --hook post-tool/git-add-changes; its settings are in .claude/settings.json.
What it helps with
- stage a file after editing it
- prepare a newly written file for a commit
- automatically add files after
EditandWrite
Example prompts for the agent
- Edit
src/app.tsand stage that file for Git. - Create
README.md, then add it to the Git index. - Update
config.jsonand stage the changed file.
How to install git-add-changes
1claude-code-templates
npx claude-code-templates@latest --hook post-tool/git-add-changes2Manually: 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 git-add-changes
How do I install git-add-changes in Claude Code?
Run npx claude-code-templates@latest --hook post-tool/git-add-changes. The hook settings are in .claude/settings.json.
What does git-add-changes do?
After Edit or Write, it runs git add on the changed file so it is staged for a later commit.
Does git-add-changes work outside a Git repository?
Yes. It suppresses command errors and exits successfully outside a repository, though there is no Git index there to stage the file in.
Source text
git-add-changes.json · MIT
{
"description": "Automatically stage changes in git after file modifications for easier commit workflow. This hook runs 'git add' on any file that Claude edits or writes, automatically staging changes for the next commit. Includes error suppression so it won't fail in non-git repositories. Helps streamline the development workflow by preparing files for commit.",
"hooks": {
"PostToolUse": [
{
"matcher": "Edit",
"hooks": [
{
"type": "command",
"command": "git add \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true"
}
]
},
{
"matcher": "Write",
"hooks": [
{
"type": "command",
"command": "git add \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true"
}
]
}
]
}
}
© davila7 · MIT · davila7/claude-code-templates