Skip to main content
hook

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.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --hook post-tool/git-add-changes
Open on GitHub

What 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 Edit and Write

Example prompts for the agent

  • Edit src/app.ts and stage that file for Git.
  • Create README.md, then add it to the Git index.
  • Update config.json and stage the changed file.

How to install git-add-changes

1claude-code-templates

npx claude-code-templates@latest --hook post-tool/git-add-changes

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 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

Similar hooks

All hooks for Git and GitHub →

From the same set804

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